golang写一段 AES-256 CBC 解密代码 IV为DMEKGJWRNRADJWTGFAWWAR
以下是使用Golang实现AES-256 CBC解密的示例代码:
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
)
func main() {
key := []byte("0123456789abcdef0123456789abcdef") // 密钥长度必须是 16、24 或 32 字节
iv := []byte("DMEKGJWRNRADJWTGFAWWAR") // IV长度必须等于块长度
// 待解密的密文
ciphertext := "q3KPWf6Pv6q8UJNclnHnAgiRrWqKg4bT6Xwva3FbKJQ="
// 对密文进行解码
ciphertextBytes, _ := base64.StdEncoding.DecodeString(ciphertext)
// 创建一个 AES 实例
block, _ := aes.NewCipher(key)
// 创建 CBC 分组模式
mode := cipher.NewCBCDecrypter(block, iv)
// 解密密文
plaintext := make([]byte, len(ciphertextBytes))
mode.CryptBlocks(plaintext, ciphertextBytes)
// 移除填充
plaintext = PKCS7UnPadding(plaintext)
// 输出解密后的明文
fmt.Println(string(plaintext))
}
// 去除 PKCS7 填充
func PKCS7UnPadding(data []byte) []byte {
length := len(data)
unpadding := int(data[length-1])
return data[:(length - unpadding)]
}
注意:在实际使用中,应该尽可能避免将密钥和 IV 等关键信息硬编码在代码中,而是应该使用安全的方式存储和传输
原文地址: https://www.cveoy.top/t/topic/flP1 著作权归作者所有。请勿转载和采集!