[英]encryption in PHP and decryption in node.js
我通过阅读有关堆栈溢出的各种答案尝试了建议和技巧,但它们似乎不够用,或者也许我缺少一些基本知识。 基本上,我正在尝试对php中的值进行加密,并将其传递给JavaScript读取的网页,然后将其发送到节点服务器进行处理。 但是我无法在php中加密的节点服务器上获得相同的值。
以下是php代码,php版本为5.5.12,在Windows 7 64位上运行:-
function encrypt($string){
$key = hash("SHA256", '1d417e2ffb2a00a3', true);
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$padding = $blockSize - (strlen($string) % $blockSize);
$string .= str_repeat(chr($padding), $padding);
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,$string, MCRYPT_MODE_CBC, $iv);
$result['cipher'] = base64_encode($ciphertext);
$result['iv'] = base64_encode($iv);
return $result;
}
我的node.js版本是0.10.31,运行在Windows 7 64位上,代码如下:-
var express = require("express");
var Server = require("http").Server;
var cookie = require("cookie");
var app = express();
var server = Server(app);
var sio = require("socket.io")(server);
var crypto = require('crypto');
sio.sockets.on("connection", function(socket) {
try{
socket.on('incoming_data', function(data){
var txt = new Buffer(data.encrypted_text,'base64');
var key = new Buffer('1d417e2ffb2a00a3','utf8');
var iv = new Buffer(data.iv,'base64');
var decipher = crypto.createDecipheriv('aes-128-cbc',key,iv);
var chunks = [];
chunks.push(decipher.update(txt,'hex','binary'));
chunks.push(decipher.final('binary'));
var fuid = chunks.join('');
console.log(fuid);
});
}catch(e){
console.log("err:-"+e);
console.log(e);
}
});// on connection ends
server.listen(9267, function(){
console.log('Node server listening on *:9267');
});
process.on('uncaughtException', function(err) {
console.log("FATAL: "+new Date().getTime()+": "+err);
});
我在nodejs控制台中通过打印流体得到的错误如下:-
FATAL: 1414483246855: TypeError: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
我正在寻找以下答案的解决方案:-
1)我的代码存在问题,需要解决哪些问题才能在节点服务器上获取与字符串相同的值。
2)希望将加密的文本和iv连接起来,并将它们作为单个base64编码的字符串发送到服务器。 因此,想要将它们分离回节点服务器上的准备传递给加密模块所需的代码。
3)此代码似乎容易受到oracle填充攻击。 如果您能建议我如何确保它的安全,那将是很棒的。
谢谢
问题可能出在您的编码上:
chunks.push(decipher.update(txt,'hex','binary'));
hex
看起来很奇怪,因为您的输入是缓冲区,而不是字符串。
以下快速测试有效(也回答2):
的PHP:
$key = 'secretsecretsecr';
$string = 'attack at dawn';
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$padding = $blockSize - (strlen($string) % $blockSize);
$string .= str_repeat(chr($padding), $padding);
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,$string, MCRYPT_MODE_CBC, $iv);
$packet = chr($iv_size) . $iv . $ciphertext;
print base64_encode($packet);
节点:
var crypto = require('crypto');
var key = 'secretsecretsecr';
var data = 'paste what the php code printed';
var txt = new Buffer(data,'base64');
var iv_size = txt[0];
var iv = txt.slice(1, iv_size + 1);
var ct = txt.slice(iv_size + 1);
var decipher = crypto.createDecipheriv('aes-128-cbc',key,iv);
var chunks = [];
chunks.push(decipher.update(ct));
chunks.push(decipher.final());
console.log(chunks.join(''));
传递iv大小时,您也可以简单地在节点侧对其进行硬编码。
关于3),我绝不是专家,据我所知,解决方法是使用HMAC对加密的数据包进行签名,以确保它们来自您的应用程序而不是“ oracle”( http:// www.ietf.org/id/draft-mcgrew-aead-aes-cbc-hmac-sha2-05.txt )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.