golang写一段 AES 256 CBC 解密代码
以下是使用golang进行AES 256 CBC解密的示例代码:
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
)
func main() {
key := []byte("12345678901234567890123456789012") // 32字节密钥
cipherText := "YmFzZTY0IGRlY29kaW5nIGlzIGVhc3QgZnJvbSBhbnl3YXk=" // 待解密的密文
decodedCipherText, _ := base64.StdEncoding.DecodeString(cipherText)
block, _ := aes.NewCipher(key)
iv := make([]byte, aes.BlockSize)
stream := cipher.NewCBCDecrypter(block, iv)
stream.CryptBlocks(decodedCipherText, decodedCipherText)
fmt.Printf("解密后的明文:%s\n", decodedCipherText)
}
注意,使用CBC模式解密需要提供初始向量(IV),这里我们使用一个全零的16字节IV。实际使用时,应当使用随机生成的IV,避免相同明文加密后产生相同密文
原文地址: https://www.cveoy.top/t/topic/flOQ 著作权归作者所有。请勿转载和采集!