import { JSEncrypt } from 'jsencrypt'

/**
 * 加密
 * @param param
 * @param key
 */
const passwordEncryption = (param: string, key: string) => {
  // 后台给的公钥
  const publicKey = `-----BEGIN PUBLIC KEY-----${key}-----END PUBLIC KEY-----\n`
  const encryptor = new JSEncrypt() // 新建JSEncrypt对象
  encryptor.setPublicKey(publicKey) // 设置公钥
  // 对密码进行加密
  return encryptor.encrypt(param)
}

// 解密
const decrypt = (msg: string) => {
  const privateKey = ''
  const decrypt = new JSEncrypt()
  decrypt.setPrivateKey(privateKey)
  return decrypt.decrypt(msg)
}

export { passwordEncryption, decrypt }
