Please can someone help me with this, i have been on this for a month now, our client are frustrated. I have a PHP API service that i am calling from asp.net using HTTP request call, the PHP API Server returns a response in an encrypted form (AES 128) and i have decrypt it in asp.net C#. I was able to successfully encrypt the data, make an HTTP POST request to the server and i got a response. Where the problem lies is that whenever i decrypt the response message i got different kind of exceptions like cryptographicexception padding is invalid and cannot be removed, Length of the data to decrypt is invalid. e.t.c
Here Encryption Code
public static string EncryptRJ128(string prm_key, string prm_iv, string prm_text_to_encrypt)
{
var sToEncrypt = prm_text_to_encrypt;
var myRijndael = new RijndaelManaged()
{
Padding = PaddingMode.Zeros,
Mode = CipherMode.CBC,
KeySize = 128,
BlockSize = 128
};
var key = Encoding.ASCII.GetBytes(prm_key);
var IV = Encoding.ASCII.GetBytes(prm_iv);
var encryptor = myRijndael.CreateEncryptor(key, IV);
var msEncrypt = new MemoryStream();
var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
var toEncrypt = Encoding.ASCII.GetBytes(sToEncrypt);
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock();
var encrypted = msEncrypt.ToArray();
return (Convert.ToBase64String(encrypted));
}
Here is my decryption code
public static string DecryptRJ128(string prm_key, string prm_iv, string prm_text_to_decrypt)
{
var sEncryptedString = prm_text_to_decrypt;
var myRijndael = new RijndaelManaged()
{
Padding = PaddingMode.Zeros,
Mode = CipherMode.CBC,
KeySize = 128,
BlockSize = 128
};
var key = Encoding.ASCII.GetBytes(prm_key);
var IV = Encoding.ASCII.GetBytes(prm_iv);
var decryptor = myRijndael.CreateDecryptor(key, IV);
var sEncrypted = Convert.FromBase64String(sEncryptedString);
var fromEncrypt = new byte[sEncrypted.Length];
var msDecrypt = new MemoryStream(sEncrypted);
var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
var strmReader = new StreamReader(csDecrypt);
var str = strmReader.ReadToEnd();
return str;
}
My encryption code (EncryptRJ128) is working properly.