前端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(
req
res
next) {   
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 (
req
res) {      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
.
log
encryption(
‘哈哈‘) ); console
.
log
decrypt(
“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
.
log
encrypt(
‘哈哈‘) ) console
.
log
decrypt(
‘1SYQyczGb5L4qmVybCV82g==‘) )

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