HarmonyOS crypto-js加密demo?

如题:HarmonyOS crypto-js加密demo?

阅读 446
1 个回答

参考链接:https://gitee.com/harmonyos\_samples/crypto-js-collection

主要代码:

// 加解密算法
export class Cipher {
  // 加密
  static CryptEncrypt(option: CipherOption): string {
    const PlaintextArray: CryptoJS.lib.WordArray = CryptoJS.enc.Utf8.parse(option.plaintext);
    const SecretKeyArray: CryptoJS.lib.WordArray = CryptoJS.enc.Utf8.parse(option.secretKey);
    const IVArray: CryptoJS.lib.WordArray = CryptoJS.enc.Utf8.parse(option.iv);
    const Res: string = option.CipherHelper.encrypt(PlaintextArray, SecretKeyArray, {
      mode: option.mode,
      padding: option.pad,
      iv: IVArray
    }).ciphertext.toString(option.encoding);
    return Res;
  }

  // 解密
  static CryptDecrypt(option: CipherOption): string {
    if (option.encoding == CryptoJS.enc.Hex) {
      const CiphertextArray: CryptoJS.lib.WordArray = CryptoJS.enc.Hex.parse(option.ciphertext);
      option.ciphertext = CryptoJS.enc.Base64.stringify(CiphertextArray);
    }
    const SecretKeyArray: CryptoJS.lib.WordArray = CryptoJS.enc.Utf8.parse(option.secretKey);
    const IVArray: CryptoJS.lib.WordArray = CryptoJS.enc.Utf8.parse(option.iv);
    const Res: string = option.CipherHelper.decrypt(option.ciphertext, SecretKeyArray, {
      mode: option.mode,
      padding: option.pad,
      iv: IVArray
    }).toString(CryptoJS.enc.Utf8);
    return Res;
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进