[英]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.