Rijndael C#窗体中的密码错误 [英] Rijndael Cipher error in c# windows form
问题描述
我开始在c# ,我点击我的加密按钮,打开一个对话窗口,以允许我选择一个文件....所有工作正常到目前为止
我选择一个文件,并逐步我的代码,我得到我的文件的根路径,生成一个密钥,然后我开始将我的纯文本转换为密文...这里是我遇到的问题。
try
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter =所有文件(*。*)|;
dialog.InitialDirectory = @桌面;
dialog.Title =请选择要加密的文件。
dialog.ShowDialog();
inputFile = dialog.FileName;
outputFile = inputFile;
string password = @secrets; //加密文件的密钥
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数据;
while((data = fsIn.ReadByte())!= -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
当我遇到这段代码时, / p>
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(key,key),
CryptoStreamMode。写);
我得到的错误是system.windows.forms.mouseeventargs可以帮助我吗? p>
编辑!!!
这是实际的错误消息我的错误..
指定的初始化向量(IV)与此算法的块大小不匹配。
以下是一些代码,打开所选文件,并使用随机文件名在同一目录中进行加密。
private void button1_Click(object sender,EventArgs e)
{
try
{
var dialog = new OpenFileDialog
{
过滤器=所有文件(*。*)|,
InitialDirectory = @桌面,
标题=请选择要加密的文件。
};
dialog.ShowDialog();
string inputFile = dialog.FileName;
//注意:密码不应该在这里硬编码
const string password = @secrets;
var fileInfo = new FileInfo(inputFile);
if(fileInfo.Directory!= null)
{
string cryptFile = Path.Combine(fileInfo.Directory.ToString(),
Path.GetRandomFileName() );
使用(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))
{
使用(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)
{
//注意:Salt是例如在这里通常不会有这个。
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);
返回算法;
}
I am currently interested in furthering my programming skills in encryption and decryption and found a number of examples for the Rijndael cipher.
I start to program my own in c#, i click my encrypt button which opens a dialog window to allow me to choose a file....all works ok so far
I choose a file, and step through my code, i get t he root path for my file, generate a key and then i start to convert my plain text into cipher text...here's where i hit the problem.
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());
}
}
the problem hits when i reach this piece of code
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(key, key),
CryptoStreamMode.Write);
the error i get is system.windows.forms.mouseeventargs can anybody help me out there?
EDIT!!! this is the actual error message my mistake.. Specified initialization vector (IV) does not match the block size for this algorithm.
Here is some code which opens up the selected file and encrypts in in the same directory with a random file name.
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;
}
这篇关于Rijndael C#窗体中的密码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!