您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)如何在golang中利用aes庫進(jìn)行加解密操作,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
golang實現(xiàn)加密解密的庫很多,這里使用的是aes庫+base64庫來實現(xiàn).
使用時,需要指定一個私鑰,來進(jìn)行加解密,這里指定是:
var aeskey = []byte(“321423u9y8d2fwfl”)
上代碼:
package main import ( "fmt" "crypto/cipher" "crypto/aes" "bytes" "encoding/base64" ) 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(origData []byte) []byte { length := len(origData) unpadding := int(origData[length-1]) return origData[:(length - unpadding)] } func AesEncrypt(origData, key []byte) ([]byte, error) { block, err := aes.NewCipher(key) if err != nil { return nil, err } blockSize := block.BlockSize() origData = PKCS5Padding(origData, blockSize) blockMode := cipher.NewCBCEncrypter(block, key[:blockSize]) crypted := make([]byte, len(origData)) blockMode.CryptBlocks(crypted, origData) return crypted, nil } func AesDecrypt(crypted, key []byte) ([]byte, error) { block, err := aes.NewCipher(key) if err != nil { return nil, err } blockSize := block.BlockSize() blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) origData := make([]byte, len(crypted)) blockMode.CryptBlocks(origData, crypted) origData = PKCS5UnPadding(origData) return origData, nil } func main() { var aeskey = []byte("321423u9y8d2fwfl") pass := []byte("vdncloud123456") xpass, err := AesEncrypt(pass, aeskey) if err != nil { fmt.Println(err) return } pass64 := base64.StdEncoding.EncodeToString(xpass) fmt.Printf("加密后:%v\n",pass64) bytesPass, err := base64.StdEncoding.DecodeString(pass64) if err != nil { fmt.Println(err) return } tpass, err := AesDecrypt(bytesPass, aeskey) if err != nil { fmt.Println(err) return } fmt.Printf("解密后:%s\n", tpass) }
輸出:
加密后:rLyZug0MCEF2TBcJdhMyjg==
解密后:vdncloud123456
補充:Golang AES CBC 加密
我就廢話不多說了,大家還是直接看代碼吧~
package main import ( "bytes" "crypto/aes" "crypto/cipher" "encoding/base64" "fmt" ) const ( key = "2018201820182018" iv = "1234567887654321" ) func main() { str := "我勒個去" es, _ := AesEncrypt(str, []byte(key)) fmt.Println(es) ds, _ := AesDecrypt(es, []byte(key)) fmt.Println(string(ds)) } func AesEncrypt(encodeStr string, key []byte) (string, error) { encodeBytes := []byte(encodeStr) //根據(jù)key 生成密文 block, err := aes.NewCipher(key) if err != nil { return "", err } blockSize := block.BlockSize() encodeBytes = PKCS5Padding(encodeBytes, blockSize) blockMode := cipher.NewCBCEncrypter(block, []byte(iv)) crypted := make([]byte, len(encodeBytes)) blockMode.CryptBlocks(crypted, encodeBytes) return base64.StdEncoding.EncodeToString(crypted), 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 AesDecrypt(decodeStr string, key []byte) ([]byte, error) { //先解密base64 decodeBytes, err := base64.StdEncoding.DecodeString(decodeStr) if err != nil { return nil, err } block, err := aes.NewCipher(key) if err != nil { return nil, err } blockMode := cipher.NewCBCDecrypter(block, []byte(iv)) origData := make([]byte, len(decodeBytes)) blockMode.CryptBlocks(origData, decodeBytes) origData = PKCS5UnPadding(origData) return origData, nil } func PKCS5UnPadding(origData []byte) []byte { length := len(origData) unpadding := int(origData[length-1]) return origData[:(length - unpadding)] }
看完上述內(nèi)容,你們對如何在golang中利用aes庫進(jìn)行加解密操作有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。