使用Rijndael加密 [英] Encryption using rijndael

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

问题描述

我很新的编程。我写的以密码提示用户加密文件下面的代码,但是当密码长度为8它只是工作,我可以做,以接受什么对于密码的字符数

 字符串传递= textBox2.Text.ToString()?; 
字符串密码= @+通+;
UnicodeEncoding UE =新UnicodeEncoding();
的byte []键= UE.GetBytes(密码);


的FileStream fsCrypt =新的FileStream(@C:\\users\\\\
ew,FileMode.Create);
NAME = fsCrypt.Name;
RijndaelManaged的RMCrypto =新RijndaelManaged的();

CryptoStream的CS =新的CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(键,键),
CryptoStreamMode.Write);

的FileStream FSIN =新的FileStream(文件名,FileMode.Open);

int数据;
,而((数据= fsIn.ReadByte())!= -1)
cs.WriteByte((字节)的数据);


解决方案

直接与派生密钥形式的密码 Encoding.GetBytes()将只有GetBytes会的结果()是合法的密钥长度工作。



更重要的是,它使一个非常弱的重点,尤其是当你选择了Unicode编码。在你的钥匙找到foobar字节模式是 66 00 00 6F 6F 00 62 00 61 00 72 00 。你看到所有的00个字节?



官方的方法是使用 Rfc2898DeriveBytes 类。此外,它可能不是使用密钥作为IV是个好主意,我不能完全肯定这一点。



另见的这太问题


I'm quite new in programming .I wrote the below code in order to prompt the user for a password to encrypting a file, But it just work when the length of password is 8, What can I do on order to accepting any number of characters for the password?

 string pass = textBox2.Text.ToString();
            string password = @"" + pass + ""; 
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] key = UE.GetBytes(password);


            FileStream fsCrypt = new FileStream(@"c:\\users\\new", FileMode.Create);
            name = fsCrypt.Name;
            RijndaelManaged RMCrypto = new RijndaelManaged();

            CryptoStream cs = new CryptoStream(fsCrypt,
                RMCrypto.CreateEncryptor(key, key),
                CryptoStreamMode.Write);

            FileStream fsIn = new FileStream(filename, FileMode.Open);

            int data;
            while ((data = fsIn.ReadByte()) != -1)
                cs.WriteByte((byte)data);

解决方案

Directly deriving a Key form your password with Encoding.GetBytes() will only work if the result of GetBytes() is a legal KeySize.

More important, it makes a very weak Key, especially as you opted for the Unicode encoding. The byte pattern in your key for "foobar" is 66 00 6F 00 6F 00 62 00 61 00 72 00. Do you see all the 00 bytes?

The official way is to use the Rfc2898DeriveBytes class. Also it is probably not a good idea to use the Key as IV, I'm not entirely sure about this.

Also see this SO question.

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

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