简体   繁体   中英

cannot enter wrong but same digit of password to decrpyt C#

Currently my program can encrypt and decrypt however when ever i key in wrong but same digits of password the program will hang. was wondering how to i solve it? Eg. correct password is 12345678 but i key in 12341234 which is same 8 digit but wrong key the decrpytion will hang

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

}
}

As I said in the comment I don't think the problem is going to be with the decryption but with how you process the output.

However, the problem you have is that you need to be able to identify when an incorrect key has been used. The simplest way to do this is to include a fixed known value at the beginning of whatever you are encrypting before you encrypt it. Then, when you decrypt, you can check to see if the plain text begins with the known value and then discard the known value if it does and raise an error if it doesn't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM