I am using Rijndael algorithm for encyption and decription in ASP.Net Core but getting error <big>specified padding mode is not valid for this algorithm</big>
private static readonly string _Pwd = "3BF3EE08-B757-41ba-87F5-59CC6B9A47B4";
private static readonly byte[] _Salt = new byte[] { 0x45, 0xf1, 0x61, 0x6e, 0x20, 0x0, 0x65, 0x64, 0x76, 0x65, 0x64, 0x3, 0x76 };
private static readonly byte[] Key = { 12, 14, 6, 22, 32, 28, 8, 24, 13, 3, 11, 9, 17, 15, 4, 29 };
private static readonly byte[] IV = { 12, 2, 16, 7, 5, 9, 17, 8, 4, 47, 16, 18, 1, 32, 25, 10 };
public static string Decrypt(string cipherText)
{
if (string.IsNullOrEmpty(cipherText))
return "";
byte[] cipherBytes = Convert.FromBase64String(cipherText);
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(_Pwd, _Salt);
byte[] decryptedData = DoDecrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return System.Text.Encoding.Unicode.GetString(decryptedData);
}
private static byte[] DoDecrypt(byte[] cipherData, byte[] Key, byte[] IV)
{
MemoryStream ms = new MemoryStream();
CryptoStream cs = null;
try
{
Rijndael alg = Rijndael.Create();
alg.Key = Key;
alg.IV = IV;
cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(cipherData, 0, cipherData.Length);
cs.FlushFinalBlock();
return ms.ToArray();
}
catch (Exception ex)
{
return null;
}
finally
{
cs.Close();
}
}
What I have tried:
public static string Decrypt(string cipherText)
{
if (string.IsNullOrEmpty(cipherText))
return "";
byte[] cipherBytes = Convert.FromBase64String(cipherText);
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(_Pwd, _Salt);
byte[] decryptedData = DoDecrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
return System.Text.Encoding.Unicode.GetString(decryptedData);
}
private static byte[] DoDecrypt(byte[] cipherData, byte[] Key, byte[] IV)
{
MemoryStream ms = new MemoryStream();
CryptoStream cs = null;
try
{
cs = new CryptoStream(ms, Rijndael.CreateDecryptor(_Pwd, IV, KeySize.Aes128), CryptoStreamMode.Write);
cs.Write(cipherData, 0, cipherData.Length);
cs.FlushFinalBlock();
return ms.ToArray();
}
catch(Exception ex)
{
return null;
}
finally
{
}
}