前言
工作中遇到nodejs端通过aes加密,安卓客户端Java解密,同样nodejs也需要解密安卓客户端加密过来的内容,发现两个加密结果不一样,查询资料发现java端需要对密钥再MD5加密一遍,以下是Java与Node.js利用AES加密解密出相同结果的方法,需要的朋友们下面来一起学习学习吧。
JAVA代码如下:
代码如下 | 复制代码 |
packageg.g;
importjava.security.MessageDigest;
importjavax.crypto.Cipher; importjavax.crypto.spec.SecretKeySpec;
publicclassAesECB { publicstaticfinalString DEFAULT_CODING ="utf-8";
/** * 解密 * @author lmiky * @date 2014-2-25 * @param encrypted * @param seed * @return * @throws Exception */ privatestaticString decrypt(String encrypted, String seed)throwsException { byte[] keyb = seed.getBytes(DEFAULT_CODING); MessageDigest md = MessageDigest.getInstance("MD5"); byte[] thedigest = md.digest(keyb); SecretKeySpec skey =newSecretKeySpec(thedigest,"AES"); Cipher dcipher = Cipher.getInstance("AES"); dcipher.init(Cipher.DECRYPT_MODE, skey);
byte[] clearbyte = dcipher.doFinal(toByte(encrypted)); returnnewString(clearbyte); }
/** * 加密 * @author lmiky * @date 2014-2-25 * @param content * @param key * @return * @throws Exception */ publicstaticString encrypt(String content, String key)throwsException { byte[] input = content.getBytes(DEFAULT_CODING);
MessageDigest md = MessageDigest.getInstance("MD5"); byte[] thedigest = md.digest(key.getBytes(DEFAULT_CODING)); SecretKeySpec skc =newSecretKeySpec(thedigest,"AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skc);
byte[] cipherText =newbyte[cipher.getOutputSize(input.length)]; intctLength = cipher.update(input,0, input.length, cipherText,0); ctLength += cipher.doFinal(cipherText, ctLength);
returnparseByte2HexStr(cipherText); }
/** * 字符串转字节数组 * @author lmiky * @date 2014-2-25 * @param hexString * @return */ privatestaticbyte[] toByte(String hexString) { intlen = hexString.length() /2; byte[] result =newbyte[len]; for(inti =0; i < len; i++) { result[i] = Integer.valueOf(hexString.substring(2* i,2* i +2),16).byteValue(); } returnresult; }
/** * 字节转16进制数组 * @author lmiky * @date 2014-2-25 * @param buf * @return */ privatestaticString parseByte2HexStr(bytebuf[]) { StringBuffer sb =newStringBuffer(); for(inti =0; i < buf.length; i++) { String hex = Integer.toHexString(buf[i] &0xFF); if(hex.length() ==1) { hex =Ɔ'+ hex; } sb.append(hex); } returnsb.toString(); }
publicstaticvoidmain(String[] args)throwsException { System.out.println(AesECB.encrypt("fsadfsdafsdafsdafsadfsadfsadf","1eVRiqy7b9Uv7ZMM")); System.out.println(AesECB.decrypt("b123e2d9199598c0e3f1999dc9e723387b68e29d2b3a0d59fc7d5946c750c6b4","1eVRiqy7b9Uv7ZMM")); } } |
Node.js代码如下:
代码如下 | 复制代码 |
varcrypto = require('crypto'); exports.aes_algorithm ="aes-128-ecb"; exports.aes_secrect ="1eVRiqy7b9Uv7ZMM";
exports.encrypt =function(text) { varcipher = crypto.createCipher(this.aes_algorithm,this.aes_secrect) varcrypted = cipher.update(text,'utf8','hex') crypted += cipher.final('hex'); returncrypted; };
exports.decrypt =function(text) { vardecipher = crypto.createDecipher(this.aes_algorithm,this.aes_secrect) vardec = decipher.update(text,'hex','utf8') dec += decipher.final('utf8'); returndec; };
//var hw = this.encrypt("fsadfsdafsdafsdafsadfsadfsadf"); //console.log(hw); //console.log(this.decrypt(hw)); |
忍者必须死34399账号登录版 最新版v1.0.138v2.0.72
下载勇者秘境oppo版 安卓版v1.0.5
下载忍者必须死3一加版 最新版v1.0.138v2.0.72
下载绝世仙王官方正版 最新安卓版v1.0.49
下载Goat Simulator 3手机版 安卓版v1.0.8.2
Goat Simulator 3手机版是一个非常有趣的模拟游
Goat Simulator 3国际服 安卓版v1.0.8.2
Goat Simulator 3国际版是一个非常有趣的山羊模
烟花燃放模拟器中文版 2025最新版v1.0
烟花燃放模拟器是款仿真的烟花绽放模拟器类型单机小游戏,全方位
我的世界动漫世界 手机版v友y整合
我的世界动漫世界模组整合包是一款加入了动漫元素的素材整合包,
我的世界贝爷生存整合包 最新版v隔壁老王
我的世界MITE贝爷生存整合包是一款根据原版MC制作的魔改整