AES前端JS后端nodeJs 實現加解密; 個人理解


 

//說明:
//  1.如果加密解密涉及到前端和后端,則這里的key要保持和后端的key一致
//  2.AES的算法模式有好幾種(ECB,CBC,CFB,OFB),所以也要和后端保持一致
//  3.AES的補碼方式有兩種(PKS5,PKS7),所以也要和后端保持一致
//  4.AES的密鑰長度有三種(128,192,256,默認是128),所以也要和后端保持一致
//  5.AES的加密結果編碼方式有兩種(base64和十六進制),具體怎么選擇由自己定,但是加密和解密的編碼方式要統////一
// 后端node js代碼:

const
 express  =  require( 'express');   //調用模塊
const app  =  express();
const server  = app . listen( 3002, function(){console . log( '服務啟動成功!');});
const crypto  =  require( 'crypto');
app . all( '*'function( reqresnext) {
   res . header( "Access-Control-Allow-Origin""*");
   res . header( "Access-Control-Allow-Headers""X-Requested-With");
   res . header( "Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
   res . header( "X-Powered-By", ' 3.2.1')
   res . header( "Content-Type""text/plain");
   next();
});
app . get( '/'function ( reqres) { 
    console . log( 111)
     res . send( '1111')
    });
/**
 * AES加密的配置 
 * 1.密鑰 
 * 2.偏移向量 
 * 3.算法模式CBC 
 * 4.補全值
 */
var AES_conf  = {
    key:  getSecretKey(),  //密鑰
    iv:  '1012132405963708'//偏移向量
    padding:  'PKCS7Padding'  //補全值 需和前端padding保持一致
}

/**
 * 讀取密鑰key
 * 更具當前客戶端的版本vid、平台platform獲取對應的key
 */
function  getSecretKey(){
     return  "46cc793c53dc451b";
}

/**
 * AES_128_CBC 加密 
 * 128位 
 * return base64
 */
function  encryption( data) {
     let key  = AES_conf .key;
     let iv  = AES_conf .iv;
     // let padding = AES_conf.padding;

     var cipherChunks  = [];
     var cipher  = crypto . createCipheriv( 'aes-128-ECB', key,  '');
    cipher . setAutoPadding( true);
    cipherChunks . push(cipher . update( data'utf8''base64'));
    cipherChunks . push(cipher . final( 'base64'));
     return cipherChunks . join( '');
}


/**
 * 解密
 * return utf8
 */
function  decryption( data){
     let key  = AES_conf .key;
     let iv  = AES_conf .iv;
     var cipherChunks  = [];
     var decipher  = crypto . createDecipheriv( 'aes-128-ECB', key,  '');
    decipher . setAutoPadding( true);
    cipherChunks . push(decipher . update( data'base64''utf8'));
    cipherChunks . push(decipher . final( 'utf8'));
     return cipherChunks . join( '');
};

let  decrypt =( data) =>{
     let key  =  '46cc793c53dc451b';
     let decipher  = crypto . createDecipheriv( 'aes-128-ecb', key, "");
     const buf1  =  new  Buffer( data, "base64") . toString( 'hex');
     let decrypted  = decipher . update(buf1,  'hex''utf8');
    decrypted  += decipher . final( 'utf8');
     return decrypted;
  };
let  encrypt  = ( data) =>{
     let key  =  '46cc793c53dc451b';
     let crypted = '';
     let cipher  = crypto . createCipheriv( 'aes-128-ecb', key,  "");
    crypted  = cipher . update( data'utf8''binary');
    crypted  += cipher . final( 'binary');
    crypted  =  new  Buffer(crypted,  'binary') . toString( 'base64');
     return crypted;
  }

console . log( encrypt( '哈哈') ); 
console . logencryption( '哈哈') );
console . logdecrypt( "1SYQyczGb5L4qmVybCV82g==") );
console . log(   decryption( "1SYQyczGb5L4qmVybCV82g=="));
 
 
 
前端代碼:
 
<! DOCTYPE  html>
< html  lang= "en">
< head>
    < meta  charset= "UTF-8">
    < meta  name= "viewport"  content= "width=device-width, initial-scale=1.0">
    < title>Document</ title>
    < script  src= "https://cdn.bootcss.com/crypto-js/3.3.0/crypto-js.min.js"></ script>
     <!-- <script src="https://cdn.bootcss.com/crypto-js/3.3.0/aes.js"></script> -->

    
</ head>
< body>
    < script>
function  encrypt( word){
     var key  = CryptoJS .enc .Utf8 . parse( "46cc793c53dc451b");
     var srcs  = CryptoJS .enc .Utf8 . parse( word);
     var encrypted  = CryptoJS .AES . encrypt(srcs, key, {
         mode: CryptoJS .mode .ECB,
         padding: CryptoJS .pad .Pkcs7 //和后端pkcs7 一致
    });
     return encrypted . toString();
}

function  decrypt( word){
     var key  = CryptoJS .enc .Utf8 . parse( "46cc793c53dc451b");
     var decrypt  = CryptoJS .AES . decrypt( word, key, {
         mode: CryptoJS .mode .ECB,
         padding: CryptoJS .pad .Pkcs7
    });
     return CryptoJS .enc .Utf8 . stringify(decrypt) . toString();
}
console . logencrypt( '哈哈') )
console . logdecrypt( '1SYQyczGb5L4qmVybCV82g==') )

       </ script>
</ body>
</ html>


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM