首页 > 解决方案 > 我的 AES 函数正确加密值,但解密返回 FALSE

问题描述

我的 AES 加密类有问题。这里是:

<?php
require_once 'SecretData.php';

class AESEncryption
{
    private static $AES_METHOD = 'aes-256-cbc';

    public static function encrypt($data, $key = null)
    {
        if($key == null)
        {
            $secretData = new SecretData();
            $key = $secretData->universalAESKey;
        }

        $ivSize = openssl_cipher_iv_length(self::$AES_METHOD);
        $iv = openssl_random_pseudo_bytes($ivSize);

        $encryptedData = openssl_encrypt($data, self::$AES_METHOD, $key, OPENSSL_RAW_DATA, $iv);
        return base64_encode($iv . $encryptedData);
    }

    public static function decrypt($data, $key = null)
    {
        $data = base64_decode($data);

        if($key == null)
        {
            $secretData = new SecretData();
            $key = $secretData->universalAESKey;
        }

        $ivSize = openssl_cipher_iv_length(self::$AES_METHOD);
        $iv = mb_substr($data, 0, $ivSize, '8bit');
        $decryptedData = mb_substr($data, $ivSize, null, '8bit');

        return openssl_decrypt($decryptedData, self::$AES_METHOD, $key, OPENSSL_RAW_DATA, $iv);
    }
}

我的登录类中有问题,我发现这个函数是错误的原因。我制作了以下调试文件:

<?php
require_once '/var/www/backend/security/HashingAlgorithms.php';
require_once '/var/www/backend/security/AESEncryption.php';

$data = 'alpha';

$enc = AESEncryption::encrypt($data);
$dec = AESEncryption::decrypt($enc);

echo 'Input: ' . $data . ' <> Encrypted: ' . $enc . ' <> Decrypted: ' . $dec;
var_dump($dec);

这是一个输出:

Input: alpha <> Encrypted: 7hB1hNiSYvU+Hy4xgvHb2sf/cVa2NPkx4+3kX+qdvUM= <> Decrypted: bool(false) 

当我查看代码时,一切看起来都很好:

我对解密功能做了同样的事情:

谁能看看这个并告诉我出了什么问题

标签: phpencryptionaes

解决方案


如果没有看到该SecretData.php文件,我无法将您引导至特定的故障点。但是,我可以自己创建一个假人来确认代码本身可以正常工作。

代码

<?php

class SecretData
{
    public $universalAESKey = '79f0f1a2e72b6654bba3071ff8210c13';
}

class AESEncryption
{
    private static $AES_METHOD = 'aes-256-cbc';

    public static function encrypt($data, $key = null)
    {
        if($key == null)
        {
            $secretData = new SecretData();
            $key = $secretData->universalAESKey;
        }

        $ivSize = openssl_cipher_iv_length(self::$AES_METHOD);
        $iv = openssl_random_pseudo_bytes($ivSize);

        $encryptedData = openssl_encrypt($data, self::$AES_METHOD, $key, OPENSSL_RAW_DATA, $iv);
        return base64_encode($iv . $encryptedData);
    }

    public static function decrypt($data, $key = null)
    {
        $data = base64_decode($data);

        if($key == null)
        {
            $secretData = new SecretData();
            $key = $secretData->universalAESKey;
        }

        $ivSize = openssl_cipher_iv_length(self::$AES_METHOD);
        $iv = mb_substr($data, 0, $ivSize, '8bit');
        $decryptedData = mb_substr($data, $ivSize, null, '8bit');

        return openssl_decrypt($decryptedData, self::$AES_METHOD, $key, OPENSSL_RAW_DATA, $iv);
    }
}

测试

$data = 'alpha';

$enc = AESEncryption::encrypt($data);
$dec = AESEncryption::decrypt($enc);

echo 'INPUT:' . $data . PHP_EOL;
echo 'ENCRP:' . $enc . PHP_EOL;
echo 'DECRP:' . $dec . PHP_EOL;

$enc = AESEncryption::encrypt($data, "e2e0cc36ea14bc5cd94473facd4731a6");
$dec = AESEncryption::decrypt($enc, "e2e0cc36ea14bc5cd94473facd4731a6");

echo 'INPUT:' . $data . PHP_EOL;
echo 'ENCRP:' . $enc . PHP_EOL;
echo 'DECRP:' . $dec . PHP_EOL;

结果

INPUT:alpha
ENCRP:PUn1xaDRMX4U0K4NVnJiRv4mtROpn3WvcFnSrR9EJ98=
DECRP:alpha

INPUT:alpha
ENCRP:QOs1vvy/6aKRSVGmZQWp7EvSNoISCpJ4Vsy3T3ixXZ4=
DECRP:alpha

推荐阅读