使用nodejs测试gzip压缩成字节数组后,再经过AES加密,传输给后端java服务器
test.js
var gzipaes = require("./gzipAesUtil")
let key = "28162263058102460902861775485335";
let message = "{\"title\":\"comewords\",\"content\":\"测试\"}";
let enc = gzipaes.aesEcbPkcs7Encrypt(gzipaes.gzip(message), key)
let data = gzipaes.gunzip(gzipaes.aesEcbPkcs7Decrypt(enc, key));
console.log(data)
运行
> node test.js
enc加密后得到的base64字符串
打印:gzipaes.aesEcbPkcs7Encrypt方法里的encrypted.toString()即可得到
yHzYi4bmHWWQUL16ORTIkXGinW0enKS/TovttudMUZRXgQKfWPmwn+9oUEeQlzDRb7OkytVQ8pNBEOXAY4xHDg==
gzipAesUtil.js
const CryptoJS = require("crypto-js");
const pako = require("pako");
var GZIPAES = {
// 构建WordArray对象
buildWordArray: function (u8arr) {
var len = u8arr.length;
var words = [];
for (var i = 0; i < len; i++) {
words[i >>> 2] |= (u8arr[i] & 0xff) << (24 - (i % 4) * 8);
}
return CryptoJS.lib.WordArray.create(words, len);
},
wordArrayToInt8: function (array, length=array.length*4) {
var bin = new Int8Array(Math.min(array.length * 4, length));
for (var i = 0; i < array.length; i++) {
var num = array[i];
if (num < 0) {
num = array[i] + 0x100000000;
}
bin[i * 4 + 0] = (num >>> 24) & 0xFF;
bin[i * 4 + 1] = (num >>> 16) & 0xFF;
bin[i * 4 + 2] = (num >>> 8) & 0xFF;
bin[i * 4 + 3] = (num >>> 0) & 0xFF;
}
return bin;
},
wordArrayToUint8: function (array, length=array.length*4) {
var bin = new Uint8Array(Math.min(array.length * 4, length));
for (var i = 0; i < array.length; i++) {
var num = array[i];
if (num < 0) {
num = array[i] + 0x100000000;
}
bin[i * 4 + 0] = (num >>> 24) & 0xFF;
bin[i * 4 + 1] = (num >>> 16) & 0xFF;
bin[i * 4 + 2] = (num >>> 8) & 0xFF;
bin[i * 4 + 3] = (num >>> 0) & 0xFF;
}
return bin;
},
base64ToArrayBuffer: function (encrypted) {
var data = CryptoJS.enc.Base64.parse(encrypted.toString());
// console.log("==========>>>", data.sigBytes);
data = this.wordArrayToUint8(data.words);
// console.log("===api===>>>data=",data);
return data;
},
aesEcbPkcs7Encrypt: function (srcdata, aeskey) {
var data = this.buildWordArray(srcdata);//加密字符串
// var key = this.buildWordArray(aeskey);//密钥
let key= CryptoJS.enc.Utf8.parse(aeskey);
//加密
//var data = JSON.stringify(data);//将数据对象转换为json字符串
var encrypted = CryptoJS.AES.encrypt(data, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
var retArray = new Int8Array(this.base64ToArrayBuffer(encrypted));
/*console.log("aesEcbPkcs7Encrypt======>>>")
console.log(srcdata.length +", ["+Array.from(srcdata).join(",")+"]")
console.log(encrypted.ciphertext.toString());
console.log(encrypted.toString());
console.log(retArray.length +", ["+Array.from(retArray).join(",")+"]")
console.log("aesEcbPkcs7Encrypt<<<======")*/
console.log(encrypted.toString());
return retArray;
},
aesEcbPkcs7Decrypt: function (aesdata, aeskey) {
var data = this.buildWordArray(aesdata);//加密字符串
// var key = this.buildWordArray(aeskey);//密钥
let key= CryptoJS.enc.Utf8.parse(aeskey);
//加密
//var data = JSON.stringify(data);//将数据对象转换为json字符串
var base64Str = CryptoJS.enc.Base64.stringify(data);
var decrypted = CryptoJS.AES.decrypt(base64Str, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
var retArray = [];
if (decrypted.sigBytes > 0) {
retArray = this.wordArrayToUint8(decrypted.words, decrypted.sigBytes);
}
/*console.log("aesEcbPkcs7Decrypt======>>>")
console.log(aesdata.length + ", ["+Array.from(aesdata).join(",")+"]")
// console.log(CryptoJS.enc.Hex.stringify(decrypted)) 错误
console.log(CryptoJS.enc.Base64.stringify(data))
console.log(data.toString(CryptoJS.enc.Hex))
// console.log("==========>>>", decrypted.sigBytes)
console.log(retArray.length +", ["+Array.from(retArray).join(",")+"]")
console.log("aesEcbPkcs7Decrypt<<<======")*/
return retArray;
},
// 压缩
gzip: function (str) {
// str = escape(str) //--->压缩前编码,防止中午乱码
var binaryString = pako.gzip(str, {to: 'string'});
return binaryString;
},
gunzip: function (binData) {
// 解压
var data = pako.inflate(new Uint8Array(binData));
// Convert gunzipped byteArray back to ascii string:
key = String.fromCharCode.apply(null, new Uint16Array(data));
// key = unescape(key) //--->解压后解码,防止中午乱码
return key;
}
}
module.exports = GZIPAES