drhib62644 2019-07-09 11:48
浏览 185

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
    • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
    • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
    • ¥30 3天&7天&&15天&销量如何统计同一行
    • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
    • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
    • ¥15 vs2019中数据导出问题
    • ¥20 云服务Linux系统TCP-MSS值修改?
    • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
    • ¥20 怎么在stm32门禁成品上增加查询记录功能