简体   繁体   English

使用用 Rijndael (C#) 加密的 PHP 解密数据

[英]Decrypt data using PHP that was encrypted with Rijndael (C#)

my task is to decrypt data encrypted in C# using Php.我的任务是使用 PHP 解密在 C# 中加密的数据。 I try to use the phpseclib library.我尝试使用 phpseclib 库。 So here is the existing code that is used in C# to encrypt :所以这里是 C# 中用于加密的现有代码:

    public static String EncryptMyText(string clearText, string Password)
    {
        if (clearText.Length == 0) return "";
        byte[] clearBytes = System.Text.Encoding.UTF8.GetBytes(clearText);
        // second parameter is "Ivan Medvedev" in string
        PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
        return Convert.ToBase64String(encryptedData);
    }

    public static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
    {
        try
        {
            MemoryStream ms = new MemoryStream();
            Rijndael alg = Rijndael.Create();
            alg.Key = Key;
            alg.IV = IV;
            CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(clearData, 0, clearData.Length);
            cs.Close();
            byte[] encryptedData = ms.ToArray();
            return encryptedData;
        }
        catch (Exception ex)
        {
            string message = ex.Message;
        }
        return null;
    }

    EncryptMyText("sometext", "xxxxxxxxxxxxxxx"); // password have 15 characters length

It is not possible to change this code.无法更改此代码。 So here is what I've tried using phpseclib :所以这是我使用 phpseclib 尝试过的:

$key = "xxxxxxxxxxxxxxx";
$salt = "Ivan Medvedev";
$cipher = new Rijndael();
$cipher->setPassword($cle, 'pbkdf1', 'sha1', $salt);
$cipher->decrypt(base64_decode("someCryptedText"));

At this point, the code breaks with an Exception "Derived key too long" raised on setPassword() call.此时,代码因setPassword()调用引发的异常“派生密钥太长”而中断。

I tried many things such as changing blockLength and KeyLenghth and not using setPassword()我尝试了很多事情,例如更改blockLengthKeyLenghth而不是使用setPassword()

$cipher->setKeyLength(256);
$cipher->setBlockLength(128);

with no notable changes.无明显变化。

I have little experience in decrypting and ciphers, so I digged some info on the C# code used.我在解密和密码方面几乎没有经验,所以我挖掘了一些关于使用的 C# 代码的信息。 Looking at the Rijndael class here https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.rijndael?view=netframework-4.8 .在此处查看 Rijndael 类https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.rijndael?view=netframework-4.8 I tried several things with not much ideas on what I should look.我尝试了几件事情,但对我应该看什么没有太多想法。 I don't even know if there is possible to use Phpseclib to decrypt data made by this C# code.我什至不知道是否可以使用 Phpseclib 来解密此 C# 代码生成的数据。

Thanks to every people who could give me some guidance.感谢每一位能给我一些指导的人。

It looks like https://github.com/phpseclib/phpseclib/issues/1447#issuecomment-580594929 might answer your question.看起来https://github.com/phpseclib/phpseclib/issues/1447#issuecomment-580594929可能会回答您的问题。 Quoting it:引用它:

According to https://crypto.stackexchange.com/q/22271/4520 PasswordDeriveBytes implements a customized version of PBKDF1.根据https://crypto.stackexchange.com/q/22271/4520 PasswordDeriveBytes实现了 PBKDF1 的定制版本。

Try this:尝试这个:

 function pbkdf1ms(&$cipher, $password, $hash, $salt, $rounds) { $keyLength = $cipher->getKeyLength() >> 3; $blockLength = $cipher->getBlockLength() >> 3; $dkLen = $keyLength + $blockLength; $hashObj = new Hash(); $hashObj->setHash($hash); if ($dkLen > 100 * $hashObj->getLength()) { user_error('Derived key too long'); return false; } $t = $password . $salt; for ($i = 0; $i < $rounds; ++$i) { $old = $t; $t = $hashObj->hash($t); } $ctr = 1; while ($dkLen > strlen($t)) { $t.= $hashObj->hash($ctr++ . $old); } $key = substr($t, 0, $dkLen); $cipher->setKey(substr($key, 0, $keyLength)); //$cipher->setKey(substr($key, $keyLength)); $remainingBytes = $hashObj->getLength() - $keyLength % $hashObj->getLength(); $cipher->setIV( substr($key, $remainingBytes, $blockLength - $remainingBytes) . substr($key, $keyLength + $remainingBytes) ); } $c = new Rijndael; $c->setKeyLength(256); pbkdf1ms($c, 'xxxxxxxxxxxxxxx', 'sha1', 'Ivan Medvedev', 100); $c->decrypt(base64_decode("s@omeCryptedText"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Rijndael在iOS中加密c#中的解密字符串 - How decrypt string in c# was encrypted in iOS using Rijndael C#使用CakePHP的Security :: rijndael解密Rijndael加密的字符串 - C# decrypt Rijndael encrypted string using CakePHP's Security::rijndael 如何解密 C# 中的加密 MCRYPT_RIJNDAEL_256 值,该值由 PHP 中的 mcrypt 加密? - How can I decrypt an encrypted MCRYPT_RIJNDAEL_256 value in C#, that was encrypted by mcrypt in PHP? 用PHP解密C#RIJNDAEL编码的文本 - Decrypt C# RIJNDAEL encoded text in PHP Rijndael加密的文本导致数据长度解密无效错误-C# - Rijndael encrypted text causes length of data to decrypt is invalid error - C# 我无法使用 c# 中的 Rijndael 方法正确解密文件中的加密数据? - I was unable to properly decrypt the encrypted data in a file with the Rijndael method in c#? 我如何使用Rijndael在以C#加密的ios中解密文件 - How can i decrypt file in ios that was encrypted in c# using Rijndael 在SQL中使用Rijndael进行加密,并在C#中进行解密 - Encrypt using Rijndael in SQL and decrypt in C# 使用使用C#加密的PHP解密字符串 - Decrypt string using PHP that was encrypted using c# 使用AES / Rijndael在PHP中加密,在C#(WP7 / Silverlight)中解密 - Encrypt in PHP, Decrypt in C# (WP7 / Silverlight) using AES / Rijndael
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM