例1:
最近、私は問題に悩まされています。 nodejsのAES暗号化は、JavaおよびC#暗号化と矛盾しています。もちろん、これは復号化されません。私は長い間苦労していました。後でそれを見ることができませんでした。通常のNodeJS AESの実装は他の言語とは異なるとオンラインで言われています。 OK ~~多分。
nodejs cryptoモジュール。
コードコピーは次のとおりです。
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';
//次の行がcomementmentメントなしである場合、最終的なClearextが間違っています。
var cipherencoding = 'base64';
/*暗号化*/
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( ''));
/*復号化*/
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 plantextが決定しました:" + plainChunks.join( ''));
確かに、それは正しい~~暗号化と復号化が成功しています。しかし、それはJavaとC#で暗号化されたものとは異なります。神。誰もがここで苦労していると思います~~そうですか?実際、ベクトルを追加するだけで、一貫性があります。オンラインで検索されるリソースが少なすぎます。それは私が長い間苦労してきたというだけです。 OK、正しいコードは次のとおりです。
コードコピーは次のとおりです。
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';
//次の行がcomementmentメントなしである場合、最終的なClearextが間違っています。
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 plantextが決定しました:" + plainChunks.join( ''));
それに比べて、暗号化が一貫していることがわかりました。 OK、投稿~~~私はあなたが嫌い、私の一日を無駄にしました。
例2:
作業中、私はAESを介して暗号化されたNodejsの側面に遭遇しましたサイドは、キーZAとMD5を暗号化する必要があります。以下は、CBCのコンテンツです。
nodejs:
コードコピーは次のとおりです。
/**
* AES暗号化
* @paramデータ
* @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復号化
* @paramデータ
* @Param SecretKey
* @returns {*}
*/
encryptutils.aesdecrypt = function(data、secretkey){
var cipher = crypto.createdecipher( 'aes-128-ecb'、secretkey);
cipher.update(data、 'hex'、 'utf8') + cipher.final( 'utf8')を返します。
}
Java:
コードコピーは次のとおりです。
パッケージcom.iofamily.util;
java.security.messagedigestをインポートします。
javax.crypto.cipherをインポートします。
javax.crypto.spec.secretkeyspecをインポートします。
/**
* AES暗号化、nodejsと一致しています
* @author lmiky
* @date 2014-2-25
*/
パブリッククラスaesfornodejs {
public static final string default_coding = "utf-8";
/**
*復号化
* @author lmiky
* @date 2014-2-25
* @param暗号化
* @paramシード
* @戻る
* @Throws例外
*/
private static string decrypt(string necrypted、string seed)は例外をスローします{
byte [] keyb = seed.getBytes(default_coding);
MESSAGEDGEST MD = MESSAGEDGEST.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(necrypted));
新しい文字列(ClearByte)を返します。
}
/**
*暗号化
* @author lmiky
* @date 2014-2-25
* @paramコンテンツ
* @paramキー
* @戻る
* @Throws例外
*/
public static string encrypt(string content、string key)スロー例外{
byte [] input = content.getBytes(default_coding);
MESSAGEDGEST MD = MESSAGEDGEST.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);
parsebyte2hexstr(ciphertext)を返します。
}
/**
*文字列からバイト配列
* @author lmiky
* @date 2014-2-25
* @Param HexString
* @戻る
*/
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)、16).bytevalue();
}
返品結果;
}
/**
*バイトからヘキサデシマルアレイ
* @author lmiky
* @date 2014-2-25
* @param buf
* @戻る
*/
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)スロー例外{
System.out.println(AESForNodejs.encrypt("fsadfsdafsdafsdafsdafsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadfsadf
System.out.println(aesfornodejs.decrypt( "5B8E85B7A86AD15A275A7CB61FE4C06005E8741F68797718A3E90D74B5092A"、 " :::::::: :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::づ 火::::: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::づ品です額です
}
}