golang写一段 AES-256 CBC 解密代码
以下是golang的AES-256 CBC解密代码示例:
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
)
func main() {
// 加密后的数据
ciphertext := "V2Kj0UT6o3cXz1o3WtYD0Q=="
// 密钥
key := []byte("01234567890123456789012345678901")
// 初始化向量
iv := []byte("0123456789012345")
// 解密base64编码的数据
data, err := base64.StdEncoding.DecodeString(ciphertext)
if err != nil {
panic(err)
}
// 创建解密器
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// CBC解密模式
mode := cipher.NewCBCDecrypter(block, iv)
// 解密数据
mode.CryptBlocks(data, data)
// 去除填充
data = PKCS5UnPadding(data)
// 输出解密结果
fmt.Printf("解密后的数据:%s\n", string(data))
}
// PKCS5UnPadding 去除填充
func PKCS5UnPadding(data []byte) []byte {
length := len(data)
unpadding := int(data[length-1])
return data[:(length - unpadding)]
}
在上面的示例中,我们首先将加密后的数据进行了base64解码,然后使用密钥和初始化向量创建了一个AES解密器,使用CBC模式进行解密,最后去除填充并输出解密结果
原文地址: https://www.cveoy.top/t/topic/flPq 著作权归作者所有。请勿转载和采集!