Im using this code to decrypt AES data:
public static String decrypt(String ciphertext, String password)
{
byte[] cipherdata = System.Convert.FromBase64String(ciphertext);
byte[] iv = new byte[AESBlockSize * 4];
Array.Copy(cipherdata, 0, iv, 0, iv.Length);
byte[] input = new byte[cipherdata.Length - iv.Length];
Array.Copy(cipherdata, iv.Length, input, 0, input.Length);
Rfc2898DeriveBytes passwordDB = new Rfc2898DeriveBytes(password, iv, PBKDF2Iterations);
byte[] keyBytes = passwordDB.GetBytes(256 / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.ISO10126;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, iv);
MemoryStream memoryStream = new MemoryStream(input);
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[input.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
I get the exception Padding is invalid and cannot be removed
on the call to cryptoStream.Read
when the wrong password is supplied.
Is there any way to prevent this exception when using the wrong password, because I'm trying to make a brute-forcer, and the exceptions are very slow to handle.