首頁 >web前端 >js教程 >Node.js中AES加密和其它語言不一致問題解決方案_javascript技巧

Node.js中AES加密和其它語言不一致問題解決方案_javascript技巧

WBOY
WBOY原創
2016-05-16 16:56:151669瀏覽

例子一:

這幾天被一個問題困擾著。 Nodejs的AES加密和Java,C#加密出來的不一致。當然,這樣就不能解密了。糾結了許久:後來還是實在不行了,看了下原始碼,要不然還得繼續糾結下去。網路上說,通常的nodejs AES和其他語言實作不一樣。好~~或許吧。
nodejs的crypto模組。

複製程式碼 程式碼如下:

var crypto = require('crypto') ;

    var data = "156156165152165156156";
    console.log('Original cleartext: ' data);
  8541561566';
    var clearEncoding = 'utf8';
    //var cipherEncoding = 'hex';
    //If the next line is uncommented, the fipan> an 素🎜>/*加密*/
    var cipher = crypto.createCipher(algorithm, key);

    var cipherChunks = [];
    cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));

H . sole.log( cipherEncoding ' ciphertext: ' cipherChunks.join(''));

/*解密*/
    var decipher = crypto.createDecipher(algorithm, key);
= var (var i = 0;i       plainChunks.push(decipher.update(cipherChunks[i], cipherEncoding, clearEncoding));

    }
    plainChunks.push(decipher.final(clearEncoding));
    console.log("UTF8 plaintext deciphered: "YChunks.join(''l.錯~~加密解密成功。但是和java,C#中加密出來的不一樣。神啊。我想,大家都在這裡糾結著吧~~對不對。其實只要加個向量,就可以和一致了。網路上搜尋出來的資源太少。才讓自己糾結那麼久。好吧,
正確代碼是:




複製代碼
代碼如下:
var crypto = require('crypto');     var data = "156156165152165156156";    console.log('Original cleartext: ' data);  8541561566';
    var clearEncoding = 'utf8';
    var iv = "";
    //var cipherEncoding = 'hex';    //var cipherEncoding = 'hex';

    //var cipher恩>    var cipherEncoding = 'base64';
    var cipher = crypto.createCipheriv(algorithm, key,iv);

    var cipherChunks = [];
    cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));
H . sole.log( cipherEncoding ' ciphertext: ' cipherChunks.join(''));

    var decipher = crypto.createDecipheriv(algorithm, key,iv);
    var plainChunks = [];
    for (var i = 009.p. s. push(decipher.update(cipherChunks[i], cipherEncoding, clearEncoding));

    }
    plainChunks.push(decipher.final(clearEncoding));
    console.log("UTF8 plaintext deciphered: "Hyk 使用,加密出來是一致的。好吧,結貼~~~我討厭你,浪費了我一天。


範例二:

工作上遇到nodejs端透過aes加密,安卓客戶端java解密,同意nodejs也需要解密安卓客戶端加密過來的內容,發現兩個加密結果不一樣,查詢資料發現java端需要對密鑰za再MD5加密一遍,以下是aes ecb加密的內容,如果是cbc也同樣需要對秘鑰MD5加密:



nodejs:



複製程式碼

程式碼如下:


/**
 * aes加密
 * @param data
 * @param secretKey
 */ 
encryptUtils.aesEncrypt = function(data, SecretKey) { 
    var cipher = crypto.createCipher('aes-1283); 
    return cipher.update(data,'utf8','hex') cipher.final('hex'); 


/**
 * aes解密
 * @param data
 * @param secretKey
 * @returns {*}
 */ 

/***/ 
encryptUtils.aesDecrypt = function(data, SecretKey) { 
    var cipher = crypto.create.煞); 
    return cipher.update(data,'hex','utf8') cipher.final('utf8'); 


javajavajava複製程式碼
程式碼如下:

套件com.iofamily.util; 

導入java.security.MessageDigest; 

導入javax.crypto.Cipher;ret

導入javax.Secypto.Cipher;ret


/**
 * AES加密,與Nodejs 保持一致
 * @author lmiky
 * @date 2014-2-25
 */ 
public class AESForNodejs { 
    public static Final String DEFAULT_CODING = "utf-8"**.     私人靜態字串解密(字串加密,字串種子)拋出例外{ 
        byte[] keyb = Seed.getBytes(DEFAULT_CODING); ("MD5" ); 
        byte[] thedigest = md.digest(keyb); 
        Secret” .getInstance("AES"); 
        dcipher.init(Cipher.DECRYPT_MODE, skey); 

        byte[ clearbyte); 
    } 

    /**
     * 解密
     * @author lmiky
     * @date 2014-2-25
  turn
     * @throws Exception
    */ 
    public static String encrypt(String content, String key) throws Exception { 
get) in. >
MessageDigest md = MessageDigest.getInstance("MD5"); 
        byte[] thedigest = md.digest(key.getBytes(DEFAULT_COcING)); , "AES"); 
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 
        cipher.in [] cipherText = new byte[cipher.getOutputSize(input .length)]; 
        int ctLength = cipher.update(input, 0, input.length, cipherText, 0); 
>
        return parseByte2HexStr (cipherText); 
    } 

    /**
     * 加密
     * @author lmiky
     * @date 2014-2-25
  turn
     * @throws 異常
    */ 
    private stat遠th() / 2;
        byte[] 結果= new byte[len]; 
        for (int i = 0; i         } 
        回傳結果; 
        

    /**
     * 位元組轉16進位陣列
     * @author lmiky
     * @date 2014-2-25     * @date 2014-2-25     * @date 2014-2-25     */ 
    private static String parseByte2HexStr(byte buf[]) { 
 
        for (int i = 0; i             String hex = Integer.HexString(buftoHex. 
            if (hex.length() == 1) { 
            
            } 
           ,b.append(hex); 
        } 
        return sb.toString(); 
    } 

    public static void main(String[] args) throws Exception { 
      jnmlkiuhA")); 
        System.out.println(AESForNodejs.decrypt("5b8e85b7a86ad15a275a7cb61fe4c0606005e8741f6879771823e4c060005e 
    } 
}

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn