当前位置: 首页 > 知识库问答 >
问题:

如何使用openssl_decrypt解密加密字符串

谭新知
2023-03-14

我的代码如下:

<?php

class Encryption{
    
    protected $data, $encryption_type, $key, $iv;

    public function __construct(){
        $mydata = "Is this safe";
        $encryption = $this->secured_encryption($mydata);

        // Decrypting data
        $data_to_decrypt = $encryption;
        $this->decrypt_data($data_to_decrypt);
    }

    public function secured_encryption($data){
        $this->data = $data;
        $this->encryption_type = "AES-256-CBC"; // cipher algorithm
        $this->key = ['6d2d823df2e08c0d3cdf70f148998d49', 'fdb3a18c7b0a322fdb3a18c7b0a320d3'];
        $this->iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->encryption_type));
        $encrypted_data = openssl_encrypt($this->data, $this->encryption_type, $this->key[0], OPENSSL_RAW_DATA, $this->iv);
        $final_encryption = hash_hmac('SHA3-512', $encrypted_data, $this->key[1], true);
        $output = base64_encode($this->iv.$final_encryption.$encrypted_data);

        if($output):
            print("Encrypted data: {$output}<br/>");
        else:
            print("Error in encrypting data");
        endif;
    }

    public function decrypt_data($data){
        $this->data = base64_decode($data);
        $this->encryption_type = "AES-256-CBC"; // cipher algorithm
        $this->key = ['6d2d823df2e08c0d3cdf70f148998d49', 'fdb3a18c7b0a322fdb3a18c7b0a320d3'];
        $ivlen = openssl_cipher_iv_length($this->encryption_type);
        $this->iv = substr($this->data, 0, $ivlen);
        $hmac = substr($this->data, $ivlen, $sha2len = 32);
        $decrypt_data = substr($this->data, $ivlen, $sha2len);
        $final_decryption = openssl_decrypt($decrypt_data, $this->encryption_type, $this->key[0], OPENSSL_RAW_DATA, $this->iv);
        $calcmac = hash_hmac('SHA3-512', $decrypt_data, $this->key[1], true);

        if(hash_equals($hmac, $calcmac)):
            print($final_decryption);
        else:
            print("Error in decrypting data");
        endif;
    }
}

$encryption = Encryption();

?> 

有人来帮我吗

共有1个答案

朱兴运
2023-03-14

代码中有几个小问题:

>

  • secured_encryption方法中缺少返回语句。这是错误消息的触发器(hash_equals():Expected known_string为字符串,给定bool),因为$HMAC不包含字符串,而是false。因此,在secured_encryption结尾处,您必须添加:

    php prettyprint-override">return $output;
    

    有了这个,加密工作了,但解密还不起作用。

    $hmac = substr($this->data, $ivlen, $sha2len = 32);
    

    必须替换为:

    $hmac = substr($this->data, $ivlen, $sha2len = 64);   
    

    分离数据时,从$ivlen+$sha2len位置开始,将所有内容考虑到底,即:

    $decrypt_data = substr($this->data, $ivlen, $sha2len); 
    

    必须替换为:

    $decrypt_data = substr($this->data, $ivlen + $sha2len);
    
    Encrypted data: uTjcPmLK8jTktJ0oy5AqB40d5YFuAbgXMHfNEM6+JOH+DtyYAhckcv3mkLhdOvUqPlriKqYjHO6cpJI3ZdoTSS4lqDr0eH0MMiUpJMSXcan81irvobcdIV+rvaMPPRj7
    Is this safe 
    

  •  类似资料:
    • 问题内容: 我的意思是: 也许像: 在PHP中,您该怎么做? 尝试使用 ,但对我不起作用。 问题答案: 更新 PHP 7就绪版本。它使用PHP OpenSSL库中的openssl_encrypt函数。

    • 我的意思是: 可能是这样的: null 尝试使用,但对我不起作用。

    • 我想使用chacha20解密和加密字符串 BouncyCastleProvider正在使用chacha20技术。所以我包括了罐子。并尝试了代码,但无法工作。 pbe.java

    • 本文向大家介绍.net core使用MD5加密解密字符串,包括了.net core使用MD5加密解密字符串的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了.net core使用MD5加密解密字符串的具体代码,供大家参考,具体内容如下 调用加密 解密看效果 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

    • 问题内容: 我是密码学的新手。我希望学习如何在文件中加密和解密文本……当我在net中引用相关文章时。我怀疑对同一文本进行多次加密后,单个文本的加密文本是否会相同?谁能解决我的疑问? 问题答案: 这是使用该类的示例:

    • 字符串像这里一样用php加密。可以用这个用参数解密:Rijndael-256,ECB,Base64。但是我的ActionScript代码无法解密它: 更新: “它无法解密”意味着我弄错了纯文本。 在php中,明文首先由aes-256-ecb加密。然后由Base64编码。在ActionScript中,这些步骤以相反的顺序进行。 UPD2: 编码-解码测试: 在 停止后的输出是: 更新3: 它适用于P