简体   繁体   中英

How to decrypt C# code in php

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....

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM