aes库如何在golang中使用?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
golang实现加密解密的库很多,这里使用的是aes库+base64库来实现.
使用时,需要指定一个私钥,来进行加解密,这里指定是:
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) //根据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)] }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。