This is a segment of code I have for setting the masterpassword:
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();
}
Back on the main form, I'm using the TryDecrypt method in the following manner:
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);
}
However, as I noted, it will not return true....I'm betting it something to do with the way I'm handling the FileStreams on the other form, but I dont understand enough whats happening under the hood to determine if I'm doing it correctly.