gpt4 book ai didi

c# - AES解密,IV长度

转载 作者:行者123 更新时间:2023-12-02 05:42:15 25 4
gpt4 key购买 nike

我正在尝试解密一个字符串,但我得到的是

指定的初始化向量 (IV) 与该算法的 block 大小不匹配。

我已经在 SO 和网络上搜索了一段时间,我知道我的 IV 是 32 字节,应该是 16 字节,但我不知道如何实现它。要获取的字符串已使用 AES/CBC/PKCS5Padding 加密,我的代码(好吧,实际上我已经在网络的某个地方找到了它)是

var btKey = Encoding.ASCII.GetBytes("7c6e1257d0e81ff55bda80cc904365ae");
var btIV = Encoding.ASCII.GetBytes("cf5e4620455cd7190fcb53ede874f1a8");

aesAlg.Key = btKey;
aesAlg.IV = btIV;

aesAlg.Padding = PaddingMode.PKCS7;

// Create a decrytor to perform the stream transform.
var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(encodedTicketAsBytes))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)){
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plainText = srDecrypt.ReadToEnd();
}
}
}

我不明白的是 aesAlg.Padding 的使用,老实说,我找不到一个简单的,据我所知,C# 中的例子。

有什么帮助吗?

谢谢!!

最佳答案

您拥有的 key 几乎可以肯定是一堆十六进制值而不是 ascii 字符。你在做什么:

var btIV = Encoding.ASCII.GetBytes("cf5e4620455cd7190fcb53ede874f1a8");

它像对待任何其他字符串一样对待它,并将其转换为二进制 ascii 字节。那些看起来像十六进制数字对我来说。每 2 个字符是一个字节值。你可能想要像

var btIV = new byte[] {0xcf,0x5e,0x46,0x20,0x45,0x5c,0xd7,0x19,0x0f,0xcb,0x53,0xed,0xe8,0x74,0xf1,0xa8};

关于c# - AES解密,IV长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10800857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com