Work encountered Nodejs through AES encryption, Android client Java decryption, the same nodejs also need to decrypt Android client encrypted content, found that two encryption results are not the same, query data found that the Java end needs to MD5 encryption, the following is the AES ECB encrypted content, If it is CBC, it also needs to encrypt the secret key MD5:
Nodejs:
/**
* AES Encryption
* @param data
* @param secretkey
/encryptutils.aesencrypt = function (data, Secretkey ) {
var cipher = crypto.createcipher (' AES-128-ECB ', secretkey);
return cipher.update (data, ' UTF8 ', ' hex ') + cipher.final (' hex ');
}
/**
* AES Decryption
* @param data
* @param secretkey
* @returns {*}
/
Encryptutils.aesdecrypt = function (data, secretkey) {
var cipher = crypto.createdecipher (' AES-128-ECB ', secretkey);
return cipher.update (data, ' hex ', ' UTF8 ') + cipher.final (' UTF8 ');
}
Java:
Package com.iofamily.util;
Import Java.security.MessageDigest;
Import Javax.crypto.Cipher;
Import Javax.crypto.spec.SecretKeySpec; /** * AES encryption, consistent with Nodejs * @author lmiky * @date 2014-2-25 * * * public class Aesfornodejs {public static final String
default_coding = "Utf-8";
/** * Decryption * @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);
/** * Encryption * @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); /** * String byte array * @author Lmiky * @date 2014-2-25 * @param hexstring * @return/private static byte[] t
Obyte (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), Bytevalue (); } return Result /** * Byte to 16 binary array * @author lmiky * @date 2014-2-25 * @param buf * @return/private static String Parseb
YTE2HEXSTR (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 (Aesfornodejs.encrypt ("Fsadfsdafsdafsda
FSADFSADFSADF "," 1234fghjnmlkiuhA ")); System.out.println (Aesfornodejs.decrypt) ("5b8e85b7a86ad15a275a7cb61fe4c0606005e8741f68797718a3e90d74b5092a", "
1234fghjnmlkiuhA "));
}
}