/* 产引入jsencrypt实现数据RSA加密 */
import JSEncrypt from 'jsencrypt' // 处理长文本数据时报错 jsencrypt.js Message too long for RSA
/* 产引入encryptlong实现数据RSA加密 */
import Encrypt from 'encryptlong' // encryptlong是基于jsencrypt扩展的长文本分段加解密功能。
 
// 密钥对生成 http://web.chacuo.net/netrsakeypair
 
// 公钥key
const publicKey = `MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzf8shQ65PrbUWFor3orc
FOTN8eerzE1eoL/2xBbog0z2nn0nXNELDrZxuOfeN3+w6DX5IxKEo0l0hSFUKjtK
6Rkv9Wp/+R6O9rPtSXprtvxsU9FYAkQJid6FJreS8kL/ulYV/CRTeh+o18i4FFKC
puH97pIgWgNaF8sTVzrj9VFpWpXZLa3mFXffSprOhwswnT4SukZ6FZfaGw85gmTB
1hty//RuSj3FYVbkDrVFl2pzzl2yw0twKN59tCVhXfJlRiaA9M4/lsanOjS+Avhf
zN3F5z2N+sZTCxSjDvCjkCY1sm8cSJMIUBkezOUf0cOISLDTIOIJ8MUe4Au1JpYw
JQIDAQAB`
// 私钥key
const privateKey = `MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDN/yyFDrk+ttRY
WiveitwU5M3x56vMTV6gv/bEFuiDTPaefSdc0QsOtnG45943f7DoNfkjEoSjSXSF
IVQqO0rpGS/1an/5Ho72s+1Jemu2/GxT0VgCRAmJ3oUmt5LyQv+6VhX8JFN6H6jX
yLgUUoKm4f3ukiBaA1oXyxNXOuP1UWlaldktreYVd99Kms6HCzCdPhK6RnoVl9ob
DzmCZMHWG3L/9G5KPcVhVuQOtUWXanPOXbLDS3Ao3n20JWFd8mVGJoD0zj+Wxqc6
NL4C+F/M3cXnPY36xlMLFKMO8KOQJjWybxxIkwhQGR7M5R/Rw4hIsNMg4gnwxR7g
C7UmljAlAgMBAAECggEAfT6WLNgE3RKADK/WWFoI8VMju95lX7Pywr20JQjgmWQj
1by2QAKBPJLE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qYDoo6qww2f4gJCyvGHPtwWLeJz
vc/8uKxB+heqCf+htBT3RvMSlARikkLXv/AmeTaNiLaOin+MUfjBcqmfS+ihpkbE
jxDnSlsKpAqMdeunX/nImzC6`
 
  /* JSEncrypt加密 */
  export function rsaPublicData(data) {
    let jsencrypt = new JSEncrypt()
    jsencrypt.setPublicKey(publicKey)
    // 如果是对象/数组的话，需要先JSON.stringify转换成字符串
    let result = jsencrypt.encrypt(data)
    return result
  }
  /* JSEncrypt解密 */
  export function rsaPrivateData(data) {
    let jsencrypt = new JSEncrypt()
    jsencrypt.setPrivateKey(privateKey)
    // 如果是对象/数组的话，需要先JSON.stringify转换成字符串
    let result = jsencrypt.encrypt(data)
    return result
  }
  /* 加密 */
  export function encrypt(data) {
    const PUBLIC_KEY = publicKey
    let encryptor = new Encrypt()
    encryptor.setPublicKey(PUBLIC_KEY)
    // 如果是对象/数组的话，需要先JSON.stringify转换成字符串
    const result = encryptor.encryptLong(data)
    return result
  }
  /* 解密 - PRIVATE_KEY - 验证 */
  export function decrypt(data) {
    const PRIVATE_KEY = privateKey
    let encryptor = new Encrypt()
    encryptor.setPrivateKey(PRIVATE_KEY)
    // 如果是对象/数组的话，需要先JSON.stringify转换成字符串
    let result = encryptor.decryptLong(data)
    return result
  }
let datas = rsaPublicData('你好')
let a = `MMeI6N/ZWwsfe92XEfmRNWxe7WXVBz4uZz052Wo4NdvIlMNl/ag/9tKs0A1/8l8cgF5558yLs+Ql+3QPKvy8gay5hB18htMnkLTpkpAxclntS3Vpr7NtdSaoW1nz3P2OgHZ+ww22zcyox0MvCkLpge/20+KcomD/fxtiEK6NZ/PPQB5kNi2Uj3kJMhDz39tvdP9zytK3Po5QNGkw+CyoYPbQi80wplmurxQ/g0l9FBsqHuoCmBs7ZNtVLlG9pE4UkG8MmyV3tQTpIStEaF4TVHwb5XdlP3e9RQ+6rx2DLaunzzeBtNR1JGiNB9nrC5ImO77wzipdbuydCY8ZkILYlA==`
console.log(datas == a)
console.log(datas)
  console.log('加密',datas)
  console.log('解密',rsaPrivateData(datas))