C# 不能输入错误但相同的密码数字来解密C
目前,我的程序可以加密和解密,但当我输入错误但相同数字的密码时,程序将挂起。我想知道怎么解决它? 正确的密码是12345678,但我输入了12341234,这是相同的8位数字,但输入了错误的密钥。密码将挂起C# 不能输入错误但相同的密码数字来解密C,c#,encryption,passwords,hash,C#,Encryption,Passwords,Hash,目前,我的程序可以加密和解密,但当我输入错误但相同数字的密码时,程序将挂起。我想知道怎么解决它? 正确的密码是12345678,但我输入了12341234,这是相同的8位数字,但输入了错误的密钥。密码将挂起 //Encrypt Method public bool DESEncrypt(String input, String output, String key) { bool success = false; try
//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;
}
}
}
正如我在评论中所说,我认为问题不在于解密,而在于如何处理输出
但是,您的问题是,您需要能够识别何时使用了错误的密钥。最简单的方法是在加密之前,在加密内容的开头包含一个固定的已知值。然后,解密时,可以检查纯文本是否以已知值开头,如果以已知值开头,则放弃该已知值,如果不以已知值开头,则引发错误。是否可以运行测试,输入无效密钥,然后在Visual Studio调试会话中按暂停按钮?然后检查脚本阻止调用的stacktrace。是否确定是解密挂起,使用错误的密钥解密数据将作为一个数学过程正常工作,但输出将是乱七八糟的。我希望在使用输出时会出现问题