Go語言常用加密算法和加密技術(shù)全面解析!

Go語言常用加密算法和加密技術(shù)全面解析!

成都創(chuàng)新互聯(lián)是一家專注網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷策劃、成都小程序開發(fā)、電子商務(wù)建設(shè)、網(wǎng)絡(luò)推廣、移動(dòng)互聯(lián)開發(fā)、研究、服務(wù)為一體的技術(shù)型公司。公司成立十年以來,已經(jīng)為上1000家封陽臺(tái)各業(yè)的企業(yè)公司提供互聯(lián)網(wǎng)服務(wù)?,F(xiàn)在,服務(wù)的上1000家客戶與我們一路同行,見證我們的成長;未來,我們一起分享成功的喜悅。

在現(xiàn)代計(jì)算機(jī)領(lǐng)域,安全性非常重要,而加密技術(shù)是保證計(jì)算機(jī)信息系統(tǒng)安全性的重要措施之一。為了滿足這個(gè)需求,Go語言提供了豐富的加密算法和加密技術(shù),本文將為大家詳細(xì)解析Go語言中常用的加密算法和加密技術(shù)。

一、對(duì)稱加密算法

對(duì)稱加密算法是加密和解密使用相同的密鑰的一種加密方式。常用的對(duì)稱加密算法有DES、3DES、AES等。Go語言中的crypto包中提供了多種對(duì)稱加密算法。

以下是一個(gè)示例,使用AES加密算法加密字符串,然后再進(jìn)行解密:

`go

package main

import (

"crypto/aes"

"crypto/cipher"

"fmt"

)

func AesEncrypt(plainText, key byte) (byte, error) {

block, err := aes.NewCipher(key)

if err != nil {

return nil, err

}

iv := byte("1234567890123456")

plainText = pkcs5Padding(plainText, block.BlockSize())

blockMode := cipher.NewCBCEncrypter(block, iv)

cipherText := make(byte, len(plainText))

blockMode.CryptBlocks(cipherText, plainText)

return cipherText, nil

}

func AesDecrypt(cipherText, key byte) (byte, error) {

block, err := aes.NewCipher(key)

if err != nil {

return nil, err

}

iv := byte("1234567890123456")

blockMode := cipher.NewCBCDecrypter(block, iv)

plainText := make(byte, len(cipherText))

blockMode.CryptBlocks(plainText, cipherText)

plainText = pkcs5UnPadding(plainText)

return plainText, nil

}

func pkcs5Padding(cipherText byte, blockSize int) byte {

padding := blockSize - len(cipherText)%blockSize

padText := bytes.Repeat(byte{byte(padding)}, padding)

return append(cipherText, padText...)

}

func pkcs5UnPadding(plainText byte) byte {

length := len(plainText)

unPadding := int(plainText)

return plainText

}

func main() {

plainText := byte("Hello World")

key := byte("12345678901234567890123456789012")

cipherText, err := AesEncrypt(plainText, key)

if err != nil {

fmt.Println(err)

return

}

fmt.Printf("Cipher Text: %x\n", cipherText)

plainTextResult, err := AesDecrypt(cipherText, key)

if err != nil {

fmt.Println(err)

return

}

fmt.Printf("Plain Text: %s\n", plainTextResult)

}

二、非對(duì)稱加密算法非對(duì)稱加密算法也稱為公鑰加密算法,加密和解密使用不同的密鑰。常用的非對(duì)稱加密算法有RSA、ECC等。Go語言中的crypto包中也提供了多種非對(duì)稱加密算法。以下是一個(gè)示例,使用RSA加密算法加密字符串,然后再進(jìn)行解密:`gopackage mainimport ( "crypto/rand" "crypto/rsa" "crypto/x509" "encoding/pem" "fmt")func RsaEncrypt(plainText byte, publicKey byte) (byte, error) { block, _ := pem.Decode(publicKey) if block == nil { return nil, fmt.Errorf("public key pem decode failed") } pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes) if err != nil { return nil, err } pub := pubInterface.(*rsa.PublicKey) cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, pub, plainText) if err != nil { return nil, err } return cipherText, nil}func RsaDecrypt(cipherText byte, privateKey byte) (byte, error) { block, _ := pem.Decode(privateKey) if block == nil { return nil, fmt.Errorf("private key pem decode failed") } pri, err := x509.ParsePKCS8PrivateKey(block.Bytes) if err != nil { return nil, err } priKey := pri.(*rsa.PrivateKey) plainText, err := rsa.DecryptPKCS1v15(rand.Reader, priKey, cipherText) if err != nil { return nil, err } return plainText, nil}func main() { plainText := byte("Hello World") publicKey, privateKey := GenerateRsaKeyPair(2048) cipherText, err := RsaEncrypt(plainText, publicKey) if err != nil { fmt.Println(err) return } fmt.Printf("Cipher Text: %x\n", cipherText) plainTextResult, err := RsaDecrypt(cipherText, privateKey) if err != nil { fmt.Println(err) return } fmt.Printf("Plain Text: %s\n", plainTextResult)}func GenerateRsaKeyPair(bitsize int) (byte, byte) { priKey, err := rsa.GenerateKey(rand.Reader, bitsize) if err != nil { return nil, nil } priDER := x509.MarshalPKCS1PrivateKey(priKey) priPEM := pem.EncodeToMemory(&pem.Block{ Type: "RSA PRIVATE KEY", Bytes: priDER, }) publicKey := priKey.PublicKey pubDER, err := x509.MarshalPKIXPublicKey(&publicKey) if err != nil { return nil, nil } pubPEM := pem.EncodeToMemory(&pem.Block{ Type: "PUBLIC KEY", Bytes: pubDER, }) return pubPEM, priPEM}

三、哈希算法

哈希算法將任意長度的消息壓縮到固定長度的摘要中。常用的哈希算法有SHA-1、SHA-256、MD5等。Go語言中的crypto包中也提供了多種哈希算法。

以下是一個(gè)示例,使用SHA-256算法計(jì)算字符串的哈希值:

`go

package main

import (

"crypto/sha256"

"fmt"

)

func ComputeSha256Hash(data byte) byte {

h := sha256.New()

h.Write(data)

return h.Sum(nil)

}

func main() {

data := byte("Hello World")

hash := ComputeSha256Hash(data)

fmt.Printf("SHA-256 Hash: %x\n", hash)

}

四、數(shù)字簽名數(shù)字簽名是一種基于公鑰密碼學(xué)的技術(shù),用來保證消息的完整性、真實(shí)性和不可否認(rèn)性。常用的數(shù)字簽名算法有RSA、ECC等。Go語言中的crypto包中也提供了多種數(shù)字簽名算法。以下是一個(gè)示例,使用RSA算法對(duì)數(shù)據(jù)進(jìn)行數(shù)字簽名,然后再進(jìn)行驗(yàn)證:`gopackage mainimport ( "crypto/rand" "crypto/rsa" "crypto/sha256" "crypto/x509" "encoding/pem" "fmt")func RsaSign(data byte, privateKey byte) (byte, error) { block, _ := pem.Decode(privateKey) if block == nil { return nil, fmt.Errorf("private key pem decode failed") } pri, err := x509.ParsePKCS8PrivateKey(block.Bytes) if err != nil { return nil, err } priKey := pri.(*rsa.PrivateKey) h := sha256.New() h.Write(data) hash := h.Sum(nil) signature, err := rsa.SignPKCS1v15(rand.Reader, priKey, crypto.SHA256, hash) if err != nil { return nil, err } return signature, nil}func RsaVerify(data, signature, publicKey byte) (bool, error) { block, _ := pem.Decode(publicKey) if block == nil { return false, fmt.Errorf("public key pem decode failed") } pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes) if err != nil { return false, err } pub := pubInterface.(*rsa.PublicKey) h := sha256.New() h.Write(data) hash := h.Sum(nil) err = rsa.VerifyPKCS1v15(pub, crypto.SHA256, hash, signature) if err != nil { return false, err } return true, nil}func main() { data := byte("Hello World") publicKey, privateKey := GenerateRsaKeyPair(2048) signature, err := RsaSign(data, privateKey) if err != nil { fmt.Println(err) return } fmt.Printf("Signature: %x\n", signature) ok, err := RsaVerify(data, signature, publicKey) if err != nil { fmt.Println(err) return } fmt.Printf("Verify Result: %t\n", ok)}

總結(jié):

上述代碼示例了Go語言中常用的加密算法和加密技術(shù),包括對(duì)稱加密算法、非對(duì)稱加密算法、哈希算法和數(shù)字簽名。這些技術(shù)在實(shí)際應(yīng)用中非常有用,能夠幫助開發(fā)人員保證系統(tǒng)的安全性。

新聞名稱:Go語言常用加密算法和加密技術(shù)全面解析!
鏈接地址:http://www.muchs.cn/article26/dgppecg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)站排名、外貿(mào)建站搜索引擎優(yōu)化、移動(dòng)網(wǎng)站建設(shè)、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營