在C#中使用RC4加密算法加密文件 [英] Encrypting files using RC4 encryption algorithm in C#
问题描述
这不是这些问题的重复:
一瞥,这个问题将会像这个问题的一个副本,但是,它大约7个月大,而且还没有直接解决问题的工作代码的答案。
然而,我提到了以下链接,但是他们都没有回答完全问题,实际上完全没有问题。
-
http://www.codeproject.com/Articles/5719/Simple-encrypting-and-decrypting-data-in -C
-
http://www.codeproject.com/Articles/5068/RC-Encryption-Algorithm-C-Version
我知道Visual Studio 2013中内置的System.Security.Cryptography库支持RC2,但现在我要关注的是RC4,作为一项研究。我知道这是弱的是的,但我仍在使用它。没有重要的数据将要使用此加密。
最好使用代码示例,它接受一个流作为输入。我引起了很大的混乱,因为我没有正确地描述我的疑虑。我选择了一个流输入,由于担心任何其他输入可能会导致处理大文件的速度降低。
规格:.NET Framework 4.5 ,C#,WinForms。
免责声明: / em>不能正确实现和/或安全。
以下是使用 BouncyCastle 的RC4Engine:
//加密/解密键作为字节数组
var key = Encoding.UTF8.GetBytes(secretpassword);
var cipher = new RC4Engine();
var keyParam = new KeyParameter(key);
//用于解密文件只需将第一个参数转换为false
cipher.Init(true,keyParam);
使用(var inputFile = new FileStream(@C:\path\to\your\input.file,FileMode.Open,FileAccess.Read))
使用(var outputFile = new FileStream(@C:\path\to\your\output.file,FileMode.OpenOrCreate,FileAccess.Write))
{
//处理文件4KB一次。
byte [] buffer = new byte [1024 * 4];
long totalBytesRead = 0;
long totalBytesToRead = inputFile.Length;
while(totalBytesToRead> 0)
{
//确保您的方法被标记为async
int read = await inputFile.ReadAsync(buffer,0,buffer.Length );
//如果我们没有读任何东西(EOF),则断开循环
if(read == 0)
{
break;
}
totalBytesRead + = read;
totalBytesToRead - = read;
byte [] outBuffer = new byte [1024 * 4];
cipher.ProcessBytes(buffer,0,read,outBuffer,0);
await outputFile.WriteAsync(outBuffer,0,read);
}
}
生成的文件使用本网站,似乎按预期工作。
My question is, how do I encrypt and decrypt a file in C# using the RC4 encryption algorithm?
This is not a duplicate of these questions:
I do however acknowledge that at first glance, this question will appear like a duplicate of this question, however, it is around 7 months old, and still has no answer with working code that solves the question directly.
I have however referred to the below links, but none of them answers the question fully, or in fact, at all.
http://www.codeproject.com/Articles/5719/Simple-encrypting-and-decrypting-data-in-C
http://www.codeproject.com/Articles/5068/RC-Encryption-Algorithm-C-Version
I do know that the built-in System.Security.Cryptography library in Visual Studio 2013 supports RC2, but what I want to focus on right now is RC4, as part of a research. I know it is weak yes, but I'm still using it. No important data is going to be using this encryption.
Preferably with a code example, that accepts a stream as an input. I have caused great confusion, as I did not describe my concerns properly. I am opting for a stream input, due to the concern that any kind of other input may cause a decrease in the speed of processing large files.
Specifications: NET Framework 4.5, C#, WinForms.
Disclaimer: While this code works, it might not be correctly implemented and/or secure.
Here's an example of file encryption/decryption using the BouncyCastle's RC4Engine:
// You encryption/decryption key as a bytes array
var key = Encoding.UTF8.GetBytes("secretpassword");
var cipher = new RC4Engine();
var keyParam = new KeyParameter(key);
// for decrypting the file just switch the first param here to false
cipher.Init(true, keyParam);
using (var inputFile = new FileStream(@"C:\path\to\your\input.file", FileMode.Open, FileAccess.Read))
using (var outputFile = new FileStream(@"C:\path\to\your\output.file", FileMode.OpenOrCreate, FileAccess.Write))
{
// processing the file 4KB at a time.
byte[] buffer = new byte[1024 * 4];
long totalBytesRead = 0;
long totalBytesToRead = inputFile.Length;
while (totalBytesToRead > 0)
{
// make sure that your method is marked as async
int read = await inputFile.ReadAsync(buffer, 0, buffer.Length);
// break the loop if we didn't read anything (EOF)
if (read == 0)
{
break;
}
totalBytesRead += read;
totalBytesToRead -= read;
byte[] outBuffer = new byte[1024 * 4];
cipher.ProcessBytes(buffer, 0, read, outBuffer,0);
await outputFile.WriteAsync(outBuffer,0,read);
}
}
The resulting file was tested using this website and it appears to be working as expected.
这篇关于在C#中使用RC4加密算法加密文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!