蝙蝠岛资源网 Design By www.hbtsch.com

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)]
}

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

标签:
golang,aes库加解密

蝙蝠岛资源网 Design By www.hbtsch.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
蝙蝠岛资源网 Design By www.hbtsch.com

评论“golang使用aes库实现加解密操作”

暂无golang使用aes库实现加解密操作的评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。