C#中的Rijndael密钥大小 [英] Rijndael key size in C#

查看:341
本文介绍了C#中的Rijndael密钥大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用C#开发一个小工具,该工具可让我快速加密文件.因此,我使用了此脚本,对我来说是完美的.但是我仍然有一个问题:密钥太短(最多8个字符).我在RijndaelManaged()文档中读到,密钥的最大大小为256位,因此我应该能够使用64个字符的密钥...(例如sha256哈希)

I'm currently developing a little tool in C# that allows me to quickly crypt my files. So I used this script which looks to be perfect for me. But I still have a problem : the key is too short (8 character max). I read in RijndaelManaged() documentation that maximum size for the key is 256 bits, so I should be able to use a 64 character key... (like sha256 hash)

但是每次我尝试增加密钥大小时,都会得到一个不错的加密失败!",即使是9个字符也是如此.我一直在Google上寻找解决方案,但没有用.

But every time I try to increase the key size, I get a nice "Encryption failed !", even for 9 characters. I've been looking for a solution on google for a while, but nothing useful.

我发现最好的是.所以我试图像这样更改填充:

The best thing I found is this. So I tried to change the padding like:

RMCrypto.Padding = PaddingMode.ISO10126;

// or
RMCrypto.Padding = PaddingMode.ANSIX923;

但是它并没有改变任何东西...

But it did not change anything...

推荐答案

Rjindael的密钥大小不是可以自由选择.它必须是128位,192位或256位.例如,它不能为9个字节或18个字节或36个字节.它必须严格是16个字节,24个字节或32个字节.

Rjindael's key size is not free to choose. It must be 128-bit, 192-bit, or 256-bit. It cannot be, say, 9 bytes or 18 bytes or 36 bytes. It must strictly be 16 bytes, 24 bytes, or 32 bytes.

此外,您应该首先适当地指定密钥大小,然后才能正确使用该类.尽管允许128位和192位密钥大小,但是,例如,您不能使用192位密钥将密钥大小指定为128位.您指定的密钥大小必须与您使用的密钥大小匹配.

Besides, you should first specify your key size suitably before you could use the class correctly. Though both 128-bit and 192-bit key size are allowed, you cannot, for instance, specify the key size to be 128-bit but using 192-bit key. The key size you specify must match the key size you use.

这是您如何执行此操作的示例:

This is an example how you do it:

您可以在RjindaelManaged.KeySize属性中指定密钥大小(不要与BlockSize混淆)

You could specify your key size (not to be confused with BlockSize) in the RjindaelManaged.KeySize property:

RMCrypto.KeySize = 256;

然后,byte []中的密钥大小应与上述密钥的大小匹配:

And then the key size in byte[] should match with the size of the key above:

byte[] key = new byte[]{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; 
RMCrypto.Key = key;

请确保使用看起来像随机噪声的密钥,以获取一定的安全性.

Be sure to use a key that looks like random noise in order to get some security.

当前您的密钥太短:

string password = @"myKey123";
byte[] key = UE.GetBytes(password);

这篇关于C#中的Rijndael密钥大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆