golang写一段 AES 192 CBC 解密代码
以下是使用Go语言实现AES 192 CBC解密的代码:
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
)
func main() {
// 待解密的密文
ciphertext := "aWj9BhXKlgJwM+LzR1Bb6V6fWvIh1GFU8h7iS5Mn5l4="
// 用于解密的密钥
key := []byte("0123456789abcdef01234567")
// 用于解密的初始化向量
iv := []byte("0123456789abcdef")
// 对密钥进行补位,使其符合AES-192的要求
if len(key) < 24 {
key = append(key, make([]byte, 24-len(key))...)
}
if len(key) > 24 {
key = key[:24]
}
// 对密文进行base64解码
ciphertextBytes, err := base64.StdEncoding.DecodeString(ciphertext)
if err != nil {
panic(err)
}
// 创建解密器
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
mode := cipher.NewCBCDecrypter(block, iv)
// 解密密文
plaintext := make([]byte, len(ciphertextBytes))
mode.CryptBlocks(plaintext, ciphertextBytes)
// 去除解密后的明文中的尾部填充
padding := int(plaintext[len(plaintext)-1])
plaintext = plaintext[:len(plaintext)-padding]
// 输出解密后的明文
fmt.Println(string(plaintext))
}
在上面的代码中,我们首先定义了待解密的密文、用于解密的密钥和初始化向量。然后,我们对密钥进行了补位,使其符合AES-192的要求。接着,我们对密文进行了base64解码,并使用创建解密器。最后,我们解密密文,并去除解密后的明文中的尾部填充。最终,我们输出了解密后的明文
原文地址: https://www.cveoy.top/t/topic/flPa 著作权归作者所有。请勿转载和采集!