使用mcrypt在Web服务上传递数据失败 [英] Using mcrypt to pass data across a webservice is failing

查看:79
本文介绍了使用mcrypt在Web服务上传递数据失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个错误处理程序脚本,该脚本对错误数据(文件,行,错误,消息等)进行加密,并将序列化的数组作为POST变量(使用curl)传递给脚本,然后将该错误记录到中央db.

I'm writing an error handler script which encrypts the error data (file, line, error, message etc) and passes the serialized array as a POST variable (using curl) to a script which then logs the error in a central db.

我已经在一个文件中测试了我的加密/解密功能,并且对数据进行了很好的加密和解密:

I've tested my encrypt/decrypt functions in a single file and the data is encrypted and decrypted fine:

define('KEY', 'abc');
define('CYPHER', 'blowfish');
define('MODE', 'cfb');


function encrypt($data) {
    $td = mcrypt_module_open(CYPHER, '', MODE, '');
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, KEY, $iv);
    $crypttext = mcrypt_generic($td, $data);
    mcrypt_generic_deinit($td);
    return $iv.$crypttext;
}

function decrypt($data) {
    $td = mcrypt_module_open(CYPHER, '', MODE, '');
    $ivsize = mcrypt_enc_get_iv_size($td);
    $iv = substr($data, 0, $ivsize);
    $data = substr($data, $ivsize);
    if ($iv)
    {
        mcrypt_generic_init($td, KEY, $iv);
        $data = mdecrypt_generic($td, $data);
    }
    return $data;
}

echo "<pre>";
$data = md5('');
echo "Data: $data\n";
$e = encrypt($data);
echo "Encrypted: $e\n";
$d = decrypt($e);
echo "Decrypted: $d\n";

输出:

Data: d41d8cd98f00b204e9800998ecf8427e
Encrypted: ê÷#¯KžViiÖŠŒÆÜ,ÑFÕUW£´Œt?†÷>c×åóéè+„N
Decrypted: d41d8cd98f00b204e9800998ecf8427e

问题是,当我将加密函数放在传输文件(tx.php)中并将解密函数放在接收文件(rx.php)中时,数据没有被完全解密(两个文件都具有相同的常量集) (用于密钥,密码和模式).

The problem is, when I put the encrypt function in my transmit file (tx.php) and the decrypt in my recieve file (rx.php), the data is not fully decrypted (both files have the same set of constants for key, cypher and mode).

Data before passing: a:4:{s:3:"err";i:1024;s:3:"msg";s:4:"Oops";s:4:"file";s:46:"/Applications/MAMP/htdocs/projects/txrx/tx.php";s:4:"line";i:80;}
Data decrypted: Mª4:{s:3:"err";i:1024@7OYªç`^;g";s:4:"Oops";s:4:"file";sôÔ8F•Ópplications/MAMP/htdocs/projects/txrx/tx.php";s:4:"line";i:80;}

注意中间的随机字符.

我的卷曲非常简单:

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'data=' . $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);

我怀疑可能是导致这种情况的原因

Things I suspect could be causing this:

  • 卷曲请求的编码
  • 与mcrypt填充丢失字节有关的事情
  • 我盯着它看了太久了,错过了一些确实很明显的东西

如果我关闭了crypt函数(因此传输tx-> rx未被加密),数据将被很好地接收.

If I turn off the crypt functions (so the transfer tx->rx is unencrypted) the data is received fine.

任何人和所有人都非常感谢!

Any and all help much appreciated!

谢谢亚当

推荐答案

我发现了这一点-加密后必须先base64_encode数据,然后在解密前先base64_decode.

I figured it out - had to base64_encode the data after encryption and then base64_decode before decrypting.

感谢那些代表我思考的人!

Thanks to those who had a think on my behalf!

这篇关于使用mcrypt在Web服务上传递数据失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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