Node.js 是一个基于 Chrome JavaScript 运行时建立的一个平台, 用来方便地搭建快速的 易于扩展的网络应用. Node.js 借助事件驱动, 非阻塞 I/O 模型变得轻量和高效, 非常适合 运行在分布式设备 的 数据密集型 的实时应用
这篇文章主要介绍了 Java 与 Node.js 利用 AES 加密解密出相同结果的方法, 文中给出了详细的示例代码, 相信对大家的学习或者工作能带来一定的帮助, 需要的朋友们下面来一起看看吧
前言
工作中遇到 nodejs 端通过 aes 加密, 安卓客户端 Java 解密, 同样 nodejs 也需要解密安卓客户端加密过来的内容, 发现两个加密结果不一样, 查询资料发现 java 端需要对密钥再 MD5 加密一遍, 以下是 Java 与 Node.js 利用 AES 加密解密出相同结果的方法, 需要的朋友们下面来一起学习学习吧
JAVA 代码如下:
- package g.g;
- import java.security.MessageDigest;
- import javax.crypto.Cipher;
- import javax.crypto.spec.SecretKeySpec;
- public class AesECB {
- public static final String DEFAULT_CODING = "utf-8";
- /**
- * 解密
- * @author lmiky
- * @date 2014-2-25
- * @param encrypted
- * @param seed
- * @return
- * @throws Exception
- */
- private static String decrypt(String encrypted, String seed) throws Exception {
- 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, String key) throws Exception {
- 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();
- }
- public static void main(String[] args) throws Exception {
- System.out.println(AesECB.encrypt("fsadfsdafsdafsdafsadfsadfsadf", "1eVRiqy7b9Uv7ZMM"));
- System.out.println(AesECB.decrypt("b123e2d9199598c0e3f1999dc9e723387b68e29d2b3a0d59fc7d5946c750c6b4", "1eVRiqy7b9Uv7ZMM"));
- }
- }
Node.js 代码如下:
- var crypto = require('crypto');
- exports.aes_algorithm = "aes-128-ecb";
- exports.aes_secrect = "1eVRiqy7b9Uv7ZMM";
- exports.encrypt = function (text) {
- var cipher = crypto.createCipher(this.aes_algorithm, this.aes_secrect)
- var crypted = cipher.update(text, 'utf8', 'hex')
- crypted += cipher.final('hex');
- return crypted;
- };
- exports.decrypt = function (text) {
- var decipher = crypto.createDecipher(this.aes_algorithm, this.aes_secrect)
- var dec = decipher.update(text, 'hex', 'utf8')
- dec += decipher.final('utf8');
- return dec;
- };
- //var hw = this.encrypt("fsadfsdafsdafsdafsadfsadfsadf");
- //console.log(hw);
- //console.log(this.decrypt(hw));
来源: http://www.phperz.com/article/18/0218/358260.html