Toggle navigation
VA伐木累
社区
VA伐木累
Proxy
JSON
BASE64
MyGit
登录
注册
×
登录
邮箱
密码
忘记密码?
JAVA AES 和 NodeJS AES
•发布于
•作者
liuzy
•1812 次浏览
•最后一次编辑是
•来自
资讯
### JAVA ```java /** * 下载local_policy.jar和US_export_policy.jar * 替换%JAVA_HOME%/jre/lib/security下文件 */ public class AES128 { public static final String CHARSET = "UTF-8"; // JAVA默认AES算法 public static final String ALGORITHM = "AES/ECB/PKCS5Padding"; public static void main(String[] args) throws Exception { String data = "liuzy"; String key = "123"; System.out.println(encrypt(data, key));// b940e9df81cdeca66de4d6297a2f08af System.out.println(decrypt(encrypt(data, key), key)); } public static String encrypt(String data, String key) throws GeneralSecurityException, IOException { SecretKeySpec keySpec = new SecretKeySpec(md5(key), "AES"); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, keySpec); cipher.update(data.getBytes(CHARSET)); byte[] dataBytes = cipher.doFinal(); return toHex(dataBytes); } private static String decrypt(String data, String key) throws GeneralSecurityException, IOException { SecretKeySpec keySpec = new SecretKeySpec(md5(key), "AES"); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, keySpec); cipher.update(fromHex(data)); byte[] dataBytes = cipher.doFinal(); return new String(dataBytes, CHARSET); } public static byte[] md5(String data) throws NoSuchAlgorithmException, IOException { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(data.getBytes(CHARSET)); return digest.digest(); } public static byte[] fromHex(String hex) { byte[] bytes = new byte[hex.length() / 2]; for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16); } return bytes; } public static String toHex(byte[] bytes) { BigInteger bi = new BigInteger(1, bytes); String hex = bi.toString(16); int paddingLength = (bytes.length * 2) - hex.length(); if (paddingLength > 0) return String.format("%0" + paddingLength + "d", 0) + hex; else return hex; } } ``` ### NodeJS ```js var crypto = require('crypto'); function md5(data) { return crypto.createHash('md5').update(data).digest('hex'); } function en128(data, key) { var cipher = crypto.createCipher('aes-128-ecb', key); return cipher.update(data, 'utf8', 'hex') + cipher.final('hex'); } function de128(data, key) { var cipher = crypto.createDecipher('aes-128-ecb', key); return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8'); } function en256(data, key) { var cipher = crypto.createCipher('aes-256-ecb', key); return cipher.update(data, 'utf8', 'hex') + cipher.final('hex'); } function de256(data, key) { var cipher = crypto.createDecipher('aes-256-ecb', key); return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8'); } function en128md5(data, key) { var cipher = crypto.createCipher('aes-128-ecb', md5(key)); return cipher.update(data, 'utf8', 'hex') + cipher.final('hex'); } function de128md5(data, key) { var cipher = crypto.createDecipher('aes-128-ecb', md5(key)); return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8'); } function en256md5(data, key) { var cipher = crypto.createCipher('aes-256-ecb', md5(key)); return cipher.update(data, 'utf8', 'hex') + cipher.final('hex'); } function de256md5(data, key) { var cipher = crypto.createDecipher('aes-256-ecb', md5(key)); return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8'); } function en128vi(data, key) { var cipher = crypto.createCipheriv('aes-128-ecb', key, ''); cipher.setAutoPadding(true); return cipher.update(data, 'utf8', 'hex') + cipher.final('hex'); } function de128vi(data, key) { var cipher = crypto.createDecipheriv('aes-128-ecb', key, ''); cipher.setAutoPadding(true); return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8'); } var data = 'liuzy', key = '123'; console.log('en128\t=>\t' + en128(data, key));// b940e9df81cdeca66de4d6297a2f08af console.log('de128\t=>\t' + de128(en128(data, key), key));// yes console.log('en256\t=>\t' + en256(data, key));// c3287900482609282bd622f94d61d241 console.log('de256\t=>\t' + de256(en256(data, key), key));// yes console.log('en128md5\t=>\t' + en128md5(data, key));// 6828223d5e4850d053b6b3566b60a586 console.log('de128md5\t=>\t' + de128md5(en128md5(data, key), key));// yes console.log('en256md5\t=>\t' + en256md5(data, key));// 4330ba388a1cf9c9e391d02af9591e62 console.log('de256md5\t=>\t' + de256md5(en256md5(data, key), key));// yes // console.log('en128vi\t=>\t' + en128vi(data, key));// error ```
1 回复
liuzy
### JAVA AES/ECB/PKCS7Padding ```java /** * 下载local_policy.jar和US_export_policy.jar * 替换%JAVA_HOME%/jre/lib/security下文件 * 需要bcprov-jdk*.jar */ public class AES256 { public static final String CHARSET = "UTF-8"; // 使用此算法需要导入bcprov-jdk*.jar public static final String ALGORITHM = "AES/ECB/PKCS7Padding"; static { Security.addProvider(new BouncyCastleProvider()); } public static void main(String[] args) throws GeneralSecurityException, IOException { String data = "liuzy"; String key = "123"; System.out.println(encrypt(data, key));// b940e9df81cdeca66de4d6297a2f08af System.out.println(decrypt(encrypt(data, key), key)); } public static String encrypt(String data, String key) throws GeneralSecurityException, IOException { SecretKeySpec keySpec = new SecretKeySpec(md5(key), "AES"); Cipher cipher = Cipher.getInstance(ALGORITHM, "BC"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); byte[] bytes = cipher.doFinal(data.getBytes(CHARSET)); return toHex(bytes); } public static String decrypt(String data, String key) throws GeneralSecurityException, IOException { SecretKeySpec keySpec = new SecretKeySpec(md5(key), "AES"); Cipher cipher = Cipher.getInstance(ALGORITHM, "BC"); cipher.init(Cipher.DECRYPT_MODE, keySpec); byte[] bytes = cipher.doFinal(fromHex(data)); return new String(bytes, CHARSET); } public static byte[] md5(String data) throws NoSuchAlgorithmException, IOException { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(data.getBytes(CHARSET)); return digest.digest(); } public static byte[] fromHex(String hex) { byte[] bytes = new byte[hex.length() / 2]; for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16); } return bytes; } public static String toHex(byte[] bytes) { BigInteger bi = new BigInteger(1, bytes); String hex = bi.toString(16); int paddingLength = (bytes.length * 2) - hex.length(); if (paddingLength > 0) return String.format("%0" + paddingLength + "d", 0) + hex; else return hex; } } ```
作者
liuzy
积分: 841
“ 黑眼圈圈男 ”
无人回复话题
SonarQube 9.4 + PostgreSQL
shell倒计时
日常网络巧技
使用ssh创建socks5代理服务
NodeJS集群demo
作者其他话题
SonarQube 9.4 + PostgreSQL
shell倒计时
日常网络巧技
使用ssh创建socks5代理服务
NodeJS集群demo
回到顶部
友情链接:
JFinal
©2015 Powered by
jfinalbbs
沪ICP备15012258号