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

本文关键字:密码 解密 数字 输入 错误 不能 | 更新日期: 2023-09-27 18:05:49

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

     //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;
    }
}
}

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

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

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