/*
 * @Author: 半生瓜 1515706227@qq.com
 * @Date: 2024-03-05 16:00:03
 * @LastEditors: 半生瓜 1515706227@qq.com
 * @LastEditTime: 2024-04-29 10:40:35
 * @FilePath: \cmccr-h5\src\views\configuration\userManager\aes.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// 引入
import CryptoJS from 'crypto-js'
//随机生成16位字符串
export function randomString() {
  var str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  var result = ''
  for (var i = 16; i > 0; --i) result += str[Math.floor(Math.random() * str.length)]
  return result
}

// 加密
export function encrypt(word, key2) {
  const key = CryptoJS.enc.Utf8.parse(key2)
  const srcs = CryptoJS.enc.Utf8.parse(word)
  const encrypted = CryptoJS.AES.encrypt(srcs, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })

  return encrypted.toString()
}

// 解密
export function decrypt(word, key2) {
  var key = CryptoJS.enc.Utf8.parse(key2)
  var decrypt = CryptoJS.AES.decrypt(word, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  return CryptoJS.enc.Utf8.stringify(decrypt).toString()
}

//rsa加密
export function RsaencryptedData(publicKey, data) {
  // 新建JSEncrypt对象
  let encryptor = new JSEncrypt()
  // 设置公钥
  encryptor.setPublicKey(publicKey)
  // 加密数据
  return encryptor.encrypt(data)
}
