golang使用aes库实现加解密操作

【字号: 日期:2022-08-25浏览:56作者:雯心

golang实现加密解密的库很多,这里使用的是aes库+base64库来实现.

使用时,需要指定一个私钥,来进行加解密,这里指定是:

var aeskey = []byte(“321423u9y8d2fwfl”)

上代码:

package mainimport ( '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('加密后:%vn',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('解密后:%sn', tpass)}

输出:

加密后:rLyZug0MCEF2TBcJdhMyjg==

解密后:vdncloud123456

补充:Golang AES CBC 加密

我就废话不多说了,大家还是直接看代码吧~

package mainimport ( '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) //根据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)]}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持乐呵呵网。如有错误或未考虑完全的地方,望不吝赐教。

标签: Golang
相关文章: