加密解密php与nodejs的加密数据互通
2016-10-20 23:53
274 查看
nodejs的加密解密代码示例如下:
#!/usr/bin/env node
var crypto = require('crypto');
//解密
function decode(cryptkey, iv, secretdata) {
var
decipher = crypto.createDecipheriv('aes-256-cbc', cryptkey, iv),
decoded = decipher.update(secretdata, 'base64', 'utf8');
decoded += decipher.final( 'utf8' );
return decoded;
}
//解密
function encode(cryptkey, iv, cleardata) {
var
encipher = crypto.createCipheriv('aes-256-cbc', cryptkey, iv),
encoded = encipher.update(cleardata, 'utf8', 'base64');
encoded += encipher.final( 'base64' );
return encoded;
}
var
cryptkey = crypto.createHash('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');
return b.toString('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);
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
#!/usr/bin/env node
var crypto = require('crypto');
//解密
function decode(cryptkey, iv, secretdata) {
var
decipher = crypto.createDecipheriv('aes-256-cbc', cryptkey, iv),
decoded = decipher.update(secretdata, 'base64', 'utf8');
decoded += decipher.final( 'utf8' );
return decoded;
}
//解密
function encode(cryptkey, iv, cleardata) {
var
encipher = crypto.createCipheriv('aes-256-cbc', cryptkey, iv),
encoded = encipher.update(cleardata, 'utf8', 'base64');
encoded += encipher.final( 'base64' );
return encoded;
}
var
cryptkey = crypto.createHash('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');
return b.toString('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);
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
相关文章推荐
- PHP使用mcrypt进行AES加密后的数据NodeJS等无法解密的问题修复方案
- iOS RSA公钥加密数据 服务端接受PHP私钥解密 反过服务端公钥加密数据 iOS端私钥解密数据
- C#, Java, PHP, Python和Javascript几种语言的AES加密解密实现【多种语言AES/CBC/PKCS5Padding通用加解密数据】
- 在PHP中使用AES加密算法加密数据及解密数据
- 关于php加密库加密数据上传数据库或解密出错的问题
- Nodejs加密php解密
- PHP通过OpenSSL生成证书、密钥并且加密解密数据
- PHP通过OpenSSL生成证书、密钥并且加密解密数据
- iOS RSA公钥加密数据 服务端接受PHP私钥解密 反过服务端公钥加密数据 iOS端私钥解密数据
- PHP通过OpenSSL生成证书、密钥并且加密解密数据
- PHP通过OpenSSL生成证书、密钥并且加密解密数据,以及公钥,私钥和数字签名的理解
- PHP对称加密函数实现数据的加密解密
- 在PHP中使用AES加密算法加密数据及解密数据
- PHP 使用 mcrypt 扩展中的 mcrypt_encrypt() 和 mcrypt_decrypt() 对数据进行加密和解密
- PHP数据传输加密解密
- PHP通过OpenSSL生成证书、密钥并且加密解密数据
- as3.0利用DES和AES对数据进行加密和解密并且与php端加密匹配!
- PHP通过OpenSSL生成证书、密钥并且加密解密数据
- PHP,Python互通的加密解密函数
- php openssl对等加密解密数据