Exemple 1:
Ces jours-ci, j'ai été troublé par un problème. Le cryptage AES de Nodejs est incompatible avec le cryptage Java et C #. Bien sûr, cela ne sera pas décrypté. Je me débattais depuis longtemps: je ne pouvais toujours pas le faire plus tard. On dit en ligne que l'implémentation habituelle de Nodejs AES est différente des autres langues. Ok ~~ peut-être.
Module Crypto Nodejs.
La copie de code est la suivante:
var crypto = require ('crypto');
var data = "156156165152165156156";
console.log ('ClearText original:' + données);
var algorithme = 'aes-128-ecb';
var key = '78541561566';
var Clearencoding = 'utf8';
// var cipherencoding = 'hex';
// Si la ligne suivante n'est pas en train, le cleanxe final est faux.
var cipherencoding = 'Base64';
/ * cryptage * /
var cipher = crypto.createCipher (algorithme, key);
var cipherchunks = [];
cipherchunks.push (cipher.update (data, clearencoding, cipherencoding));
cipherchunks.push (cipher.Final (cipherencoding));
console.log (cipherencoding + 'cipherText:' + cipherchunks.join (''));
/ * Décryptage * /
var Deciffher = crypto.CreateDeCipher (algorithme, key);
var PlainChunks = [];
pour (var i = 0; i <cipherchunks.length; i ++) {
Plainchunks.push (Depiry.Update (cipherchunks [i], ciphérencodage, clearenccoding));
}
PlainChunks.push (Decipher.Final (Clearenccoding));
Console.log ("UTF8 PlainText décidé:" + PlainChunks.join (''));
En effet, c'est vrai ~~ Le cryptage et le décryptage sont réussis. Mais c'est différent de ceux cryptés dans Java et C #. Dieu. Je pense que tout le monde a du mal ici ~~, non? En fait, ajoutez simplement un vecteur et ce sera cohérent. Il y a trop peu de ressources recherchées en ligne. C'est juste que je lutte depuis si longtemps. Ok, le code correct est:
La copie de code est la suivante:
var crypto = require ('crypto');
var data = "156156165152165156156";
console.log ('ClearText original:' + données);
var algorithme = 'aes-128-ecb';
var key = '78541561566';
var Clearencoding = 'utf8';
var iv = "";
// var cipherencoding = 'hex';
// Si la ligne suivante n'est pas en train, le cleanxe final est faux.
var cipherencoding = 'Base64';
var cipher = crypto.createcipheriv (algorithme, key, iv);
var cipherchunks = [];
cipherchunks.push (cipher.update (data, clearencoding, cipherencoding));
cipherchunks.push (cipher.Final (cipherencoding));
console.log (cipherencoding + 'cipherText:' + cipherchunks.join (''));
var Deciffher = crypto.CreateDecipheriv (algorithme, key, iv);
var PlainChunks = [];
pour (var i = 0; i <cipherchunks.length; i ++) {
Plainchunks.push (Depiry.Update (cipherchunks [i], ciphérencodage, clearenccoding));
}
PlainChunks.push (Decipher.Final (Clearenccoding));
Console.log ("UTF8 PlainText décidé:" + PlainChunks.join (''));
Comparativement, il a été constaté que le chiffrement était cohérent. Ok, post ~~~ je te déteste, je me suis gaspillé ma journée.
Exemple 2:
Pendant le travail, j'ai rencontré le côté de Nodejs via AES, et le client Android Java a décrypté. Le côté doit crypter la clé ZA et MD5.
Nodejs:
La copie de code est la suivante:
/ **
* cryptage AES
* Données @param
* @param secretkey
* /
Encryptutils.aeSencrypt = fonction (data, SecretKey) {
var cipher = crypto.createecipher ('AES-128-ECB', SecretKey);
return cipher.update (data, 'utf8', 'hex') + cipher.final ('hex');
}
/ **
* Décryptage AES
* Données @param
* @param secretkey
* @returns {*}
* /
encryptutils.aesdecrypt = fonction (data, secretKey) {
var cipher = crypto.createedecipher ('AES-128-ECB', SecretKey);
return cipher.update (data, 'hex', 'utf8') + cipher.final ('utf8');
}
Java:
La copie de code est la suivante:
Package com.iofamily.util;
Importer Java.Security.MessagediGest;
import javax.crypto.cipher;
Importer javax.crypto.spec.SecretKeyspec;
/ **
* Cryptage AES, cohérent avec les nodejs
* @author lmiky
* @Date 2014-2-25
* /
classe publique aesfornodejs {
public static final String default_coding = "utf-8";
/ **
* Décryptage
* @author lmiky
* @Date 2014-2-25
* @param crypté
* @param semence
* @retour
* @throws exception
* /
Decrypt de chaîne statique privée (chaîne cryptée, graine de chaîne) lève l'exception {
Byte [] keyB = seed.getBytes (default_coding);
MessagediGest MD = MessagediGest.getInstance ("MD5");
octet [] thedigest = md.digest (keyB);
SecretKeyspec SKEY = new SecretKeyspec (Thedigest, "AES");
Cipher dcipher = cipher.getInstance ("aes");
dcipher.init (cipher.decrypt_mode, skey);
octet [] clearbyte = dcipher.dofinal (Tobyte (crypté));
return new String (ClearByte);
}
/ **
* Cryptage
* @author lmiky
* @Date 2014-2-25
* Contenu @param
* clé @param
* @retour
* @throws exception
* /
Public Static String Encrypt (String Content, String Key) lève l'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);
octet [] cipherText = new Byte [cipher.getOutputSize (input.length)];
int ctLength = cipher.update (entrée, 0, input.length, cipherText, 0);
ctLength + = Cipher.Dofinal (CipherText, ctLength);
return parsebyte2hexstr (CipherText);
}
/ **
* Chaîne à byte
* @author lmiky
* @Date 2014-2-25
* @param hexstring
* @retour
* /
octet statique privé [] Tobyte (chaîne hexstring) {
int len = hexstring.length () / 2;
octet [] résultat = nouveau octet [len];
pour (int i = 0; i <len; i ++) {
Résultat [i] = Integer.ValueOf (hexString.Substring (2 * i, 2 * i + 2), 16) .ByteValue ();
}
Résultat de retour;
}
/ **
* Byte à un tableau hexadécimal
* @author lmiky
* @Date 2014-2-25
* @param buf
* @retour
* /
chaîne statique privée 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) lève une exception {
System.out.println(AESForNodejs.encrypt("fsadfsdafsdafsdafsdafsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadf
System.out.println(AESForNodejs.decrypt("5b8e85b7a86ad15a275a7cb61fe4c0606005e8741f68797718a3e90d74b5092a", "1234fghjnmlkiuhA")); : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :::::::::::::::::::::::::::::::::::- :::::::::::::::::::::::::::::::::::- :::: ::
}
}