从openssl_encrypt PHP 7.0获取字节数组

时间:2019-06-19 14:14:08

标签: php cryptography aes

我使用以下C#代码加密字节数组:

public static byte[] AES256_Encrypt(byte[] input, byte[] key)
    {
        var aesAlg = new AesManaged
        {
            KeySize = 256,
            Key = key,
            BlockSize = 128,
            Mode = CipherMode.ECB,
            Padding = PaddingMode.Zeros,
            IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
        };

        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
        return encryptor.TransformFinalBlock(input, 0, input.Length);
    }

输入字节:8F 21 FF 43 49 27

私钥:C8 AE 1D EC D8 25 8D 52 6F 56 FB 13 F1 5D 3C 84 E0 02 16 89 34 8F 32 87 10 0A B6 CD 81 27 DE 7E

输出:F1 00 ED B4 25 8F A6 D5 13 32 4A 8E DE 87 0C 3F

现在我想在PHP中计算相同的值

$plaintext = chr(0x8F). chr(0x21). chr(0xFF). chr(0x43). chr(0x49). chr(0x27);

$method = 'aes-256-ecb';

$key = chr(0xc8). chr(0xae ). chr(0x1d ). chr(0xec ). chr(0xd8 ). chr(0x25 ). chr(0x8d ). chr(0x52 ). chr(0x6f ). chr(0x56 ). chr(0xfb ). chr(0x13 ). chr(0xf1 ). chr(0x5d ). chr(0x3c ). chr(0x84
). chr(0xe0 ). chr(0x02 ). chr(0x16 ). chr(0x89 ). chr(0x34 ). chr(0x8f ). chr(0x32 ). chr(0x87 ). chr(0x10 ). chr(0x0a ). chr(0xb6 ). chr(0xcd ). chr(0x81 ). chr(0x27 ). chr(0xde ). chr(0x7e);

$encrypted = base64_encode(openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA));

输出将是:y2ipJIV4m96LKDu7jI + Mrg ==(当我将其从ASCII转换为十六进制时,这不是我的十六进制值)

我需要什么来取回字节数组?

0 个答案:

没有答案