nodejs和java使用AES实现相同的加密解密方式
由于开发项目使用Java做的后台管理系统,而用户使用平台为nodejs编写,故需要实现两个平台的相同加密解密方式,我这里是使用了AES进行实现。nodejs定义//引入组件var crypto = require('crypto');//加密var aesEncrypt = function (data, secretKey) {var cipher = crypto.createCipher('
·
由于开发项目使用Java做的后台管理系统,而用户使用平台为nodejs编写,故需要实现两个平台的相同加密解密方式,我这里是使用了AES进行实现。
nodejs
定义
//引入组件
var crypto = require('crypto');
//加密
var aesEncrypt = function (data, secretKey) {
var cipher = crypto.createCipher('aes-128-ecb', secretKey);
return cipher.update(data, 'utf8', 'hex') + cipher.final('hex');
}
//解密
var aesDecrypt = function (data, secretKey) {
var cipher = crypto.createDecipher('aes-128-ecb', secretKey);
return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8');
}
使用方法
//解密(传入已加密的密码和自定义的解密key)
var db_password = aesDecrypt(password, "1234567887654321");
//加密(传入需要加密的密码和自定义的加密key)
var new_password = aesEncrypt(password, "1234567887654321");
注意加密和解密的key需要一致,是由数字和字母组成的字符串,而且需要是16位!!否则的话可能会出错
java
定义
package com.web.controller.tool;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
* AES加密,与Nodejs 保持一致
* @author lmiky
* @date 2014-2-25
*/
public class AESForNodejs {
public static final String DEFAULT_CODING = "utf-8";
/**
* 解密
* @author lmiky
* @date 2014-2-25
* @param encrypted
* @param seed
* @return
* @throws Exception
*/
public static String decrypt(String encrypted) throws Exception {
String seed = "1234567887654321";//此处需要和nodejs里的key一致
byte[] keyb = seed.getBytes(DEFAULT_CODING);
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(keyb);
SecretKeySpec skey = new SecretKeySpec(thedigest, "AES");
Cipher dcipher = Cipher.getInstance("AES");
dcipher.init(Cipher.DECRYPT_MODE, skey);
byte[] clearbyte = dcipher.doFinal(toByte(encrypted));
return new String(clearbyte);
}
/**
* 加密
* @author lmiky
* @date 2014-2-25
* @param content
* @param key
* @return
* @throws Exception
*/
public static String encrypt(String content) throws Exception {
String key = "1234567887654321";//此处需要和nodejs里的key一致
byte[] input = content.getBytes(DEFAULT_CODING);
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(key.getBytes(DEFAULT_CODING));
SecretKeySpec skc = new SecretKeySpec(thedigest, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skc);
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
return parseByte2HexStr(cipherText);
}
/**
* 字符串转字节数组
* @author lmiky
* @date 2014-2-25
* @param hexString
* @return
*/
private static byte[] toByte(String hexString) {
int len = hexString.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++) {
result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
}
return result;
}
/**
* 字节转16进制数组
* @author lmiky
* @date 2014-2-25
* @param buf
* @return
*/
private static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex);
}
return sb.toString();
}
}
使用
//解密
String db_password = AESForNodejs.decrypt(password);
//加密
String new_password = AESForNodejs.encrypt(password);
更多推荐
所有评论(0)