开发者

Cryptographic Exception: Input is not a complete block

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);
          开发者_StackOverflow  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.


Your input stream isn't complete. To be able to attempt to decrypt it, it must be a certain size. Ensure that your encryption process is correct. The encrypted data should be equal to or longer than your plain data.

[edit]
My conclusion back on the other site was that the CryptoStream did not have a chance to finish writing the data before your output file was closed. The output stream should remain open before the CryptoStream is disposed to be able to write the rest of the ciphertext and necessary padding.

My test code:

public static byte[] Encrypt(string password, string plaintext, SymmetricAlgorithm algorithm)
{
    byte[] key, iv;
    CreateKeyIV(password, out key, out iv);
    using (MemoryStream encrypted = new MemoryStream())
    {
        using (CryptoStream enc = new CryptoStream(encrypted, algorithm.CreateEncryptor(key, iv), CryptoStreamMode.Write))
        using (StreamWriter writer = new StreamWriter(enc))
            writer.Write(plaintext);
        return encrypted.ToArray();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜