C#windows窗体中的Rijndael Cipher错误

我目前有兴趣进一步提高我在加密和解密方面的编程技巧,并为Rijndael密码找到了一些例子。

我开始在c#中编写自己的程序,然后单击我的加密按钮,打开一个对话框窗口,允许我选择一个文件……到目前为止一切正常

我选择一个文件,然后逐步执行我的代码,我得到了我的文件的根路径,生成一个密钥,然后我开始将我的纯文本转换为密文…这里是我遇到问题的地方。

try { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "All Files (*.*)|"; dialog.InitialDirectory = @"Desktop"; dialog.Title = "Please select a file to encrypt."; dialog.ShowDialog(); inputFile = dialog.FileName; outputFile = inputFile; string password = @"secrets"; // key to encrypt files UnicodeEncoding UE = new UnicodeEncoding(); byte[] key = UE.GetBytes(password); string cryptFile = outputFile; FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create); RijndaelManaged RMCrypto = new RijndaelManaged(); CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write); FileStream fsIn = new FileStream(inputFile, FileMode.Append); int data; while ((data = fsIn.ReadByte()) != -1) cs.WriteByte((byte)data); fsIn.Close(); cs.Close(); fsCrypt.Close(); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } } 

当我到达这段代码时问题就出现了

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

我得到的错误是system.windows.forms.mouseeventargs有人可以帮助我吗?

编辑!!! 这是我的错误的实际错误消息。指定的初始化向量(IV)与此算法的块大小不匹配。

我得到的错误是system.windows.forms.mouseeventargs有人可以帮助我吗?

看一下catch语句。 你没有指定一个变量来保存exception,但你在里面使用e 。 由于您处于事件处理程序中,因此e已被声明为方法的参数。

你的捕获应该是这样的:

  catch (Exception ex) { // now ex holds your exception } 

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

使用GenerateIV创建有效的IV。 如果要指定自己的IV,请确保它符合算法的块大小。 在这种情况下:16 BYTE

下面是一些代码,它打开所选文件并使用随机文件名在同一目录中加密。

 private void button1_Click(object sender, EventArgs e) { try { var dialog = new OpenFileDialog { Filter = "All Files (*.*)|", InitialDirectory = @"Desktop", Title = "Please select a file to encrypt." }; dialog.ShowDialog(); string inputFile = dialog.FileName; // NOTE: The password should not be hardcoded in here const string password = @"secrets"; var fileInfo = new FileInfo(inputFile); if(fileInfo.Directory != null) { string cryptFile = Path.Combine(fileInfo.Directory.ToString(), Path.GetRandomFileName()); using (var rijndael = InitSymmetric(Rijndael.Create(), password, 256)) { using(var fsCrypt = new FileStream(cryptFile, FileMode.Create)) { using (var cs = new CryptoStream(fsCrypt, rijndael.CreateEncryptor(), CryptoStreamMode.Write)) { using (var fsIn = new FileStream(inputFile, FileMode.Open)) { int data; while ((data = fsIn.ReadByte()) != -1) { cs.WriteByte((byte)data); } } } } } } } catch(Exception ex) { MessageBox.Show(ex.ToString()); } } public static SymmetricAlgorithm InitSymmetric(SymmetricAlgorithm algorithm, string password, int keyBitLength) { // NOTE: Salt is for example purposes, would not normally have this in here. var salt = new byte[] { 1, 3, 66, 234, 73, 48, 134, 69, 250, 6 }; const int iterations = 10000; var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, salt, iterations); if (!algorithm.ValidKeySize(keyBitLength)) throw new InvalidOperationException("Invalid size key"); algorithm.Key = rfc2898DeriveBytes.GetBytes(keyBitLength / 8); algorithm.IV = rfc2898DeriveBytes.GetBytes(algorithm.BlockSize / 8); return algorithm; } 

您将密码直接输入密钥和IV。 这是错误的做法。

  1. 对于每个加密,IV应该是随机的和不同的。 使用自动生成的IV并将其与密文一起存储。
  2. 使用真实密钥,而不是密码。 或者通过PBKDF2发送密码和盐,请求16个输出字节并将其用作密钥。