评论

小程序实现混合加密

简单实现小程序混合加密

class SMCryptoFactory {
  cipherMode = 1;
  privateKey1 = "";
  publicKey1 = "";
  publicKey2 = "";
  openEncrypt = false;


  constructor({
    cipherMode = 1,
    privateKey1 = "",
    publicKey1 = "",
    publicKey2 = "",
    openEncrypt = false,
  }) {
    this.cipherMode = cipherMode;
    this.privateKey1 = privateKey1;
    this.publicKey1 = publicKey1;
    this.publicKey2 = publicKey2;
    this.openEncrypt = openEncrypt;
  }


  resetKeyPair() {
    this.publicKey1 = "";
    this.privateKey1 = "";
    this.publicKey2 = "";
  }


  mixCryptoEnCrypto(requestData: any) {
    // 使用sm4加密请求,使用sm2加密sm4秘钥,返回密文请求和加密秘钥
    const encryptKey = this.generateSM4key(); // 秘钥
    const encryptedData = this.sm4EnCrypto(requestData, encryptKey);


    const secretSM4Key = this.sm2EnCrypto(encryptKey);


    return {
      ...encryptedData,
      encryptKey: secretSM4Key,
    };
  }


  mixCryptoDeCrypto(response: any, secretSM4Key: string) {
    // 使用sm2解密sm4秘钥,使用sm4秘钥解密密文,返回明文请求
    const decryptKey = this.sm2DeCrypto(secretSM4Key);
    const result = this.sm4DeCrypto(response, decryptKey);
    return result;
  }


  // 生成客户端公钥和私钥
  generateSM2Key() {
    if (this.openEncrypt) {
      const { privateKey1, publicKey } = sm2.generateKeyPairHex(); // 在请求成功之后再存储,防止重复登录导致重复加密


      return {
        publicKey,
        privateKey1,
      };
    } else {
      return {};
    }
  }
  generateSM4key() {
    const res = randomStr(32);
    return res;
  }


  doVerifySign(msg, signValueHex) {
    const res = sm2.doVerifySignature(msg, signValueHex, this.publicKey2, {
      hash: true,
      publicKey: this.publicKey2,
    });
    return res;
  }


  // 请求签名 防篡改
  doSign(request) {
    const str = JSON.stringify(request);
    const msg = MD5(str);


    const signValueHex = sm2.doSignature(msg, this.privateKey1, {
      hash: true,
      publicKey: this.publicKey2,
    });


    return signValueHex;
  }


  // 使用sm2加密key
  sm2EnCrypto(key: string) {
    const secretKey = sm2.doEncrypt(key, this.publicKey1, this.cipherMode);
    return secretKey;
  }


  // sm2解密key
  sm2DeCrypto(secretKey: string) {
    const key = sm2.doDecrypt(secretKey, this.privateKey1, this.cipherMode);
    return key;
  }


  // sm4加密请求
  sm4EnCrypto(request: any, encryptKey: string) {
    const signValueHex = this.doSign(request);
    const _req = {
      ...request,
      signValueHex,
    };
    const requestData = sm4.encrypt(JSON.stringify(_req), encryptKey);
    return {
      requestData,
      cryptoType: "wx",
    };
  }


  // sm4解密响应
  sm4DeCrypto(response: any, deCryptoKey: string) {
    if (response) {
      const str = sm4.decrypt(response, deCryptoKey);
      return JSON.parse(str);
    } else {
      return "";
    }
  }
}
class SMCryptoFactory {
  cipherMode = 1;
  privateKey1 = "";
  publicKey1 = "";
  publicKey2 = "";
  openEncrypt = false;


  constructor({
    cipherMode = 1,
    privateKey1 = "",
    publicKey1 = "",
    publicKey2 = "",
    openEncrypt = false,
  }) {
    this.cipherMode = cipherMode;
    this.privateKey1 = privateKey1;
    this.publicKey1 = publicKey1;
    this.publicKey2 = publicKey2;
    this.openEncrypt = openEncrypt;
  }


  resetKeyPair() {
    this.publicKey1 = "";
    this.privateKey1 = "";
    this.publicKey2 = "";
  }


  mixCryptoEnCrypto(requestData: any) {
    // 使用sm4加密请求,使用sm2加密sm4秘钥,返回密文请求和加密秘钥
    const encryptKey = this.generateSM4key(); // 秘钥
    const encryptedData = this.sm4EnCrypto(requestData, encryptKey);


    const secretSM4Key = this.sm2EnCrypto(encryptKey);


    return {
      ...encryptedData,
      encryptKey: secretSM4Key,
    };
  }


  mixCryptoDeCrypto(response: any, secretSM4Key: string) {
    // 使用sm2解密sm4秘钥,使用sm4秘钥解密密文,返回明文请求
    const decryptKey = this.sm2DeCrypto(secretSM4Key);
    const result = this.sm4DeCrypto(response, decryptKey);
    return result;
  }


  // 生成客户端公钥和私钥
  generateSM2Key() {
    if (this.openEncrypt) {
      const { privateKey1, publicKey } = sm2.generateKeyPairHex(); // 在请求成功之后再存储,防止重复登录导致重复加密


      return {
        publicKey,
        privateKey1,
      };
    } else {
      return {};
    }
  }
  generateSM4key() {
    const res = randomStr(32);
    return res;
  }


  doVerifySign(msg, signValueHex) {
    const res = sm2.doVerifySignature(msg, signValueHex, this.publicKey2, {
      hash: true,
      publicKey: this.publicKey2,
    });
    return res;
  }


  // 请求签名 防篡改
  doSign(request) {
    const str = JSON.stringify(request);
    const msg = MD5(str);


    const signValueHex = sm2.doSignature(msg, this.privateKey1, {
      hash: true,
      publicKey: this.publicKey2,
    });


    return signValueHex;
  }


  // 使用sm2加密key
  sm2EnCrypto(key: string) {
    const secretKey = sm2.doEncrypt(key, this.publicKey1, this.cipherMode);
    return secretKey;
  }


  // sm2解密key
  sm2DeCrypto(secretKey: string) {
    const key = sm2.doDecrypt(secretKey, this.privateKey1, this.cipherMode);
    return key;
  }


  // sm4加密请求
  sm4EnCrypto(request: any, encryptKey: string) {
    const signValueHex = this.doSign(request);
    const _req = {
      ...request,
      signValueHex,
    };
    const requestData = sm4.encrypt(JSON.stringify(_req), encryptKey);
    return {
      requestData,
      cryptoType: "wx",
    };
  }


  // sm4解密响应
  sm4DeCrypto(response: any, deCryptoKey: string) {
    if (response) {
      const str = sm4.decrypt(response, deCryptoKey);
      return JSON.parse(str);
    } else {
      return "";
    }
  }
}
点赞 0
收藏
评论
登录 后发表内容