aes-256-cbc加密解密php与nodejs的加密数据互通nodejs的加密解密代码⽰例如下:
#!/usr/bin/env node
var crypto =require('crypto');
//解密
function decode(cryptkey, iv, secretdata){
var
decipher = ateDecipheriv('aes-256-cbc', cryptkey, iv),
decoded = decipher.update(secretdata,'base64','utf8');
decoded += decipher.final('utf8');
return decoded;
}
/
/解密
function encode(cryptkey, iv, cleardata){
var
encipher = ateCipheriv('aes-256-cbc', cryptkey, iv),
encoded = encipher.update(cleardata,'utf8','base64');
encoded += encipher.final('base64');
return encoded;
}
var
cryptkey = ateHash('sha256').update('__tazai_wolf__key').digest(),
iv ='1234567890000000',
buf ="Hello World",
enc =encode( cryptkey, iv, buf );
var dec =decode(cryptkey, iv, enc);
function b64enc(data){
var b =new Buffer(data,'binary');
String('base64');
}
console.warn("Encoded length: ", enc);
console.warn("Decoded all: "+ dec);
请问php能相应的加密解密代码应该如何写?
要求能解上⾯加密后的如:Gpkr1WGBFhMvNd/Hr0eaBg==
也能加密数据给nodejs进⾏解密。
需要注意的是在编译的时候需要加上–with-openssl选项
#!/usr/bin/env php
<?php
function decode($cryptkey,$iv,$secretdata){
return openssl_decrypt($secretdata,'aes-256-cbc',$cryptkey,false,$iv);
}
function encode($cryptkey,$iv,$secretdata){
return openssl_encrypt($secretdata,'aes-256-cbc',$cryptkey,false,$iv);
}
$cryptkey=hash('sha256','__tazai_wolf__key',true);
$iv='1234567890000000';
$buf="Hello World";
$enc=encode($cryptkey,$iv,$buf);
$dec=decode($cryptkey,$iv,$enc);js代码加密软件
echo"Encoded length: ",$enc,"\n";
echo"Decoded all: ",$dec,"\n";
运⾏结果:
➜./encode.js
Encoded length: Gpkr1WGBFhMvNd/Hr0eaBg== Decoded all: Hello World
➜./encode.php
Encoded length: Gpkr1WGBFhMvNd/Hr0eaBg== Decoded all: Hello World
做成模块
var crypto =require('crypto');
var fs =require('fs');
const Buffer =require('safer-buffer').Buffer
var fs =require('fs');
let iv ='1234567890009000';
/* 输⼊base64 输出utf8*/
exports.strEncode=function strEncode(buf){
//const iv = Buffer.alloc(16, 0);
let cryptkey = fs.readFileSync('../public/utils/rsa_private_key.pem'); cryptkey = ateHash('sha256').update(cryptkey).digest(), enc =encode(cryptkey, iv, buf);
return base64Encode(enc);
};
/*进来base64 出去utf8*/
exports.strDecode=function strDecode(buf){
//const iv = Buffer.alloc(16, 0);
let cryptkey = fs.readFileSync('../public/utils/rsa_private_key.pem'); cryptkey = ateHash('sha256').update(cryptkey).digest(), dec =decode(cryptkey, iv,base64Decode(buf));
return dec;
};
//解密
decode=function decode(cryptkey, iv, secretdata){
var decipher = ateDecipheriv('aes-256-cbc', cryptkey, iv), decoded = decipher.update(s
ecretdata,'base64','utf8');
decoded += decipher.final('utf8');
return decoded;
};
//解密
encode=function encode(cryptkey, iv, cleardata){
var encipher = ateCipheriv('aes-256-cbc', cryptkey, iv), encoded = encipher.update(cleardata,'utf8','base64');
encoded += encipher.final('base64');
return encoded;
};
base64Decode=function base64Decode(str){
let buffer = Buffer.from(str,'base64');
let s = String("utf-8");
return s;
};
base64Encode=function base64Encode($base64Str){
let buffer = Buffer.from($base64Str);
let s = String('base64');
return s;
};
function b64enc(data){
var b =new Buffer(data,'binary');
String('base64');
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论