.net中的Openssl_Decrypt方法在PHP中不起作用

I have a PHP code sample showing how to decrypt a certain message. I'm trying to decrypt that message using C#, but no luck yet.

Here is what I'm trying:

public static string AesDecrypt(byte[] dataToDecrypt, byte[] key, byte[] iv)
    {
        using (var aes = new AesCryptoServiceProvider())
        {
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.None;
            //aes.KeySize = 256;

            aes.Key = key;
            aes.IV = iv;

            using (var memoryStream = new MemoryStream(dataToDecrypt))
            {
                var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(key, iv), CryptoStreamMode.Read);

                using (var reader = new StreamReader(cryptoStream))
                {
                    return reader.ReadToEnd();
                }
            }
        }

    }

Here is how I'm calling the method:

        var aesKey = GenerateSha256Hash(Encoding.ASCII.GetBytes("t-3zafRa"));

        var aesIv = GenerateSha256Hash(Encoding.ASCII.GetBytes("St@cE4eZ"));
        var ivString = ConvertByteArrayToSring(aesIv).Substring(0, 16);

        // I'm getting a 24 byte array here, but message needs to be multiple of 16, isn't it?
        var encryptedText = Convert.FromBase64String("RmVpUjU4VjJlcXp2dkN5UlhUODQzUT09");


        var aesDecryptedBytes = AesDecrypt(encryptedText, aesKey, Encoding.ASCII.GetBytes(ivString));

Here are the User-defined methods from above:

public static byte[] GenerateSha256Hash(byte[] toBeHashed)
    {
        using (var sha256 = SHA256.Create())
        {
            return sha256.ComputeHash(toBeHashed);
        }
    }

    public static string ConvertByteArrayToSring(byte[] bytes)
    {
        var builder = new StringBuilder();
        foreach (var t in bytes)
        {
            builder.Append(t.ToString("x2"));
        }
        return builder.ToString();
    }

The PHP code sample I have is bellow:

php > $secret_key = "t-3zafRa";
$secret_iv = "St@cE4eZ";
php > $encrypted_txt = "RmVpUjU4VjJlcXp2dkN5UlhUODQzUT09";
php > $encrypt_method = "AES-256-CBC";
php > $key = hash('sha256', $secret_key);
php > $iv = substr(hash('sha256', $secret_iv), 0, 16);
php > echo openssl_decrypt(base64_decode($encrypted_txt), $encrypt_method, $key, 0, $iv);

I'm getting correct value for Key, IV and even the message when I'm converting it from base64String but the final call to Openssl_Decrypt seems not working for me.

I'm getting an error which says "Input data is not a complete block", which is kind of expected, I tried to remove first and last 8 byte as IV from the message to make it a 16 byte, but I'm still getting jibberish.

What I'm missing here? Thanks.

duandu8707
duandu8707 它实际上并没有解决问题,编写PHP部分的人说,用C#解密它很麻烦。他们将为我们制作api以获得解密值。
一年多之前 回复
duangou2046
duangou2046 Topaco的评论是否为您解决了问题?如果是这样,您可以删除问题或自己发布答案。或者,如果你非常好,你首先给予Topaco回答的机会,然后如果Topaco不这样做的话自己做。
一年多之前 回复
douying4203
douying4203 目前,PHP-hash函数将结果作为十六进制字符串返回,结果长度为64字节。因此,使用隐式截断的64字节密钥。这真的是你想要的吗?请检查结果是否不应返回更好的二进制(这是通过将哈希函数的第三个参数设置为true来实现的)?同样的问题适用于IV。
一年多之前 回复
douzhao6584
douzhao6584 据我所知,0表示没有填充。
一年多之前 回复
duanjuan1103
duanjuan1103 openssl_decrypt中的0参数是OPENSSL_RAW_DATA或OPENSSL_ZERO_PADDING,这些选项究竟意味着什么?
一年多之前 回复
dpxnrx11199
dpxnrx11199 不,我错过了复制,它只是另一个变量,我现在将更新它。
一年多之前 回复
dongmacuo1193
dongmacuo1193 PHP代码中缺少secret_key定义?
一年多之前 回复
doucao1066
doucao1066 根据建议更新了帖子。
一年多之前 回复
duanji9311
duanji9311 发布PHP代码和缺少的用户定义方法。
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐