AES front-end JS back-end nodeJs to achieve encryption and decryption; personal understanding

 

// Description: 
// 1. If encryption and decryption involve the front-end and back-end, the key here must be the same as the back-end key 
// 2. There are several AES algorithm modes (ECB, CBC, CFB, OFB) , So it must be consistent with the backend 
// 3. There are two ways of complementing AES (PKS5, PKS7), so it must also be consistent with the backend 
// 4. There are three AES key lengths (128,192,256, default It is 128), so it must be consistent with the back end 
// 5. There are two encoding methods for AES encryption results (base64 and hexadecimal). The specific choice is up to you. ////One
// Backend node js code:

const
 express  =   require ( 'express' );   // Call module
const app  =  express();
const  server  =  app . listen ( 3002 , function () {console . log ( 'Service started successfully!' );});
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>


 

Guess you like

Origin www.cnblogs.com/lkkk/p/12672382.html
Recommended