您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關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));
以上就是Java和Node.js如何使用AES加密解密方法,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。