Skip to content

Instantly share code, notes, and snippets.

@twn39
Created August 22, 2014 06:45
Show Gist options
  • Select an option

  • Save twn39/92a3ce2eb10e852a53e7 to your computer and use it in GitHub Desktop.

Select an option

Save twn39/92a3ce2eb10e852a53e7 to your computer and use it in GitHub Desktop.
PHP加密,node解密,采用blowfish CBC模式。
var crypto = require('crypto');
function BlowFish(){
this.decode = function(data, password){
var text = data.split('.');
var code = text[0];
var iv = text[1];
var decipher = crypto.createDecipheriv( 'bf-cbc', new Buffer(password, 'utf8'), new Buffer(iv, 'base64') );
decipher.setAutoPadding( auto_padding=false );
return decipher.update( code, 'base64', 'utf8' );
}
}
var password = 'key';
var data = 'xxxxxxxxxx';
var bf = new BlowFish();
var result = bf.decode(data, password);
console.log(result);
<?php
$password = 'key';
$data = 'data:' . mt_rand();
echo $data;
echo '<br>';
// 创建 iv ,无 iv 会提示错误
$size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($size, MCRYPT_DEV_RANDOM);
// 加密
$enc = mcrypt_encrypt(MCRYPT_BLOWFISH, $password, $data, MCRYPT_MODE_CBC, $iv );
$data = base64_encode($enc) . '.' . base64_encode($iv);
echo $data;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment