C# 文件将无法正确解密
好的,在这一点上,我已经解决了我遇到的许多其他问题。现在,在尝试解密文件时,我没有收到任何异常,但是数据没有正确解密……它在整个操作过程中没有引发异常,直到解密后我再次尝试通过组合框访问XML文档。引发此错误的时间: 出现意外的文件结尾。以下元素未关闭:帐户。第12行,位置10。 当我在web浏览器中打开文件时,它只有大约一半的XML节点。发生了什么事C# 文件将无法正确解密,c#,.net,encryption,file-io,C#,.net,Encryption,File Io,好的,在这一点上,我已经解决了我遇到的许多其他问题。现在,在尝试解密文件时,我没有收到任何异常,但是数据没有正确解密……它在整个操作过程中没有引发异常,直到解密后我再次尝试通过组合框访问XML文档。引发此错误的时间: 出现意外的文件结尾。以下元素未关闭:帐户。第12行,位置10。 当我在web浏览器中打开文件时,它只有大约一半的XML节点。发生了什么事 public partial class Form1 : Form { public string fileName = "passfi
public partial class Form1 : Form
{
public string fileName = "passfile.xml";
public DataSet ds = new DataSet("Account List");
public DataTable accounts = new DataTable("Accounts");
public Form1()
{
InitializeComponent();
accountGroupsBox.Enabled = false;
menuStrip1.Enabled = false;
button2.Enabled = false;
Types type = new Types();
this.accountGroupsBox.Items.AddRange(type.accountTypes);
accounts.Columns.AddRange(new DataColumn[] {
new DataColumn("Username"),
new DataColumn("Password"),
new DataColumn("Description")});
dataGridView1.DataSource = accounts;
}
private void addNewPasswordToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 addAccount = new Form2(this);
addAccount.Show();
}
private void S_Click(object sender, EventArgs e)
{ //Need to add some code to check for password correctness, for now just unlock the interface
accountGroupsBox.Enabled = true;
menuStrip1.Enabled = true;
button2.Enabled = true;
label2.Text = "Interface Unlocked";
}
private void accountGroupsBox_SelectedIndexChanged(object sender, EventArgs e)
{ //Display the accounts on the datagrid
accounts.Clear();
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
foreach (XmlNode node in doc.GetElementsByTagName("Account"))
{
if (node["AccountType"].InnerText == accountGroupsBox.SelectedItem.ToString())
{
DataRow row = accounts.Rows.Add(
node["Username"].InnerText,
node["Password"].InnerText,
node["Description"].InnerText);
}
}
}
public void Encrypt()
{
string temp = Path.GetTempFileName();
string path = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
byte[] pword = Encoding.UTF8.GetBytes(textBox1.Text);
byte[] hash = SHA256.Create().ComputeHash(pword);
byte[] iv = MD5.Create().ComputeHash(pword);
byte[] key = hash;
using(FileStream fsInput = new FileStream(fileName, FileMode.Open, FileAccess.Read))
using(SymmetricAlgorithm alg = Aes.Create())
using(ICryptoTransform enc = alg.CreateEncryptor(key, iv))
using (FileStream fsOutput = new FileStream(temp, FileMode.Create, FileAccess.Write))
using (CryptoStream cs = new CryptoStream(fsOutput, enc, CryptoStreamMode.Write))
{
try
{
byte[] byteInput = new byte[fsInput.Length - 1];
fsInput.Read(byteInput, 0, byteInput.Length);
cs.Write(byteInput, 0, byteInput.Length);
FileInfo old = new FileInfo(fileName);
FileInfo newer = new FileInfo(temp);
old.Delete();
newer.MoveTo(Path.Combine(path, fileName));
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Encryption Error", MessageBoxButtons.OK);
}
}
}
public void Decrypt()
{
byte[] pword = Encoding.UTF8.GetBytes(textBox1.Text);
byte[] hash = SHA256.Create().ComputeHash(pword);
byte[] iv = MD5.Create().ComputeHash(pword);
byte[] key = hash;
using(FileStream fsInput = new FileStream(fileName, FileMode.Open, FileAccess.Read))
using(SymmetricAlgorithm alg = Aes.Create())
using(ICryptoTransform enc = alg.CreateDecryptor(key, iv))
using (CryptoStream cs = new CryptoStream(fsInput, enc, CryptoStreamMode.Read))
{
StreamWriter sw = new StreamWriter(temp);
sw.Write(new StreamReader(cs).ReadToEnd());
sw.Flush();
sw.Close();
}
FileInfo encrypted = new FileInfo(fileName);
FileInfo decrypted = new FileInfo(temp);
encrypted.Delete();
decrypted.MoveTo(Path.Combine(path, fileName);
删除和移动到操作看起来是在打开的文件上执行的。此外,decrypt方法会打开“filename”文件两次。在哪一行抛出异常?调用
old.Delete()时,旧文件fileName
仍处于打开状态代码>当使用{}块结束时,它将关闭。将文件删除和重命名移动到加密方法的末尾。效果很好,谢谢。但是现在我从new.MoveTo()部分得到一个无法创建已经存在的文件的异常。如何修复此问题?@Stev0-能否将更新的代码和异常的堆栈跟踪粘贴到pastebin.com或其他地方。我不确定是什么原因造成的。下面是指向异常详细信息粘贴箱的链接:该异常来自您的accountGroupsBox\u SelectedIndexChanged()
方法。看起来与加密无关,但文件中的Xml格式不正确。我会打开这个文件并检查它是什么。是否可能是文件的结束元素?这行代码看起来可疑:新字节[fsInput.Length-1]
你为什么要从输入文件的末尾切掉一个字节?同样的,上面的答案修复了与加密相关的部分,现在我在解密上得到了相同的异常,特别是在Streamwriter sw行中,我可以更改什么来修复它?好的,为了解决这个问题,我只做了与加密方法相同的事情…将输出放入一个tempfile,然后移动重命名…@Stev0:实例化StreamWriter时的问题是您将文件名字符串传递给它,而不是文件流。编辑时有点输入错误。代码已经更改,以反映我在vS项目中的内容。它实际上传递临时文件名,并执行与加密方法类似的移动重命名操作,但文件已损坏ID您是指C#文件加密还是.NET文件加密?