不能输入错误但相同数字的密码来解密C#

时间:2011-08-15 07:36:08

标签: c# encryption passwords hash

目前我的程序可以加密和解密但是当我输入错误但相同数字的密码时程序将挂起。想知道如何解决它? 例如。正确的密码是12345678,但我键入12341234,这是相同的8位数但错误的键,decrpytion将挂起

     //Encrypt Method
    public bool DESEncrypt(String input, String output, String key)
    {
        bool success = false;
        try
        {
            int requiredLength = 8;


            FileStream fsInput = new FileStream(input, FileMode.Open, FileAccess.Read);

            FileStream fsEncrypted = new FileStream(output, FileMode.Create, FileAccess.Write);


            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();


            DES.Padding = PaddingMode.PKCS7;
            if (key.Length < requiredLength)
            {
                key = key.PadRight(requiredLength);
            }
            else if (key.Length > requiredLength)
            {
                key = key.Substring(0, requiredLength);
            }

            DES.Key = ASCIIEncoding.ASCII.GetBytes(key);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(key);



            ICryptoTransform desEncrypt = DES.CreateEncryptor();
            CryptoStream cryptoStream = new CryptoStream(fsEncrypted, desEncrypt, CryptoStreamMode.Write);

            byte[] byteInput = new byte[fsInput.Length];

            fsInput.Read(byteInput, 0, byteInput.Length);


            cryptoStream.Write(byteInput, 0, byteInput.Length);

            cryptoStream.Flush();
            cryptoStream.Close();
            fsInput.Close();
            fsEncrypted.Close();

            success = true;
            MessageBox.Show("Lock Success!");

        }
        catch (Exception ex)
        {
            success = false;
            MessageBox.Show("Encryption Unsuccessful!" + ex);
            //To Be Continue.....
            //File being processed error
            //try .Dispose()?
        }
        return success;
    }


     //Decrypt method
    public bool Decrypt(String input, String output, String key)
    {
        bool success = false;
        try
        {

            int requiredLength = 8;
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Padding = PaddingMode.PKCS7;
            if (key.Length < requiredLength)
            {
                key = key.PadRight(requiredLength);
            }
            else if (key.Length > requiredLength)
            {
                key = key.Substring(0, requiredLength);
            }

            //Set secret key For DES algorithm.
            DES.Key = ASCIIEncoding.ASCII.GetBytes(key);
            //Set initialization vector.
            DES.IV = ASCIIEncoding.ASCII.GetBytes(key);

            //Create a file stream to read the encrypted file back.
            FileStream fsInput = new FileStream(input, FileMode.Open, FileAccess.Read);
            //Create a DES decryptor from the DES instance.
            ICryptoTransform desDecrypt = DES.CreateDecryptor();
            //Create crypto stream set to read and do a 
            //DES decryption transform on incoming bytes.
            CryptoStream cryptostreamDecr = new CryptoStream(fsInput, desDecrypt, CryptoStreamMode.Read);
            //Print the contents of the decrypted file.

            BinaryWriter bw = new BinaryWriter(new FileStream(output, FileMode.Create, FileAccess.Write));
            byte[] buffer = new byte[500];
            int bytesRead = -1;
            while (true)
            {
                bytesRead = cryptostreamDecr.Read(buffer, 0, 500);
                if (bytesRead == 0)
                {
                    break;
                }
                bw.Write(buffer, 0, bytesRead);
            }


            //StreamWriter fsDecrypted = new StreamWriter(output);
            //fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
            bw.Flush();
            fsInput.Close();
            cryptostreamDecr.Close();
            bw.Close();

            success = true;
            MessageBox.Show("Unlock Success!");
        }
        catch (Exception ex)
        {
            success = false;
            MessageBox.Show("Decryption Unsuccessful!" + ex);
            //Try memory stream
        }

        return success;
    }

}
}

1 个答案:

答案 0 :(得分:0)

正如我在评论中所说,我认为问题不在于解密,而在于你如何处理输出。

但是,您遇到的问题是您需要能够识别何时使用了错误的密钥。最简单的方法是在加密之前在加密的开头包含一个固定的已知值。然后,当您解密时,您可以检查纯文本是否以已知值开头,如果有,则丢弃已知值,如果不是,则抛出错误。