用openssl解密mcrypt [英] Decrypt mcrypt with openssl

查看:290
本文介绍了用openssl解密mcrypt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于mcrypt被认为是过时的,我的任务是将当前的代码升级为使用openssl。听起来很简单,但是经过几天的尝试和失败,我觉得疯了。



我的问题是:有什么办法可以解密以前用mcrypt加密的openssl数据?我已经阅读了很多关于这个问题的帖子,大多数人都说在运行mcrypt之前,需要先前手动填充数据。
问题是mcrypted的数据已经被加密(使用自动填充mcrypt提供),并驻留在数据库中,因此修改不可能和/或需要。



提示:


  1. 使用的算法是rijndael-128 cbc,带有32 -byte键(所以我使用aes-256-cbc作为openssl)。

  2. 我正在使用一个用于php(php-crypto)的openssl包装器。

  3. 我已经设法使反向操作工作(使用mcrypt解码openssl),只要它们是非字母数字即可解除结束解码的字符。

  4. 手动填充mcrypting之前的数据,然后使用openssl进行解密,就像一个魅力一样,但这不是问题。

一些代码段:

  //简单的mcrypt加密,用php-crypto示例解密
//这不起作用,并产生完成密码失败错误
$ data =This是文字;
$ strMcryptData = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$ key,$ data,MCRYPT_MODE_CBC,$ iv);

$ algorithm ='aes-256-cbc';
$ cipher = new Cipher($ algorithm);
$ sim_text = $ cipher-> decrypt($ strMcryptData,$ key,$ iv);

//使用填充进行简单的mcrypt加密,使用php-crypto解密
//解密时生成正确的文本
$ pad = $ blocksize - (strlen )%$ blocksize);
$ text = $ data;
$ text。= str_repeat(chr($ pad),$ pad);
$ strPaddedData = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$ key,$ text,MCRYPT_MODE_CBC,$ iv);

$ sim_text = $ cipher-> decrypt($ strPaddedData,$ key,$ iv);


解决方案

如果您在mcrypt中加密而不用手动添加PKCS7,mcrypt将快乐地用 NUL 字节填充您的明文。



OpenSSL将为您执行PKCS7填充每当使用 aes-X-cbc 。不幸的是,如果您有 AES-CBC(NULL_PADDED(明文))并尝试解密, openssl_decrypt 将尝试删除填充并失败。



比较 http: //3v4l.org/bdQe9 http://3v4l.org/jr68f http://3v4l.org/K6ZEU



OpenSSL扩展名目前不提供一种方式来说这个字符串没有填充,请不要剥离我的填充,然后删除您自己的 NUL 字节。您必须使用PKCS7填充进行加密,以便解密才能成功。



尽管这是OpenSSL的一个限制,但它强调,您遇到的唯一原因是因为 mcrypt是可怕的


Since mcrypt is considered obsolete, my task is upgrading the current code to use openssl. Sounds simple, but ... after a few days of try and failure I feel like going insane.

My question to you is: Is there any way you can decrypt with openssl data previously encrypted with mcrypt? I've read so many posts on this matter and most of them say that a previous manual padding of the data was/is necessary before running mcrypt on it. The issue is that the mcrypt-ed data is already encrypted (with the automatic null padding mcrypt provides) and resides in a database, so modification of that is not possible and/or desired.

Mentions:

  1. the algorithm used is rijndael-128 cbc with a 32-byte key (so I'm using aes-256-cbc for openssl).
  2. I'm using an openssl wrapper for php (php-crypto).
  3. I've managed to make the inverse operation work (decode openssl with mcrypt) by simply stripping the end decoded characters if they were non alpha-numerical.
  4. Manually padding the data before mcrypt-ing and then decrypting it using openssl works like a charm, but that's not the problem here.

Some code snippets:

// Simple mcrypt encrypt, decrypt with php-crypto example
// This doesn't work and produces a "Finalizing of cipher failed" error
        $data = "This is a text";
        $strMcryptData=mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv);

        $algorithm = 'aes-256-cbc';
        $cipher = new Cipher($algorithm);
        $sim_text = $cipher->decrypt($strMcryptData, $key, $iv);

// Simple mcrypt encrypt with padding, decrypt with php-crypto
// Works and produces the correct text on decryption
        $pad =  $blocksize - (strlen($data) % $blocksize);
        $text = $data;
        $text .= str_repeat(chr($pad), $pad);
        $strPaddedData=mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);

        $sim_text = $cipher->decrypt($strPaddedData, $key, $iv);

解决方案

If you encrypt in mcrypt without adding PKCS7 manually, mcrypt will happily pad your plaintext with NUL bytes.

OpenSSL will do PKCS7 padding for you whenever using aes-X-cbc. The unfortunate consequence of this is that if you have AES-CBC(NULL_PADDED(plaintext)) and try to decrypt it, openssl_decrypt will attempt to remove the padding and fail.

Compare http://3v4l.org/bdQe9 vs http://3v4l.org/jr68f and http://3v4l.org/K6ZEU

The OpenSSL extension does not currently offer you a way to say "This string is not padded, please don't strip the padding for me" and then remove the NUL bytes on your own. You must encrypt with PKCS7 padding in order for decryption to succeed.

Although this is a limitation of OpenSSL, it bears emphasizing that the only reason you're running into it is because mcrypt is terrible.

这篇关于用openssl解密mcrypt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆