tsrpc 框架flows中间件 的加密解密算法 及ase 对称加密算法Ts

  • Post author:
  • Post category:其他



// 发送前加密
server.flows.preSendDataFlow.push(v => {
    v.data= EncryptUtil.encrypt(JSON.stringify(v.data));
    return v;
});

// 接收前解密
server.flows.preRecvDataFlow.push(v => {
    v.data= EncryptUtil.decrypt(JSON.stringify(v.data));
    console.log(v.data);
    return v;
})



 // 发送前加密
    this.client.flows.preSendDataFlow.push(v => {
        let en=new EncryptUtil();
        v.data=en.encrypt(JSON.stringify(v.data));
        return v;
    });

    // 接收前解密
    this.client.flows.preRecvDataFlow.push(v => {
        let en=new EncryptUtil();
        v.data=en.decrypt(v.data);
        return v;
    })


Uint8Array加密算法1

加密 各个字节-1

解密 各个字节+1

export class EncryptUtil {

    // 加密
    encrypt(data: string): Uint8Array {
        const buf=this.stringToUint8Array(data)
        for (let i = 0; i < buf.length; ++i) {
            buf[i] -= 1;
        }
        return buf;
    }

     // 解密
    decrypt(data:any): Array<JSON> {
        let buf;
        if(typeof data =="string"){
            const arr_data=JSON.parse(data)
            buf=arr_data.data
        }else if(typeof data =="object"){
            buf=new Uint8Array(data)
            
        }
        buf=new Uint8Array(buf)
        for (let i = 0; i < buf.length; ++i) { 
            buf[i] += 1;
        }
        const nk=this.Uint8ArrayToString(buf)
        const result=JSON.parse(nk)
        return result;
    }

    //uint8array 转 string   
    Uint8ArrayToString(fileData:Uint8Array):string{
        let dataString = "";
        for (let i = 0; i < fileData.length; i++) {
        dataString += String.fromCharCode(fileData[i]);
        }
        return dataString
    }
  
    //string 转 uint8array
    stringToUint8Array(str:string):Uint8Array{
        const arr = [];
        for (let i = 0, j = str.length; i < j; ++i) {
            arr.push(str.charCodeAt(i));
        }
        const tmpUint8Array = new Uint8Array(arr);
        return tmpUint8Array
    }
    //object-uint8array 转uin8array

}

关于vue项目any类型报警信息屏蔽

  "eslintConfig": {
    "rules": {
        "no-var": 0,
        "@typescript-eslint/no-explicit-any": ["off"]

        }
    }

ase 加密 解密算法

import CryptoJS from “crypto-js”;

//npm i –save-dev @types/crypto-js    npm install crypto-js

//秘钥

const CRYPTOJSKEY = “123456789asdfghj”;

//向量

const iv = “32位”;

// 发送前加密
server.flows.preSendDataFlow.push(v => {
    v.data= EncryptUtil.aes_encrypt(JSON.stringify(v.data));
    return v;
});

// 接收前解密
server.flows.preRecvDataFlow.push(v => {
    v.data=EncryptUtil.aes_decrypt(v.data)
    return v;
})
    //加密
    /*
    * {param} plaintText 加密明文
    * return  str 加密结果
    */
    static aes_encrypt(plaintText:string): string {
        const options = {
            iv: CryptoJS.enc.Utf8.parse(iv),
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        };
        const key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
        const encryptedData = CryptoJS.AES.encrypt(plaintText, key, options);
        return encryptedData.ciphertext.toString().toUpperCase();
    }
    //解密
    /*
    * {param} plaintText 解密密文
    
    * return  str 解密结果
    */
    public static aes_decrypt(encryptedBase64Str:any): string {
        const encryptedHexStrp = CryptoJS.enc.Hex.parse(encryptedBase64Str)
        const encryptedHexStr = CryptoJS.enc.Base64.stringify(encryptedHexStrp)
        const options = {
            iv: CryptoJS.enc.Utf8.parse(iv),
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        };
        const key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
        // 解密
        const decryptedData = CryptoJS.AES.decrypt(encryptedHexStr, key, options);
        // 解密后,需要按照Utf8的方式将明文转位字符串
        const decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
        return JSON.parse(decryptedStr);
    }