最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Java与Node.js利用AES加密解密出相同结果的方法示例
时间:2022-06-29 01:36:26 编辑:袖梨 来源:一聚教程网
前言
工作中遇到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)); |
相关文章
- 时空中的绘旅人天宇之间怎么玩 绘旅人天宇之间活动玩法介绍 12-25
- QQ2024年度报告怎么看 2024qq年度报告玩法介绍 12-25
- 归龙潮珠砂什么时候up 归龙潮红缘绮梦卡池介绍 12-25
- 王者荣耀S38赛季有什么更新 12-25
- 王者荣耀S38赛季有什么更新 王者荣耀S38赛季更新内容介绍 12-25
- 世界之外12.25有什么更新 世界之外12月25日更新内容介绍 12-25