如果openssl encrypt decrypt的内容只有零(0),则解密失败

时间:2016-05-08 12:58:40

标签: php openssl

我正在使用以下函数对WordPress插件中的wordpress帖子内容进行加密和解密:

public static function afz_encrypt_decrypt( $action, $string ) {
    $output = false;

    $encrypt_method = "AES-256-CBC";
    $secret_key     = '12345678901234567890123456789012';
    $iv_size        = 16;

    if ( $action == 'encrypt' ) {
        $encrypt_iv = mcrypt_create_iv( $iv_size, MCRYPT_DEV_URANDOM );
        $output     = openssl_encrypt( $string, $encrypt_method, $secret_key, 0, $encrypt_iv );
        $output     = base64_encode( $encrypt_iv . $output );
    } else if ( $action == 'decrypt' ) {
        $decrypt_iv = substr( base64_decode( $string ), 0, $iv_size );
        $output     = stripslashes( openssl_decrypt( substr( base64_decode( $string ), $iv_size ), $encrypt_method,
            $secret_key,
            0,
            $decrypt_iv ) );
        if ( false == $output ) {
            $output = $string;
        }
    }

    return $output;
}

如果内容仅为零字符,则上述代码无法解密文本!!!

它返回加密生成的编码字符串。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

if ( false == $output ) {
    $output = $string;
}

检查输出是否为" falsy"如果是,则将$output设置回原始加密字符串。 "0"是假的。用

替换支票
if ( false === $output ) {

检查输出是否实际为假而不仅仅是假的。

要更好地解释什么是和不是假,请参阅this documentation page

相关问题