登录
注册
node.js 学习社区
如何通过AES-128-CBC加密AMF3协议的内容

acker

2018-11-27 16:36

const CryptoJS = require('crypto-js');

const ByteArray = require('bytearray-node');

let CBC_encryption = function (data) {

    let BA = new ByteArray();

    BA.writeObject(data);

    BA.buffer = BA.buffer.slice(0, BA.writePosition);

    let key = CryptoJS.enc.Utf8.parse('sjqtvrwwt7yijjla');

    let iv = CryptoJS.enc.Utf8.parse('1234abcdefgh6789');

    //下面的可以正常加密UTF-8的,但是Base64格式的就得不到期望的

    let encrypted = CryptoJS.AES.encrypt(BA.buffer.toString(), key, {

        iv,

        mode: CryptoJS.mode.CBC,

        padding: CryptoJS.pad.Pkcs7

    });

    return encrypted.toString(CryptoJS.enc.Base64);

};

let CBC_decryption = function (param1, key, iv) {

    key = CryptoJS.enc.Utf8.parse('sjqtvrwwt7yijjla');

    iv = CryptoJS.enc.Utf8.parse('1234abcdefgh6789');

    let decrypted = CryptoJS.AES.decrypt(param1, key, {

        iv,

    mode: CryptoJS.mode.CBC,

    padding: CryptoJS.pad.Pkcs7

    });

    // 解析Base64编码

    let buffer = new Buffer(decrypted.toString(CryptoJS.enc.Base64), "base64");

    return new ByteArray(buffer).readObject();

};

//希望能加密下面那个对象能得到第二个方法解密的内容

console.log(CBC_encryption({c: 'JPZnSMX6AGNZv04A2wANNfx0U0psbvGJixr2MgRaDeA7B1SPY80S/XcdLtyx4q+/9Ui4s2GPDCmWKX45n6EWm64EUd4w1BjHpr/3iTRGB4EjrbwVtkqHQXaqqwcQN08pkTaONOIxpJRKkKcbZKeBzsvhvHN+Oy+G4d0aqOMD/mA='}));

console.log(CBC_decryption('riJMo7dV47qukyH2tT0GLSOZpg/GTng831ctiECPqeItIgXmAL+k39mSHEFch9T6TVoxgjmQZg9oJn6p+sUVjJUAGgslGV74X9m9jt3UgOHbrcAturHnFVfS8tPz5IeBtalYRMa3/nNc6MIl03E/Aacd2DGi9CEjVuQiI5wSbfT7tZsOJSx3o1VQ1obmAqgod4qlVF9U46kt+B2und5YdHk49Cgkl2ZeYh3a8S0+GUStXxm1ydIFbh2SxQtpGDkC'));

回复 · 1

发表回复

你可以在回复中 @ 其他人