参考链接: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; } }
参考链接:https://gitee.com/harmonyos\_samples/crypto-js-collection
主要代码: