微信小程序之如何优雅的使用AES加密

技能 · 2018-11-21 · 549 人浏览
微信小程序之如何优雅的使用AES加密

最近在搞一个扫码的小程序,需要跟硬件对接,其中有一项选用AES128/CFB128/NOPACK加密,找半天才发现原来小程序本身有插件这东西


1、登录小程序后台进到最下面的设置里选择第三方服务,然后再点击下方的添加插件

登录小程序后台

2、搜索到Crypot然后添加

添加Crypot

3、引入Crypot加密

1、在app.json文件里加入下面代码

"plugins": {
    "crypto": {
        "version": "0.0.003",
        "provider": "wxf25d506ff81e19fb"
    }
}

2、在要使用加密的文件里引入

const crypto = requirePlugin("crypto");

3、加密解密

var mi = new crypto.AES().encrypt('010203040506', key, {
    iv: iv,
    mode: crypto.Mode.CFB,
    padding: crypto.Padding.NoPadding
});

console.log('加密:', mi.toString());


const v = new crypto.AES().decrypt(mi.toString(), key, {
      iv: iv,
      mode: crypto.Mode.CFB,
      padding: crypto.Padding.NoPadding
});

console.log('解密:', v.toString(crypto.Utf8));

4、查看结果
加密解密结果


附官方示例代码 【去看文档

const crypto = requirePlugin("crypto");
Page({
  onLoad: function () {

    console.warn('----------------------------------------------value: 布包云BubaoCloud salt: salt')
    let key = new crypto.PBKDF2("布包云BubaoCloud", new crypto.Utf8.parse("salt"), {
      keySize: parseInt(4),
      iterations: 1000
    });
    console.log('PBKDF2',key.toString());

    console.warn('----------------------------------------------value: 布包云')

    const sha = ['SHA1', 'SHA224', 'SHA256', 'SHA3', 'SHA384', 'SHA512', 'RIPEMD160'];
    sha.map((cryptoItem) => {
      console.log(cryptoItem, new crypto[cryptoItem]("布包云").toString());
    });

    console.warn('----------------------------------------------value: 布包云  key: BubaoCloud')

    const hmac = ['HmacSHA1', 'HmacSHA224', 'HmacSHA256', 'HmacSHA3', 'HmacSHA384', 'HmacSHA512', 'HmacRIPEMD160'];
    hmac.map((cryptoItem) => {
      console.log(cryptoItem, new crypto[cryptoItem]("布包云", "BubaoCloud").toString());
    });


    const mode = ['CBC', 'CFB', 'CTRGladman', 'CTR', 'ECB', 'OFB'];
    const padding = ['AnsiX923', 'Iso10126', 'Iso97971', 'NoPadding', 'ZeroPadding', 'Pkcs7'];

    console.warn('----------------------------------------------AES value: 布包云  key: BubaoCloud  iv: 8')
    mode.map((modeItem) => {
      padding.map((paddingItem) => {
        console.log(`AES mode: ${modeItem}, padding: ${paddingItem}`)
        const mi = new crypto.AES().encrypt('布包云', "BubaoCloud", {
          iv: 8,
          mode: crypto.Mode[modeItem],
          padding: crypto.Padding[paddingItem]
        });
        console.log('encrypt:', mi.toString());

        const v = new crypto.AES().decrypt(mi.toString(), "BubaoCloud", {
          iv: 8,
          mode: crypto.Mode[modeItem],
          padding: crypto.Padding[paddingItem]
        });
        console.log('decrypt:', v.toString(crypto.Utf8));
        console.log(``);
      });
    });

    console.warn('----------------------------------------------DES value: 布包云  key: BubaoCloud  iv: 8')
    mode.map((modeItem) => {
      padding.map((paddingItem) => {
        console.log(`DES mode: ${modeItem}, padding: ${paddingItem}`)
        const mi = new crypto.DES().encrypt('布包云', "BubaoCloud", {
          iv: 8,
          mode: crypto.Mode[modeItem],
          padding: crypto.Padding[paddingItem]
        });
        console.log('encrypt:', mi.toString());

        const v = new crypto.DES().decrypt(mi.toString(), "BubaoCloud", {
          iv: 8,
          mode: crypto.Mode[modeItem],
          padding: crypto.Padding[paddingItem]
        });
        console.log('decrypt:', v.toString(crypto.Utf8));
        console.log(``);
      });
    });

    console.warn('----------------------------------------------TripleDES value: 布包云  key: BubaoCloud  iv: 8')
    mode.map((modeItem) => {
      padding.map((paddingItem) => {
        console.log(`TripleDES mode: ${modeItem}, padding: ${paddingItem}`)
        const mi = new crypto.TripleDES().encrypt('布包云', "BubaoCloud", {
          iv: 8,
          mode: crypto.Mode[modeItem],
          padding: crypto.Padding[paddingItem]
        });
        console.log('encrypt:', mi.toString());

        const v = new crypto.TripleDES().decrypt(mi.toString(), "BubaoCloud", {
          iv: 8,
          mode: crypto.Mode[modeItem],
          padding: crypto.Padding[paddingItem]
        });
        console.log('decrypt:', v.toString(crypto.Utf8));
        console.log(``);
      });
    });

    console.warn('----------------------------------------------Rabbit value: 布包云  key: BubaoCloud  iv: 8')
    mode.map((modeItem) => {
      padding.map((paddingItem) => {
        console.log(`Rabbit mode: ${modeItem}, padding: ${paddingItem}`)
        const mi = new crypto.Rabbit().encrypt('布包云', "BubaoCloud", {
          iv: 8,
          mode: crypto.Mode[modeItem],
          padding: crypto.Padding[paddingItem]
        });
        console.log('encrypt:', mi.toString());

        const v = new crypto.Rabbit().decrypt(mi.toString(), "BubaoCloud", {
          iv: 8,
          mode: crypto.Mode[modeItem],
          padding: crypto.Padding[paddingItem]
        });
        console.log('decrypt:', v.toString(crypto.Utf8));
        console.log(``);
      });
    });



    console.warn('----------------------------------------------RSA Key')
    const keypair = new crypto.Keypair({bits:512});
    console.log('RSA key', keypair);

    console.warn('----------------------------------------------value: 布包云 key: RSA KEY')
    const encrypt = new crypto.RSA();
    encrypt.setPublicKey(keypair.public);
    const encrypted = encrypt.encrypt('布包云');
    console.log('RSA encrypt', encrypted);


    const decrypt = new crypto.RSA();
    decrypt.setPrivateKey(keypair.private);
    const decrypted = decrypt.decrypt(encrypted);
    console.log('RSA decrypt', decrypted);
  }
})
JavaScript 小程序
Theme Jasmine by Kent Liao