1.1 加密
var key = '2cef781a9c0411eb'
var str = JSON.stringify({ "ip": "192.168.12.1", "mac": "80:3F:5D:0E:04:2B", "wan_ip": "0.0.0.0", "internet": "1", "5g_ssid": "531A6-yang_test_2G", "2g_ssid": "531A6-yang_test_5G", "5g_tx_power": "100", "firmware_ver": "68PD_210504", "new_firmware": "1", "new_firmware_ver": "68PD_210606", "usb_over_ip_ver": "098.022.2019SEP27a", "usb_over_ip_status": "1", "bind_list": "80:3f:5d:12:34:56;80:3f:5d:12:34:78;80:3f:5d:12:34:90;", "24g_clients": [{ "host": "", "ip": "192.168.12.139" }] })
var crypto = require('crypto');
function customPadding(str, blockSize, padder, format) {
str = Buffer.from(str, "utf8").toString(format);
//1 char = 8bytes
var bitLength = str.length * 8;
if (bitLength < blockSize) {
for (i = bitLength; i < blockSize; i += 8) {
str += padder;
}
} else if (bitLength > blockSize) {
while ((str.length * 8) % blockSize != 0) {
str += padder;
}
}
return Buffer.from(str, format).toString("utf8");
}
const encrypt = (plainText, keyhex) => {
const textBuffer = customPadding(plainText, 128, 0x0, "hex");
const key = Buffer.from(keyhex, 'utf8');
var iv = Buffer.alloc(16, 0);
const cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
cipher.setAutoPadding(false);
let encrypted = cipher.update(textBuffer, 'utf8', 'base64')
encrypted += cipher.final('base64');
return encrypted;
};
const hash = encrypt(str, key);
console.log(hash)
1.2 解密
var crypto = require('crypto');
const decrypt = (messagebase64, keyhex) => {
const key = Buffer.from(keyhex, 'utf8');
// const encoded = Buffer.from(messagebase64, 'base64');
var iv = Buffer.alloc(16, 0);
const decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
decipher.setAutoPadding(false);
let decrypted = decipher.update(messagebase64, 'base64', 'utf-8');
decrypted += decipher.final();
return decrypted;
}
const hash = decrypt("KGLMC7rl8km1FjS1uLzFnZzAwrcNarUw7+C9rnuowvI1Le2pjOlpxCMQwWzbbc8MbgRvyRN20hxVAryUqHwjVloubf3L+74wev3k6RD9YiYGJNc8jzXsrFyOQyOWbo499EdZmD5I2zgkqRzB3gy2p3xbZmj9QVtHvAuTDUM5cwTDwIaL9zWTpLCHRigGmtf0dxIdlzro4FrYPm+GHOoKKePzoskGltBU8TBAMimjhx0+Fosu57GlNdbnjCHzWsssTUP5GmWrxod6X+2hCHES02oTkEVvzTc/rklNpFFo7FikTjSMdpBLw4fVR1vL0MS+IiRmdm54DTpyog4ywzlyLur1s6ljggk4PS2gk7hUhiGXrbOml1iiDjgw2BrKVIcDgrl27xvFDxBs3nI9RZkVet2rPgcKJHpzPULahIASyzv9AF3Gy7OBhikaLhVGub+BAKnYOFewALq8sDJKFUhfe2E2DVNq0piNM2p8npanD5Ru8sWizSs+zWjGGAdCfEkRNRTAVA4ASmKp0O5yn0wsO9UbYMuX9y7b95+l9b2zCYA9l0TycmdJyKJmZHuD7FxL", '2cef781a9c0411eb');
console.log(hash)
2.1 加密
let key = '2cef781a9c0411eb'
const iv = Buffer.alloc(16, 0);
let data = JSON.stringify({ "ip": "192.168.12.1", "mac": "80:3F:5D:0E:04:2B", "wan_ip": "0.0.0.0", "internet": "1", "5g_ssid": "531A6-yang_test_2G", "2g_ssid": "531A6-yang_test_5G", "5g_tx_power": "100", "firmware_ver": "68PD_210504", "new_firmware": "1", "new_firmware_ver": "68PD_210606", "usb_over_ip_ver": "098.022.2019SEP27a", "usb_over_ip_status": "1", "bind_list": "80:3f:5d:12:34:56;80:3f:5d:12:34:78;80:3f:5d:12:34:90;", "24g_clients": [{ "host": "", "ip": "192.168.12.139" }] })
let cryptojs = require('crypto-js');
function customPadding(str, blockSize, padder, format) {
str = Buffer.from(str, "utf8").toString(format);
//1 char = 8bytes
var bitLength = str.length * 8;
if (bitLength < blockSize) {
for (i = bitLength; i < blockSize; i += 8) {
str += padder;
}
} else if (bitLength > blockSize) {
while ((str.length * 8) % blockSize != 0) {
str += padder;
}
}
return Buffer.from(str, format).toString("utf8");
}
let encryptedData = cryptojs.AES.encrypt(customPadding(data, 128, 0x0, "hex"), cryptojs.enc.Utf8.parse(key), {
iv: cryptojs.enc.Utf8.parse(iv),
mode: cryptojs.mode.CBC,
padding: cryptojs.pad.Pkcs7
})
console.log(encryptedData.ciphertext.toString(cryptojs.enc.Base64));
2.2 解密
let key = '2cef781a9c0411eb'
let iv = Buffer.alloc(16, 0);
let cryptojs = require('crypto-js');
//解密方法
function Decrypt(word) {
let decrypt = cryptojs.AES.decrypt(word, cryptojs.enc.Utf8.parse(key), {
iv: cryptojs.enc.Utf8.parse(iv),
mode: cryptojs.mode.CBC,
padding: cryptojs.pad.Pkcs7
})
let decryptedStr = decrypt.toString(cryptojs.enc.Utf8);
return decryptedStr.toString();
}
console.log(Decrypt("KGLMC7rl8km1FjS1uLzFnZzAwrcNarUw7+C9rnuowvI1Le2pjOlpxCMQwWzbbc8MbgRvyRN20hxVAryUqHwjVloubf3L+74wev3k6RD9YiYGJNc8jzXsrFyOQyOWbo499EdZmD5I2zgkqRzB3gy2p3xbZmj9QVtHvAuTDUM5cwTDwIaL9zWTpLCHRigGmtf0dxIdlzro4FrYPm+GHOoKKePzoskGltBU8TBAMimjhx0+Fosu57GlNdbnjCHzWsssTUP5GmWrxod6X+2hCHES02oTkEVvzTc/rklNpFFo7FikTjSMdpBLw4fVR1vL0MS+IiRmdm54DTpyog4ywzlyLur1s6ljggk4PS2gk7hUhiGXrbOml1iiDjgw2BrKVIcDgrl27xvFDxBs3nI9RZkVet2rPgcKJHpzPULahIASyzv9AF3Gy7OBhikaLhVGub+BAKnYOFewALq8sDJKFUhfe2E2DVNq0piNM2p8npanD5Ru8sWizSs+zWjGGAdCfEkRNRTAVA4ASmKp0O5yn0wsO9UbYMuX9y7b95+l9b2zCYA9l0TycmdJyKJmZHuD7FxLol5I6QmPJwoJr65Emerb0g=="))