C#中使用RC4加密算法加密文件
Encrypting files using RC4 encryption algorithm in C#
我的问题是,如何在C#中使用RC4加密算法加密和解密文件?
这不是这些问题的重复:
What is a NullReferenceException, and how do I fix it?
RC4 Algorithm: Unable to Encrypt / Decrypt data where client uses Javascript and Server c#
RC4 128 bit encryption in C#
但是我确实承认,乍一看,这个问题看起来像是 this question 的副本,但是,它已经有 7 个月大了,仍然没有直接解决问题的工作代码的答案.
不过,我参考了以下链接,但其中 none 完全回答了问题,或者实际上根本没有回答问题。
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。
免责声明:虽然此代码有效,但可能未正确实施and/or安全。
下面是文件 encryption/decryption 使用 BouncyCastle 的 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);
}
}
生成的文件已使用 this website 进行了测试,它似乎按预期工作。
我的问题是,如何在C#中使用RC4加密算法加密和解密文件?
这不是这些问题的重复:
What is a NullReferenceException, and how do I fix it?
RC4 Algorithm: Unable to Encrypt / Decrypt data where client uses Javascript and Server c#
RC4 128 bit encryption in C#
但是我确实承认,乍一看,这个问题看起来像是 this question 的副本,但是,它已经有 7 个月大了,仍然没有直接解决问题的工作代码的答案.
不过,我参考了以下链接,但其中 none 完全回答了问题,或者实际上根本没有回答问题。
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。
免责声明:虽然此代码有效,但可能未正确实施and/or安全。
下面是文件 encryption/decryption 使用 BouncyCastle 的 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);
}
}
生成的文件已使用 this website 进行了测试,它似乎按预期工作。