0

I have written a code in C# for encryption using Rijndael algorithm. Now I want to decrypt the encrypted value in php. I tried for that but am not getting exact string which I encrypted. Below is the encryption code in C#.

public string Encrypt(string textToBeEncrypted, string Password) 
{ 

    RijndaelManaged RijndaelCipher = new RijndaelManaged(); 
    ICryptoTransform Encryptor = null; 
    byte[] plainText = null; 
    try 
    { 
        byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString()); 
        PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt); 
        //Creates a symmetric encryptor object. 
        Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)); 
        plainText = Encoding.Unicode.GetBytes(textToBeEncrypted); 

    } 
    catch (Exception ex) 
    { 
        string str = "Method Name: " + MethodBase.GetCurrentMethod().Name + " | Description: " + ex.Message + ex.InnerException; 
        log.Error(str); 

    } 
    return Convert.ToBase64String(Encryptor.TransformFinalBlock(plainText, 0, plainText.Length)); 

} 

Decryption code in php is

function decryptData($value){
    $key = "same key used in above c# code";
    $crypttext = $value;
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
    return trim($decrypttext); 
}

I got c# code for decryption as below

public string Decrypt(string TextToBeDecrypted, string Password) { 
RijndaelManaged RijndaelCipher = new RijndaelManaged(); 
string DecryptedData; 
byte[] EncryptedData = Convert.FromBase64String(TextToBeDecrypted); 
byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString()); 
//Making of the key for decryption 
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt); 
//Creates a symmetric Rijndael decryptor object. 
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32),SecretKey.GetBytes(16)); 
byte[] plainText = Decryptor.TransformFinalBlock(EncryptedData, 0, EncryptedData.Length); 
//Converting to string 
DecryptedData = Encoding.Unicode.GetString(plainText); 
return DecryptedData; 
} 

But want same code in PHP.Key will be same as used for encryption. Please advice....

3
  • Can you show an example $key? Commented Jul 22, 2013 at 5:00
  • Seems you base64 it in your non PHP code, don't forget to base64_decode it in php (unless I don't remember the params for mcrypt correctly, I haven't used it much). Commented Jul 22, 2013 at 5:04
  • Hi @AmalMurali, $key contains alphabets upto 12 chars ie.ABCDEFGPQRUV Commented Jul 23, 2013 at 4:53

1 Answer 1

1

The following checks should fix your problem.

  1. You need to have the same mode for encryption and decrytion.In php code you are using ECB mode for decryption.check if you are using the same ECB mode in C#.

  2. Generate the key and iv in the c# for encryption and use the same values for decryption.dont generate key or iv in php decryption code.

  3. Decode the base64 string before decrytion in php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.