Это фрагмент кода, который у меня есть для установки главного пароля:
private void button1_Click(object sender, EventArgs e)
{
string current = textBox1.Text;
string newPass = textBox2.Text;
string confirmed = textBox3.Text;
string massPass = "winxp.pma";
if (File.Exists(massPass))
{
byte[] cipertext = File.ReadAllBytes(massPass);
string decoded;
if(Encryptor.TryDecrypt(current, cipertext, out decoded))
{
FileStream fs = new FileStream(massPass, FileMode.Truncate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
if(newPass == confirmed)
{
byte[] newCipher = Encryptor.Encrypt(newPass, newPass);
string writeIt = System.Text.Encoding.UTF8.GetString(newCipher);
sw.Write(writeIt);
sw.Flush();
sw.Close();
fs.Close();
this.Close();
}
else
{
MessageBox.Show("New password do not match.", "Error", MessageBoxButtons.OK);
}
}
}
else
{
FileStream fs = new FileStream(massPass, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
if (newPass == confirmed)
{
byte[] ciphertext = Encryptor.Encrypt(newPass, newPass);
string writeIt = System.Text.Encoding.UTF8.GetString(ciphertext);
sw.Write(ciphertext);
sw.Flush();
sw.Close();
fs.Close();
this.Close();
}
Вернувшись к главной форме, я использую метод TryDecrypt следующим образом:
private void S_Click(object sender, EventArgs e)
{
byte[] ciphertext = File.ReadAllBytes(massPass);
string decoded;
if (Encryptor.TryDecrypt(textBox1.Text, ciphertext, out decoded))
{
accountGroupsBox.Enabled = true;
addNewPasswordToolStripMenuItem.Enabled = true;
label2.Text = "Interface Unlocked";
}
else
{
MessageBox.Show("Incorrect Master Password.", "Authentication Error", MessageBoxButtons.OK);
}
Однако, как я уже отметил, он не вернет истину .... Я держу пари, что это связано с тем, как я обрабатываю FileStreams в другой форме, но я недостаточно понимаю, что происходит под капотом. чтобы определить, правильно ли я это делаю.