使用RC4加密算法,在C#中加密文件 [英] Encrypting files using RC4 encryption algorithm in C#
问题描述
我的问题是,我该如何进行加密,并使用RC4加密算法,解密C#文件?
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:
-
<一个href="http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it">What是一个NullReferenceException,如何解决呢?
<一个href="http://stackoverflow.com/questions/13075326/rc4-algorithm-unable-to-encrypt-decrypt-data-where-client-uses-javascript-and">RC4算法:无法加密/解密数据,其中客户端使用JavaScript和服务器C#
我却承认,乍看之下,这个问题就会出现像的副本这个问题,但是,它是大约7个月大,仍然有工作code直接解决问题没有答案。
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.$c$cproject.com/Articles/5068/RC-Encryption-Algorithm-C-Version
我不知道内置在Visual Studio 2013 System.Security.Cryptography库支持RC2,但我想专注于现在的问题是RC4,作为研究的一部分。我知道这是弱,是的,但我仍然使用它。无重要数据将要使用这种加密。
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用code例如,接受流作为输入。我造成了很大的混乱,因为我没有描述我的顾虑正确。我选择了一个流输入,因为,任何一种其他的输入可能会导致在处理大文件的速度降低的问题。
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.
规格:.NET框架4.5,C#,的WinForms
Specifications: NET Framework 4.5, C#, WinForms.
推荐答案
免责声明:虽然这code ++工程,它的可能的无法正确执行和/或安全的。
Disclaimer: While this code works, it might not be correctly implemented and/or secure.
下面的文件加密/解密使用 BouncyCastle的的RC4Engine一个例子:
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.
这篇关于使用RC4加密算法,在C#中加密文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!