Go語(yǔ)言常用加密算法和加密技術(shù)全面解析!
在現(xiàn)代計(jì)算機(jī)領(lǐng)域,安全性非常重要,而加密技術(shù)是保證計(jì)算機(jī)信息系統(tǒng)安全性的重要措施之一。為了滿足這個(gè)需求,Go語(yǔ)言提供了豐富的加密算法和加密技術(shù),本文將為大家詳細(xì)解析Go語(yǔ)言中常用的加密算法和加密技術(shù)。
一、對(duì)稱加密算法
對(duì)稱加密算法是加密和解密使用相同的密鑰的一種加密方式。常用的對(duì)稱加密算法有DES、3DES、AES等。Go語(yǔ)言中的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語(yǔ)言中的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}
三、哈希算法
哈希算法將任意長(zhǎng)度的消息壓縮到固定長(zhǎng)度的摘要中。常用的哈希算法有SHA-1、SHA-256、MD5等。Go語(yǔ)言中的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ù),用來(lái)保證消息的完整性、真實(shí)性和不可否認(rèn)性。常用的數(shù)字簽名算法有RSA、ECC等。Go語(yǔ)言中的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語(yǔ)言中常用的加密算法和加密技術(shù),包括對(duì)稱加密算法、非對(duì)稱加密算法、哈希算法和數(shù)字簽名。這些技術(shù)在實(shí)際應(yīng)用中非常有用,能夠幫助開(kāi)發(fā)人員保證系統(tǒng)的安全性。
以上就是IT培訓(xùn)機(jī)構(gòu)千鋒教育提供的相關(guān)內(nèi)容,如果您有web前端培訓(xùn),鴻蒙開(kāi)發(fā)培訓(xùn),python培訓(xùn),linux培訓(xùn),java培訓(xùn),UI設(shè)計(jì)培訓(xùn)等需求,歡迎隨時(shí)聯(lián)系千鋒教育。