Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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();

            //csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

            return str; // (Encoding.ASCII.GetString(str));
        }


My encryption code (EncryptRJ128) is working properly.
Posted
Comments
CPallini 29-Nov-13 5:35am    
Is your DecryptRJ128 function able to decrypt what EncryptRJ128 encrypts (did you make a test)?
Then, do EncryptRJ128 and 'PHP encrypting' produce the same result when given the same input? What are the differences?
ahmedfaruk88 29-Nov-13 6:55am    
My decryption function is able to decrypt what my encryption function encrypts

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900