简体   繁体   English

指定的初始化向量(IV)与使用AES的c#中的块大小不匹配

[英]Specified initialization vector (IV) does not match the block size in c# using AES

I'm starting with a provided example in Ruby: 我从Ruby中提供的示例开始:

cipher = OpenSSL::Cipher::AES.new(128, :CBC)
cipher.encrypt
cipher.key = "wB\x14=\r\xC3\xC1\x84$\x10\xCE\xC0\x10\x03\xFE\x18"
cipher.iv = "\xD8a\"\xFAs\xBD\xE4\xF9\xA4\xA1\x1E\xA5l\xA6@\xFD"

And trying to replicate in c#: 并尝试在C#中复制:

string AesKey = "wB\x14=\r\xC3\xC1\x84$\x10\xCE\xC0\x10\x03\xFE\x18";
string AesIV = "\xD8a\"\xFAs\xBD\xE4\xF9\xA4\xA1\x1E\xA5l\xA6@\xFD";

AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.BlockSize = 128;
aes.KeySize = 128;
aes.IV = Encoding.UTF8.GetBytes(AesIV);
aes.Key = Encoding.UTF8.GetBytes(AesKey);
aes.Mode = CipherMode.CBC;

byte[] src = Encoding.Unicode.GetBytes(text);
using (ICryptoTransform encrypt = aes.CreateEncryptor())
  {
      byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);
      string EncryptedResult = Convert.ToBase64String(dest);
      EncryptedValue.Text = EncryptedResult;
  }

I'm getting the error: 我收到错误消息:

"Specified initialization vector (IV) does not match the block size for this algorithm." “指定的初始化向量(IV)与该算法的块大小不匹配。”

Am I misunderstanding something about the format of the original key and iv values that I am failing to account for? 我是否会误解我无法解释的原始键和iv值的格式?

Ruby and C# strings are different, so your C# IV winds up with extra bytes in it. Ruby和C#字符串不同,因此C#IV结束时会有额外的字节。

In Ruby, a string is really just a byte sequence. 在Ruby中,字符串实际上只是一个字节序列。 Each \\x is followed by two hexadecimal digits. 每个\\x后跟两个十六进制数字。 So, \\xD8a is really a textual representation of the bytes 0xD8 0x61. 因此, \\xD8a实际上是字节0xD8 0x61的文本表示形式。 No character encoding is necessary when this "string" is used as an initialization vector; 当将此“字符串”用作初始化向量时,无需字符编码; it's already a byte sequence masquerading as text. 它已经是伪装成文本的字节序列。

In C#, a string is a sequence of characters. 在C#中,字符串是字符序列 To convert a string to a byte array, a character-encoding is used—you've chosen UTF-8 in this case. 要将字符串转换为字节数组,需要使用字符编码-在这种情况下,您选择了UTF-8。 In order to represent millions of different characters, in C# each \\x escape is followed by 1 to 4 hexadecimal digits. 为了表示数百万个不同的字符,在C#中,每个\\x转义后跟1至4个十六进制数字。 So, for example, the substring \\xD8a doesn't represent two bytes, but the single character (U+0D8A), and when you encode it with UTF-8, it translates to the 3-byte sequence 0xE0 0xB6 0x8A instead of the two-byte 0xD8 0x61 Ruby-equivalent. 因此,例如,子字符串\\xD8a不代表两个字节,而是单个字符 (U + 0D8A),当您使用UTF-8对其进行编码时,它转换为3个字节的序列0xE0 0xB6 0x8A而不是两字节0xD8 0x61相当于Ruby。

You can base-64–encode the IV if you need a printable form. 如果需要可打印的表格,可以对IV进行64位编码。 Or you can write it in C# as a byte array literal: 或者,您可以将其作为字节数组文字以C#形式编写:

aes.IV = new byte[] { 0xD8, 0x61, ... };

However, the point is probably moot, since a real application requires security, and hard-coded IVs are not secure. 但是,这可能是没有意义的,因为实际的应用程序需要安全性,而硬编码的IV也不安全。

Make sure you are using the same padding technique, such as PKCS5 padding or just pure zero's. 确保使用相同的填充技术,例如PKCS5填充或仅使用纯零。 I had an issue with this a few weeks ago in php/java/c#, and it was the padding that was causing the issue. 几周前我在php / java / c#中遇到了一个问题,正是填充导致了问题。

The data is stored in blocks, and the last block typically won't be the exact size of the others, so it has to padd the end of it to make it the correct block size. 数据存储在块中,最后一个块通常不会是其他块的确切大小,因此必须填充其末尾以使其成为正确的块大小。 In C# you can easily set it, but in ruby you may have to write the code yourself to append the padding. 在C#中,您可以轻松地进行设置,但是在ruby中,您可能必须自己编写代码以附加填充。

Edit: It looks like Ruby may use PKCS5 by default as it seems to be here , so just set the padding in C# and see what happens. 编辑:看起来Ruby可能默认使用PKCS5,因为它似乎在这里 ,因此只需在C#中设置填充并查看会发生什么。

aes.Padding = PaddingMode.PKCS5;

Also try base64 encoding the data before you hand it to the algorithm, and then base64 decoding the data you get from the decrypt method. 在将数据交给算法之前,也请尝试对数据进行base64编码,然后对从DEcrypt方法获得的数据进行base64解码。 I had to do this as well, otherwise the data from the decrypt would sometimes be corrupted. 我也必须这样做,否则解密中的数据有时会被破坏。

I also would recommend using a 256 bit key since it is more secure, and just as easy to use as a 128 bit key. 我还建议使用256位密钥,因为它更安全,并且与128位密钥一样容易使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 指定的初始化向量(IV)与此算法的块大小不匹配 - Specified initialization vector(IV) does not match the block size for this algorithm 指定的初始化向量 (IV) 与此算法的块大小不匹配 - Specified initialization vector (IV) does not match the block size for this algorithm 指定的初始化向量 (IV) 与此算法的块大小不匹配 - Specified initialization vector (IV) does not match the block size for this algorithm 指定的初始化向量(IV)与该算法的块大小不匹配 - The specified initialization vector (IV) does not match the block size for this algorithm 使用CryptoStream的“指定的初始化向量(IV)与该算法的块大小不匹配” - “Specified initialization vector (IV) does not match the block size for this algorithm” using an CryptoStream 连接 WCF Cryptography.CryptographicException:指定的初始化向量 (IV) 与此算法的块大小不匹配 - Connect WCF Cryptography.CryptographicException: Specified initialization vector (IV) does not match the block size for this algorithm C# 中 RijndaelManaged 的​​ IV 块大小问题 - IV Block Size Issue with RijndaelManaged in C# C#Rijndael IV大小与块大小不匹配,但它应该 - C# Rijndael IV size doesn't match block size, while it should C#无法生成初始化向量IV - C# Can't generate initialization vector IV 解密 C# 中的 AES,其中使用 OpenSSL -nosalt 加密文件; AES 期望大小为 16 字节的数组 IV? - Decrypting AES in C# where the file was encryped using OpenSSL -nosalt; the AES is expecting a size 16 byte array IV?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM