Nextj项目如何引用jsencrypt

568 阅读1分钟

因为直接引用jsencrypt会报错找不到window 但是查了下,其他的rsa加密都不太好使,按照网上的方式也不生效,所以最后研究了下发现可以异步引用

export const RSAEncrypt = async (message: string): Promise<string> => {
  const JSEncrypt = (await import('jsencrypt')).default;
  const jsencrypt = new JSEncrypt();
  // 公钥
  const pubKey ='_PUBLIC_KEY_';
  jsencrypt.setPublicKey(pubKey);
  return jsencrypt.encrypt(message) || message;
};

使用的时候就

const phoneEncode = await RSAEncrypt(phone)

简单方便