您现在的位置是:网站首页> 编程资料编程资料
golang使用aes库实现加解密操作_Golang_
2023-05-26
387人已围观
简介 golang使用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)] } 以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
相关内容
- 使用go操作redis的有序集合(zset)_Golang_
- Golang 1.16 中 Modules的主要变化更新_Golang_
- Golang中的自定义类型之间的转换的实现(type conversion)_Golang_
- golang 中string和int类型相互转换_Golang_
- Golang 之区分类型别名与类型定义的方法_Golang_
- Golang使用WebSocket通信的实现_Golang_
- golang 字符串比较是否相等的方法示例_Golang_
- golang的强制类型转换实现_Golang_
- 详解go语言中type关键词的几种使用_Golang_
- golang官方嵌入文件到可执行程序的示例详解_Golang_
