node.js php 混用 java,node.js php java python AES/CBC/PKCS5Padding加密解密通用

node.js php java python AES/CBC/PKCS5Padding加密解密通用

目录

[TOC]

nodejsvar crypto = require('crypto');

var data = "test";

var key = '7854156156611111';

//data 是准备加密的字符串,key是你的密钥

function encryption(data, key) {

var iv = "0000000000000000";

var clearEncoding = 'utf8';

var cipherEncoding = 'base64';

var cipherChunks = [];

var cipher = crypto.createCipheriv('aes-128-cbc', key, iv);

cipher.setAutoPadding(true);

cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));

cipherChunks.push(cipher.final(cipherEncoding));

return cipherChunks.join('');

}

//data 是你的准备解密的字符串,key是你的密钥

function decryption(data, key) {

var iv = "0000000000000000";

var clearEncoding = 'utf8';

var cipherEncoding = 'base64';

var cipherChunks = [];

var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);

decipher.setAutoPadding(true);

cipherChunks.push(decipher.update(data, cipherEncoding, clearEncoding));

cipherChunks.push(decipher.final(clearEncoding));

return cipherChunks.join('');

}

console.log(encryption(data, key)) ;

phpclass Aes

{

/**

* AES加密

* @param string $decrypted_data

* @param string $secret_key

* @param string $iv

* @return string|null

*/

public static function encrypt($decrypted_data, $secret_key, $iv)

{

if (empty($secret_key) || strlen($iv) < 16)

{

return null;

}

$blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);

$padded_data = Aes::pkcs5_pad($decrypted_data, $blocksize);

$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $secret_key, $padded_data, MCRYPT_MODE_CBC, $iv);

$encrypted_data = base64_encode($encrypted);

return $encrypted_data;

}

/**

* AES解密

* @param string $encrypted_data

* @param string $secret_key

* @param string $iv

* @return string|null

*/

public static function decrypt($encrypted_data, $secret_key, $iv)

{

if (empty($secret_key) || strlen($iv) < 16)

{

return null;

}

$encrypted_data = base64_decode($encrypted_data);

$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $secret_key, $encrypted_data, MCRYPT_MODE_CBC, $iv);

$decrypted_data = Aes::pkcs5_unpad($decrypted, "\0");

return $decrypted_data;

}

/**

* 采用pkcs5pad方式填充数据

* @param type $text

* @param type $blocksize

* @return type

*/

public static function pkcs5_pad($text, $blocksize)

{

$pad = $blocksize - (strlen($text) % $blocksize);

return $text . str_repeat(chr($pad), $pad);

}

/**

* 删除多余的填充数据

* @param type $text

* @return boolean

*/

public static function pkcs5_unpad($text)

{

$pad = ord($text{strlen($text) - 1});

if ($pad > strlen($text))

{

return false;

}

if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)

{

return false;

}

return substr($text, 0, -1 * $pad);

}

}

javaimport sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

public class AESPlus {

/**

* 加密

*

* @param strKey 密匙

* @param strIn 待加密串

* @return

* @throws Exception

*/

public static String encrypt(String strKey, String strIn) {

try {

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

cipher.init(

Cipher.ENCRYPT_MODE,

new SecretKeySpec(strKey.getBytes(), "AES"),

new IvParameterSpec(new byte[16])//初始化16空字节

);

byte[] encrypted = cipher.doFinal(strIn.getBytes());

return new BASE64Encoder().encode(encrypted);

} catch (Exception e) {

System.out.println(e);

return "";

}

}

/**

* 解密

* @param strKey 密匙

* @param strIn 待解密密串

* @return

*/

public static String decrypt(String strKey, String strIn){

try

{

byte[] encrypted1 = new BASE64Decoder().decodeBuffer(strIn);

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

SecretKeySpec keyspec = new SecretKeySpec(strKey.getBytes(), "AES");

IvParameterSpec ivspec = new IvParameterSpec(new byte[16]);

cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

byte[] original = cipher.doFinal(encrypted1);

String originalString = new String(original);

return originalString;

}

catch (Exception e) {

e.printStackTrace();

return null;

}

}

}

pythonfrom Crypto.Cipher import AES

import base64

def _pad(s): return s + (AES.block_size - len(s) % AES.block_size) * chr(AES.block_size - len(s) % AES.block_size)

def _cipher():

key = '7854156156611111'

iv = '0000000000000000'

return AES.new(key=key, mode=AES.MODE_CBC, IV=iv)

def encrypt_token(data):

return _cipher().encrypt(_pad(data))

def decrypt_token(data):

return _cipher().decrypt(data)

if __name__ == '__main__':

print('Python encrypt: ' + base64.b64encode(encrypt_token('test')))

# print('Python decrypt: ' + decrypt_token(base64.b64decode('FSfhJ/gk3iEJOPVLyFVc2Q==')))

目录

[TOC]

## nodejs

```nodejs

var crypto = require('crypto');

var data = "test";

var key = '7854156156611111';

//data 是准备加密的字符串,key是你的密钥

function encryption(data, key) {

var iv = "0000000000000000";

var clearEncoding = 'utf8';

var cipherEncoding = 'base64';

var cipherChunks = [];

var cipher = crypto.createCipheriv('aes-128-cbc', key, iv);

cipher.setAutoPadding(true);

cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));

cipherChunks.push(cipher.final(cipherEncoding));

return cipherChunks.join('');

}

//data 是你的准备解密的字符串,key是你的密钥

function decryption(data, key) {

var iv = "0000000000000000";

var clearEncoding = 'utf8';

var cipherEncoding = 'base64';

var cipherChunks = [];

var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);

decipher.setAutoPadding(true);

cipherChunks.push(decipher.update(data, cipherEncoding, clearEncoding));

cipherChunks.push(decipher.final(clearEncoding));

return cipherChunks.join('');

}

console.log(encryption(data, key)) ;

```

## php

```php

class Aes

{

/**

* AES加密

* @param string $decrypted_data

* @param string $secret_key

* @param string $iv

* @return string|null

*/

public static function encrypt($decrypted_data, $secret_key, $iv)

{

if (empty($secret_key) || strlen($iv) < 16)

{

return null;

}

$blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);

$padded_data = Aes::pkcs5_pad($decrypted_data, $blocksize);

$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $secret_key, $padded_data, MCRYPT_MODE_CBC, $iv);

$encrypted_data = base64_encode($encrypted);

return $encrypted_data;

}

/**

* AES解密

* @param string $encrypted_data

* @param string $secret_key

* @param string $iv

* @return string|null

*/

public static function decrypt($encrypted_data, $secret_key, $iv)

{

if (empty($secret_key) || strlen($iv) < 16)

{

return null;

}

$encrypted_data = base64_decode($encrypted_data);

$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $secret_key, $encrypted_data, MCRYPT_MODE_CBC, $iv);

$decrypted_data = Aes::pkcs5_unpad($decrypted, "\0");

return $decrypted_data;

}

/**

* 采用pkcs5pad方式填充数据

* @param type $text

* @param type $blocksize

* @return type

*/

public static function pkcs5_pad($text, $blocksize)

{

$pad = $blocksize - (strlen($text) % $blocksize);

return $text . str_repeat(chr($pad), $pad);

}

/**

* 删除多余的填充数据

* @param type $text

* @return boolean

*/

public static function pkcs5_unpad($text)

{

$pad = ord($text{strlen($text) - 1});

if ($pad > strlen($text))

{

return false;

}

if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)

{

return false;

}

return substr($text, 0, -1 * $pad);

}

}

```

## java

```java

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

public class AESPlus {

/**

* 加密

*

* @param strKey 密匙

* @param strIn 待加密串

* @return

* @throws Exception

*/

public static String encrypt(String strKey, String strIn) {

try {

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

cipher.init(

Cipher.ENCRYPT_MODE,

new SecretKeySpec(strKey.getBytes(), "AES"),

new IvParameterSpec(new byte[16])//初始化16空字节

);

byte[] encrypted = cipher.doFinal(strIn.getBytes());

return new BASE64Encoder().encode(encrypted);

} catch (Exception e) {

System.out.println(e);

return "";

}

}

/**

* 解密

* @param strKey 密匙

* @param strIn 待解密密串

* @return

*/

public static String decrypt(String strKey, String strIn){

try

{

byte[] encrypted1 = new BASE64Decoder().decodeBuffer(strIn);

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

SecretKeySpec keyspec = new SecretKeySpec(strKey.getBytes(), "AES");

IvParameterSpec ivspec = new IvParameterSpec(new byte[16]);

cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

byte[] original = cipher.doFinal(encrypted1);

String originalString = new String(original);

return originalString;

}

catch (Exception e) {

e.printStackTrace();

return null;

}

}

}

```

## python

````python

from Crypto.Cipher import AES

import base64

def _pad(s): return s + (AES.block_size - len(s) % AES.block_size) * chr(AES.block_size - len(s) % AES.block_size)

def _cipher():

key = '7854156156611111'

iv = '0000000000000000'

return AES.new(key=key, mode=AES.MODE_CBC, IV=iv)

def encrypt_token(data):

return _cipher().encrypt(_pad(data))

def decrypt_token(data):

return _cipher().decrypt(data)

if __name__ == '__main__':

print('Python encrypt: ' + base64.b64encode(encrypt_token('test')))

# print('Python decrypt: ' + decrypt_token(base64.b64decode('FSfhJ/gk3iEJOPVLyFVc2Q==')))

````

*参考:*

[关于crypto的des-cbc加密向量的问题](https://cnodejs.org/topic/54f42ea3c8cd6f4d1c1f8bc0)

[Node.js中AES加密和其它语言不一致问题解决办法](http://www.jb51.net/article/47872.htm)

[node.js AES/ECB/PKCS5Padding 与其他语言的加密解密通用](http://yijiebuyi.com/blog/13e2ae33082ac12ba4946b033be04bb5.html)

评论:

d41d8cd98f00b204e9800998ecf8427e?s=40&d=mm&r=g

Mr_baicai

2017-09-27 20:57

java和python的输出好像不一样

2c6a749a55e9b6f7e15d0ada2244899e?s=40&d=mm&r=g

2017-10-01 20:38

@Mr_baicai:是不一样

13a0937628ca070cabd7097c6967ffe9?s=40&d=mm&r=g

2016-08-22 17:17

用楼主这个代码, python和 JAVA加密出来的base64不一样啊

2c6a749a55e9b6f7e15d0ada2244899e?s=40&d=mm&r=g

2016-09-25 19:43

@小七:抱歉一个多月才看到回复,key值,签名方法都一样的吗?

发表评论 登录:

昵称

2c6b9431e93c918dea94c1d18a9f2ef2.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值