博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java and Nodejs on AES
阅读量:4073 次
发布时间:2019-05-25

本文共 3156 字,大约阅读时间需要 10 分钟。

转载:http://srcode.org/2014/05/07/java-and-nodejs-on-aes/

Java and Nodejs on AES

If you are like me, having a legacy Java system, and developing a new Nodejs system. Somehow the new Nodejs system needs to talk to the Java based authentication system doing encryption or decryption. Here are the Java and Nodejs code using the same AES algorithm, which will enable the Java and Nodejs systems to talk to each other.

Java code:

AES.java
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
import java.security.MessageDigest;import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import javax.xml.bind.DatatypeConverter;public class AES {
private static byte[] iv = "0000000000000000".getBytes(); private static String decrypt(String encrypted, String seed) throws Exception {
byte[] keyb = seed.getBytes("utf-8"); MessageDigest md = MessageDigest.getInstance("SHA-256"); byte[] thedigest = md.digest(keyb); SecretKeySpec skey = new SecretKeySpec(thedigest, "AES"); Cipher dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); dcipher.init(Cipher.DECRYPT_MODE, skey, new IvParameterSpec(iv)); byte[] clearbyte = dcipher.doFinal(DatatypeConverter .parseHexBinary(encrypted)); return new String(clearbyte); } public static String encrypt(String content, String key) throws Exception {
byte[] input = content.getBytes("utf-8"); MessageDigest md = MessageDigest.getInstance("SHA-256"); byte[] thedigest = md.digest(key.getBytes("utf-8")); SecretKeySpec skc = new SecretKeySpec(thedigest, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skc, new IvParameterSpec(iv)); byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; int ctLength = cipher.update(input, 0, input.length, cipherText, 0); ctLength += cipher.doFinal(cipherText, ctLength); return DatatypeConverter.printHexBinary(cipherText); } public static void main(String[] args) throws Exception {
String data = "hello"; String key = "hi"; String cipher = AES.encrypt(data, key); String decipher = AES.decrypt(cipher, key); System.out.println(cipher); System.out.println(decipher); }}

Compile and run this Java program:

1234
$ javac AES.java $ java AESDD0D07B77C1606C97A1B10AC8D9AC180hello

Node code:

aes.js
123456789101112131415161718192021
var crypto = require('crypto');var iv = new Buffer('0000000000000000');var encrypt = function(data, key) {
var decodeKey = crypto.createHash('sha256').update(key, 'utf-8').digest(); var cipher = crypto.createCipheriv('aes-256-cbc', decodeKey, iv); return cipher.update(data, 'utf8', 'hex') + cipher.final('hex');};var decrypt = function(data, key) {
var encodeKey = crypto.createHash('sha256').update(key, 'utf-8').digest(); var cipher = crypto.createDecipheriv('aes-256-cbc', encodeKey, iv); return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8');};var data = 'hello'var key = 'hi';var cipher = encrypt(data, key);var decipher = decrypt(cipher, key);console.log(cipher);console.log(decipher);

Run the Node program:

123
$ node aes.js dd0d07b77c1606c97a1b10ac8d9ac180hello

你可能感兴趣的文章
hd disk / disk raid / disk io / iops / iostat / iowait / iotop / iometer
查看>>
project ASP.NET
查看>>
db db2_monitorTool IBM Rational Performace Tester
查看>>
OS + Unix Aix telnet
查看>>
IBM Lotus
查看>>
Linux +Win LAMPP Tools XAMPP 1.7.3 / 5.6.3
查看>>
my read_university
查看>>
network manager
查看>>
OS + Linux Disk disk lvm / disk partition / disk mount / disk io
查看>>
RedHat + OS CPU、MEM、DISK
查看>>
net TCP/IP / TIME_WAIT / tcpip / iperf / cain
查看>>
webServer kzserver/1.0.0
查看>>
hd printer lexmark / dazifuyin / dayin / fuyin
查看>>
OS + Unix IBM Aix basic / topas / nmon / filemon / vmstat / iostat / sysstat/sar
查看>>
my ReadMap subway / metro / map / ditie / gaotie / traffic / jiaotong
查看>>
OS + Linux DNS Server Bind
查看>>
linux下安装django
查看>>
Android 解决TextView设置文本和富文本SpannableString自动换行留空白问题
查看>>
最完整的Java IO流学习总结
查看>>
Android开发中Button按钮绑定监听器的方式完全解析
查看>>