【发布时间】:2015-12-15 20:59:24
【问题描述】:
我在http://www.codeproject.com/Articles/26085/File-Encryption-and-Decryption-in-C 上找到了这个脚本。当我使用静态键// string password = @"myKey1234"; // Your Key Here 时它工作正常。当我传入不同的密钥时,它不起作用string password = @keyPwd;。您可以在我的代码中看到我正在传递密钥以使其不起作用。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CSVEncrypts
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string inputFile = "";
string outputFilePath = "";
string oFilePathName = "";
// EncryptFile
private void EncryptFile(string inputFile, string outputFile,string keyPwd )
{
try
{
// string password = @"myKey123"; // Your Key Here
string password = @keyPwd;
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.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
catch
{
MessageBox.Show("Encryption failed!", "Error");
}
}
// Decrypt
private void DecryptFile(string inputFile, string outputFile, string keyPwd)
{
{
//string password = @"myKey123"; // Your Key Here
string password = @keyPwd; // Your Key Here
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateDecryptor(key, key),CryptoStreamMode.Read);
FileStream fsOut = new FileStream(outputFile, FileMode.Create);
int data;
while ((data = cs.ReadByte()) != -1)
fsOut.WriteByte((byte)data);
fsOut.Close();
cs.Close();
fsCrypt.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (inputFile != "")
{
oFilePathName = outputFilePath + "\\" + textBox1.Text;
EncryptFile(inputFile, oFilePathName,keytextBox.Text);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (inputFile != "") ;
{
oFilePathName = outputFilePath + "\\" + textBox1.Text;
DecryptFile(inputFile, oFilePathName, keytextBox.Text);
}
}
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog InputOpenFileDialog1 = new OpenFileDialog();
if (InputOpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string strInfilename = InputOpenFileDialog1.FileName;
button3.Text = strInfilename;
inputFile = strInfilename;
outputFilePath = Path.GetDirectoryName(inputFile);
}
}
}
}
【问题讨论】:
-
对称加密算法具有预定义的密钥大小。尝试传入 8 个字符的字符串(64 位密钥),因为 myKey123 的长度为 8 个字符
-
Unicode 编码在这种情况下也很危险。如果您的密钥中有任何非 ASCII 字符,它将呈现为多个字节,这将导致另一个错误
-
另外,有效密钥大小不限于64位,可以看details here
标签: c# encryption cryptography rijndael rijndaelmanaged