Paul Rajs

Paul Rajs

  • NA
  • 641
  • 146.9k

Length of the data to decrypt is invalid - Solve

May 15 2021 10:31 AM
Hi Developers,
I have created a new windows form application for Decrypt a string with password. Its working fine.
But sometime if the length is too it throws the following error.
"Length of the data to decrypt is invalid"
AES password : 8468A97B4373415365537265744B6579746869834973215365637261745B6579
public static byte[] AES_DECRYPT(string text, string hexPassword)
{
byte[] originalBytes = HexStringToByte(text);
byte[] passwordBytes = HexStringToByte(hexPassword);
byte[] encryptedBytes = null;
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.Padding = PaddingMode.Zeros;
AES.Key = passwordBytes;
AES.Mode = CipherMode.ECB;
using (CryptoStream cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(originalBytes, 0, originalBytes.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return encryptedBytes;
}
for text
private static byte[] HexStringToByte(string hexString)
{
try
{
int bytesCount = (hexString.Length) / 2;
byte[] bytes = new byte[bytesCount];
for (int x = 0; x < bytesCount; ++x)
{
bytes[x] = Convert.ToByte(hexString.Substring(x * 2, 2), 16);
}
return bytes;
}
catch
{
throw;
}
}
for password
private static byte[] HexStringToByte(string hexString)
{
try
{
int bytesCount = (hexString.Length) / 2;
byte[] bytes = new byte[bytesCount];
for (int x = 0; x < bytesCount; ++x)
{
bytes[x] = Convert.ToByte(hexString.Substring(x * 2, 2), 16);
}
return bytes;
}
catch
{
throw;
}
}
plese suggest me how to fix this error. Am using the following code
Thanks & Regards
Paul.S

Answers (1)