diff --git a/utils/jsencrypt.uts b/utils/jsencrypt.uts new file mode 100644 index 0000000..3c5953a --- /dev/null +++ b/utils/jsencrypt.uts @@ -0,0 +1,42 @@ +import { Config } from "../config"; +import { rsaEncryptText, rsaDecryptText } from "@/uni_modules/laoqianjunzi-crypto"; + +export class JSEncrypt { + + /** + * RSA公钥 + */ + private static readonly PUBLIC_KEY = Config.VITE_APP_RSA_PUBLIC_KEY; + + /** + * RSA私钥 + * ⚠ 前端一般不建议存储私钥 + */ + private static readonly PRIVATE_KEY = Config.VITE_APP_RSA_PRIVATE_KEY; + + /** + * RSA加密 + * + * @param text 明文 + * @returns Base64密文 + */ + static encrypt(text : string) : string { + if (!text) { + return ""; + } + return rsaEncryptText(JSEncrypt.PUBLIC_KEY, text, "base64"); + } + + /** + * RSA解密 + * + * @param cipherText Base64密文 + * @returns 明文 + */ + static decrypt(cipherText : string) : string { + if (!cipherText) { + return ""; + } + return rsaDecryptText(JSEncrypt.PRIVATE_KEY, cipherText, "base64"); + } +} \ No newline at end of file