Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中解密C#代码_C#_Php - Fatal编程技术网

如何在php中解密C#代码

如何在php中解密C#代码,c#,php,C#,Php,我用C#编写了一段代码,用于使用Rijndael算法进行加密。现在我想用php解密加密的值。我试过了,但没有得到我加密的确切字符串。 下面是C#中的加密代码 php中的解密代码是 function decryptData($value){ $key = "same key used in above c# code"; $crypttext = $value; $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYP

我用C#编写了一段代码,用于使用Rijndael算法进行加密。现在我想用php解密加密的值。我试过了,但没有得到我加密的确切字符串。 下面是C#中的加密代码

php中的解密代码是

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); 
}
我得到了解密的c代码,如下所示

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; 
} 

但我们需要相同的PHP代码。密钥将与用于加密的相同。请告知……

以下检查将解决您的问题

  • 加密和解密需要使用相同的模式。在php代码中,您使用ECB模式进行解密。请检查在C#中是否使用相同的ECB模式

  • 在c#中生成密钥和iv进行加密,并使用相同的值进行解密。不要在php解密代码中生成密钥或iv

  • 在php中,在decrytion之前解码base64字符串


  • 你能展示一个例子吗?
    $key
    ?似乎你在非PHP代码中使用它,别忘了用PHP对它进行base64_解码(除非我没有正确记住mcrypt的参数,我没有太多使用它)。Hi@AmalMurali,$key包含最多12个字符的字母,即ABCDEFGPQRUV
    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; 
    }