最近VUE项目需要用到MD5和RSA加密,废话不多说

md5方法:  使用 crypto-js

1 安装: npm install crypto-js

2 引用: import CryptoJS from 'crypto-js'

3 加密: let md5 = CryptoJS.MD5("test").toString();

RSA方法: 使用 jsencrypt

1 安装: npm install jsencrypt

2 引用: import JsEncrypt from 'jsencrypt'

3 加密: 
    let jsEncrypt= new JsEncrypt();
    jsEncrypt.setPublicKey(PUBLIC_KEY); // 设置 加密公钥
    let signature = jsEncrypt.encrypt(data); // 进行加密

4 解密:
    let jsEncrypt= new JsEncrypt();
    jsEncrypt.setPrivateKey(prikey); // 设置私钥
    let str = jsEncrypt.decrypt(signature);

RSA分段加解密方法: 使用 encryptlong

1 安装: npm install encryptlong

2 引用: import EncryptLong from 'encryptlong'

3 加密: 
    let encryptor = new EncryptLong();
    encryptor.setPublicKey(PUBLIC_KEY); // 设置 加密公钥
    let signature = encryptor.encryptLong(data);  // 进行加密

4 解密:
    let encryptor = new EncryptLong();
    encryptor.setPrivateKey(PRIVATE_KEY);  // 设置私钥
    let str = encryptor.decryptLong(signature);

AES方法:使用 crypto-js

加密:
     // 转换秘钥
    let realKey = CryptoJS.SHA1(SECRET_KEY);
    realKey = CryptoJS.SHA1(realKey).toString().substring(0, 32); //真正的key

    let message = JSON.stringify(data);
    let key =  CryptoJS.enc.Hex.parse(realKey);
    let srcs = CryptoJS.enc.Utf8.parse(message);
    let encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
    let cipherText = encrypted.toString();


解密:
    // 转换秘钥
    let realKey = CryptoJS.SHA1(SECRET_KEY);
    realKey = CryptoJS.SHA1(realKey).toString().substring(0, 32); //真正的key
    let key = CryptoJS.enc.Hex.parse(realKey);

    let content = CryptoJS.enc.Base64.parse(data);
    let decrypt = CryptoJS.AES.decrypt({ciphertext: content}, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
    let content = decrypt.toString(CryptoJS.enc.Utf8);

Logo

前往低代码交流专区

更多推荐