通过编辑加密字符串解密时删除特殊字符

时间:2016-01-16 17:30:33

标签: php encryption mcrypt

$secretKey = "MYSECRETKEY"; 

$plaintext = 'Plain Text Will Be here';

$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);

$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);  

$ivDecode = base64_encode(mcrypt_create_iv($iv_size, MCRYPT_RAND));

$encrypted = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, 
                            substr(sha1($secretKey), 0, 32),
                             $plaintext, 
                             MCRYPT_MODE_CBC, 
                             $iv), "\0..\32");

$encrypted = $iv . $encrypted;

$ciphertext_base64 = base64_encode($encrypted);

#echo  $ciphertext_base64 . "\n"; 
$decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128,
                             substr(sha1($secretKey), 0, 32),
                             base64_decode($ciphertext_base64),
                             MCRYPT_MODE_CBC,
                             base64_decode($ivDecode)), "\0..\32");

echo $decrypted;

当我运行上面的代码时,我得到了这个输出。

»_w>ø9â„6ÅkžPlain Text Will Be here

我无法编辑$decrypted字符串,因为我无法访问它。我只能编辑$ encrypted。那么如何通过编辑$encrypted字符串从输出中删除额外的特殊字符(»_ w>ø9â“6Åkž)。我想使用JSON将加密文本发送到不同的服务器进行解密。

1 个答案:

答案 0 :(得分:2)

在Base64解码之前无法分割iv和加密数据,首先是Base64解码然后拆分它们。

  1. MCRYPT_RIJNDAEL_128也是AES,其块大小为128位或16字节。 iv必须是那么大。而不是将base64_decode($iv)作为参数实际创建一个16字节的iv。如果它不是Base64编码的话,对它进行基本64解码将不起作用,在这种情况下它不是。

  2. 密钥应为128,192或256位(16,24或32字节),具有完全正确的互操作性大小,不依赖于加密算法的填充。

  3. 同样,对于要加密的输入,密钥在单独的语句中进行准备,以便更容易进行调试。

  4. 不要修剪输出,mcrypt_decrypt是正确的。填充可能会添加一个额外的块,这是必需的。

  5. 不要对解密的结果进行Base64解码,明文不是Base64编码的。 - zaph刚刚编辑

  6. “像这样的文本ïÕ[pI¤;Køv”可能在尝试以字符串形式打印数据时发生,并非所有二进制字节都有打印表示,并且许多二进制字节在0x80-0xff范围内具有特殊字符作为其打印表示。 / p>

    这是概念,没有经过测试,我在20年内没有使用过php,所以修正了任何错误:

    $secretKey = "1234567890123456"; # Note the length is 16-bytes, a full key
    $plaintext = 'XXXXXXXXX';
    echo  $plaintext . "\n";
    
    # --- ENCRYPTION ---
    $key = substr(sha1($secretKey), 0, 32)
    $iv = mcrypt_create_iv(16, MCRYPT_RAND);  
    $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, 
                                 $key,
                                 $plaintext, 
                                 MCRYPT_MODE_CBC, 
                                 $iv);
    # prepend the IV for it to be available for decryption
    $ciphertext = $iv . $ciphertext;
    $ciphertext_base64 = base64_encode($ciphertext);
    echo  $ciphertext_base64 . "\n";
    
    # --- DECRYPTION ---
    $key = substr(sha1($secretKey), 0, 32)
    $cipher_text_iv = base64_decode($ciphertext_base64)
    # split the iv and encrypted text
    $iv = substr($cipher_text_iv, 0, 16)
    $ciphertext = substr($cipher_text_iv, 16)
    
    $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,
                                 $key,
                                 $ciphertext,
                                 MCRYPT_MODE_CBC,
                                 $iv);
    echo $decrypted;