golang使用aes库实现加解密操作
golang实现加密解密的库很多,这里使用的是aes库+base64库来实现.
使用时,需要指定一个私钥,来进行加解密,这里指定是:
varaeskey=[]byte(“321423u9y8d2fwfl”)
上代码:
packagemain
import(
"fmt"
"crypto/cipher"
"crypto/aes"
"bytes"
"encoding/base64"
)
funcPKCS5Padding(ciphertext[]byte,blockSizeint)[]byte{
padding:=blockSize-len(ciphertext)%blockSize
padtext:=bytes.Repeat([]byte{byte(padding)},padding)
returnappend(ciphertext,padtext...)
}
funcPKCS5UnPadding(origData[]byte)[]byte{
length:=len(origData)
unpadding:=int(origData[length-1])
returnorigData[:(length-unpadding)]
}
funcAesEncrypt(origData,key[]byte)([]byte,error){
block,err:=aes.NewCipher(key)
iferr!=nil{
returnnil,err
}
blockSize:=block.BlockSize()
origData=PKCS5Padding(origData,blockSize)
blockMode:=cipher.NewCBCEncrypter(block,key[:blockSize])
crypted:=make([]byte,len(origData))
blockMode.CryptBlocks(crypted,origData)
returncrypted,nil
}
funcAesDecrypt(crypted,key[]byte)([]byte,error){
block,err:=aes.NewCipher(key)
iferr!=nil{
returnnil,err
}
blockSize:=block.BlockSize()
blockMode:=cipher.NewCBCDecrypter(block,key[:blockSize])
origData:=make([]byte,len(crypted))
blockMode.CryptBlocks(origData,crypted)
origData=PKCS5UnPadding(origData)
returnorigData,nil
}
funcmain(){
varaeskey=[]byte("321423u9y8d2fwfl")
pass:=[]byte("vdncloud123456")
xpass,err:=AesEncrypt(pass,aeskey)
iferr!=nil{
fmt.Println(err)
return
}
pass64:=base64.StdEncoding.EncodeToString(xpass)
fmt.Printf("加密后:%v\n",pass64)
bytesPass,err:=base64.StdEncoding.DecodeString(pass64)
iferr!=nil{
fmt.Println(err)
return
}
tpass,err:=AesDecrypt(bytesPass,aeskey)
iferr!=nil{
fmt.Println(err)
return
}
fmt.Printf("解密后:%s\n",tpass)
}
输出:
加密后:rLyZug0MCEF2TBcJdhMyjg==
解密后:vdncloud123456
补充:GolangAESCBC加密
我就废话不多说了,大家还是直接看代码吧~
packagemain
import(
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
)
const(
key="2018201820182018"
iv="1234567887654321"
)
funcmain(){
str:="我勒个去"
es,_:=AesEncrypt(str,[]byte(key))
fmt.Println(es)
ds,_:=AesDecrypt(es,[]byte(key))
fmt.Println(string(ds))
}
funcAesEncrypt(encodeStrstring,key[]byte)(string,error){
encodeBytes:=[]byte(encodeStr)
//根据key生成密文
block,err:=aes.NewCipher(key)
iferr!=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)
returnbase64.StdEncoding.EncodeToString(crypted),nil
}
funcPKCS5Padding(ciphertext[]byte,blockSizeint)[]byte{
padding:=blockSize-len(ciphertext)%blockSize
//填充
padtext:=bytes.Repeat([]byte{byte(padding)},padding)
returnappend(ciphertext,padtext...)
}
funcAesDecrypt(decodeStrstring,key[]byte)([]byte,error){
//先解密base64
decodeBytes,err:=base64.StdEncoding.DecodeString(decodeStr)
iferr!=nil{
returnnil,err
}
block,err:=aes.NewCipher(key)
iferr!=nil{
returnnil,err
}
blockMode:=cipher.NewCBCDecrypter(block,[]byte(iv))
origData:=make([]byte,len(decodeBytes))
blockMode.CryptBlocks(origData,decodeBytes)
origData=PKCS5UnPadding(origData)
returnorigData,nil
}
funcPKCS5UnPadding(origData[]byte)[]byte{
length:=len(origData)
unpadding:=int(origData[length-1])
returnorigData[:(length-unpadding)]
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持毛票票。如有错误或未考虑完全的地方,望不吝赐教。
