Example one:
These days have been plagued with a problem. Nodejs AES Encryption and java,c# encryption out of the inconsistency. Of course, this will not be decrypted. Tangled for a long time: Later or really die, looked at the source code, or else it has to continue to tangle down. On the Internet, it is common to say that Nodejs AES is different from other language implementations. Well, maybe.
Nodejs's crypto module.
Copy Code code as follows:
var crypto = require (' crypto ');
var data = "156156165152165156156";
Console.log (' Original cleartext: ' + data);
var algorithm = ' AES-128-ECB ';
var key = ' 78541561566 ';
var clearencoding = ' UTF8 ';
var cipherencoding = ' hex ';
If the next line are uncommented, the final cleartext is wrong.
var cipherencoding = ' base64 ';
/* Encryption */
var cipher = Crypto.createcipher (algorithm, key);
var cipherchunks = [];
Cipherchunks.push (cipher.update (data, clearencoding, cipherencoding));
Cipherchunks.push (Cipher.final (cipherencoding));
Console.log (cipherencoding + ' ciphertext: ' + cipherchunks.join ("));
/* Decryption */
var decipher = Crypto.createdecipher (algorithm, key);
var plainchunks = [];
for (var i = 0;i < cipherchunks.length;i++) {
Plainchunks.push (Decipher.update (Cipherchunks[i], cipherencoding, clearencoding));
}
Plainchunks.push (Decipher.final (clearencoding));
Console.log ("UTF8 plaintext deciphered:" + plainchunks.join ('));
True, yes ~ ~ Encryption decryption succeeded. But it's not the same as the java,c# encryption. Oh, God. I think, we are all entangled in here ~ ~ Right. In fact, just add a vector, you can and consistent. There are too few resources on the Internet to search out. Just let oneself struggle for so long. All right
the correct code is:
Copy Code code as follows:
var crypto = require (' crypto ');
var data = "156156165152165156156";
Console.log (' Original cleartext: ' + data);
var algorithm = ' AES-128-ECB ';
var key = ' 78541561566 ';
var clearencoding = ' UTF8 ';
var IV = "";
var cipherencoding = ' hex ';
If the next line are uncommented, the final cleartext is wrong.
var cipherencoding = ' base64 ';
var cipher = Crypto.createcipheriv (algorithm, KEY,IV);
var cipherchunks = [];
Cipherchunks.push (cipher.update (data, clearencoding, cipherencoding));
Cipherchunks.push (Cipher.final (cipherencoding));
Console.log (cipherencoding + ' ciphertext: ' + cipherchunks.join ("));
var decipher = Crypto.createdecipheriv (algorithm, KEY,IV);
var plainchunks = [];
for (var i = 0;i < cipherchunks.length;i++) {
Plainchunks.push (Decipher.update (Cipherchunks[i], cipherencoding, clearencoding));
}
Plainchunks.push (Decipher.final (clearencoding));
Console.log ("UTF8 plaintext deciphered:" + plainchunks.join ('));
The comparison found that the encryption is consistent. OK, knot paste ~ ~ ~ I hate you, wasted my day.
Example two:
Work encountered Nodejs through AES encryption, Android client Java decryption, agreed to Nodejs also need to decrypt Android client encrypted content, found two encryption results are different, query data found Java end need to key za again MD5 encryption, the following is AES ECB encrypted content, if it is CBC also need to encrypt secret key MD5:
Nodejs:
Copy Code code as follows:
/**
* 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:
Copy Code code as follows:
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[] 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). Bytevalue ();
}
return result;
}
/**
* Byte to 16 binary array
* @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 (Aesfornodejs.encrypt ("FSADFSDAFSDAFSDAFSADFSADFSADF", "1234fghjnmlkiuhA"));
System.out.println (Aesfornodejs.decrypt) ("5b8e85b7a86ad15a275a7cb61fe4c0606005e8741f68797718a3e90d74b5092a", " 1234fghjnmlkiuhA "));
}
}