[英]cannot enter wrong but same digit of password to decrpyt C#
目前我的程序可以加密和解密,但是當我輸入錯誤但相同數字的密碼時,程序將掛起。 想知道我該如何解決? 例如。 正確的密碼是 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;
}
}
}
正如我在評論中所說,我認為問題不在於解密,而在於您如何處理 output。
但是,您遇到的問題是您需要能夠識別何時使用了不正確的密鑰。 最簡單的方法是在加密之前在要加密的任何內容的開頭包含一個固定的已知值。 然后,當您解密時,您可以檢查純文本是否以已知值開頭,如果是則丟棄已知值,如果不是則引發錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.