简体   繁体   English

使用加密数据php后mcrypt_decrypt错误输出

[英]mcrypt_decrypt wrong output after using encrypted data php

I have a small problem with php mcrypt_decrypt function. 我对php mcrypt_decrypt函数有一个小问题。 Firstly, I use a 16-byte string, and encrypt it using mcrypt_encrypt ; 首先,我使用一个16字节的字符串,并使用mcrypt_encrypt对其进行mcrypt_encrypt then, I use base64_encode , and put the output to mcrypt_decrypt , in order to get the initial string. 然后,我使用base64_encode ,并将输出放入mcrypt_decrypt ,以获取初始字符串。

But the output is not what's expected. 但是输出不是预期的。 I checked that my base64 decoded string input for decoding is the exact output produced by mcrypt_decrypt . 我检查了用于解码的base64解码字符串输入是否是mcrypt_decrypt产生的确切输出。 Here is my code: 这是我的代码:

//encrypt
$str="KKQT9W4st7vmdkps";
$key="43625A8C1E4330BDF84DDEE3DD105037";
$block = mcrypt_get_block_size('rijndael_128', 'ecb');
$passcrypt=mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_ECB);
echo $passcrypt;

That outputs PTfZ6Ephh8LTxXL4In33Og== . 输出PTfZ6Ephh8LTxXL4In33Og== The decryption script is the following: 解密脚本如下:

//decrypt
$str='PTfZ6Ephh8LTxXL4In33Og==';
$key='43625A8C1E4330BDF84DDEE3DD105037';
$str = base64_decode($str);
$str = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key,
    $str, MCRYPT_MODE_ECB,''),"\0");
$block = mcrypt_get_block_size('rijndael_128', 'ecb');
echo $str;  

And the output is not KKQT9W4st7vmdkps , but -nγ kk7Ζn'T instead. 输出不是KKQT9W4st7vmdkps ,而是-nγ kk7Ζn'T Any ideas? 有任何想法吗? I'm using XAMPP and Apache server. 我正在使用XAMPP和Apache服务器。

谢谢反馈,这是我犯的一个愚蠢的错误……实际上,解密函数中的'PTfZ6Ephh8LTxXL4In33Og =='是错误的,因为最后“ I”为“ l” ...所以解密是不正确的。 。但这也不是我的错,因为我是从QR CODE扫描仪获取此字符串并且“ I”和“ l”显示相同的...

For encryption, you need to: 对于加密,您需要:

1) Create an encryption resource 1)创建一个加密资源

$str = "KKQT9W4st7vmdkps";
$key = "43625A8C1E4330BDF84DDEE3DD105037";
$r = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '',MCRYPT_MODE_ECB, '');

2) Randomly create encryption vector based on the size of $r 2)根据$r的大小随机创建加密向量

$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($r),MCRYPT_RAND);

3) Initiliazing module using the resource,key and string vector 3)使用资源,密钥和字符串向量初始化模块

mcrypt_generic_init($r,$key,$iv);

4) Encrypt data/string using resource $r 4)使用资源$r加密数据/字符串

$encrypted = mcrypt_generic($r,$str);

5) Encode it using base64_encode 5)使用base64_encode对其进行编码

  $encoded = base64_encode($encrypted);
        if(!mcrypt_generic_deinit($r) || !mcrypt_module_close($r))
            $encoded = false;

6) Echoing it 6)回声

echo 'Encrypted: '.$encoded;

For decryption, it's like a reverse process of encrypt 对于解密,这就像是加密的反向过程

        //Using the same enrypted string       
        $decoded = (string) base64_decode(trim($encoded));
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '',MCRYPT_MODE_ECB, '');
        $ivs = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        mcrypt_generic_init($td,$key, $ivs);

        $decoded = (string) trim(mdecrypt_generic($td, $decoded));

        if(!mcrypt_generic_deinit($td) || !mcrypt_module_close($td))
            $decoded = false;

Echoing it 呼应

echo 'Decrypted: '. $decoded;

Hope this helps. 希望这可以帮助。 More info here . 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM