rsa.js 2 KB
Newer Older
wangdanlei's avatar
wangdanlei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/* 产引入jsencrypt实现数据RSA加密 */
import JSEncrypt from 'jsencrypt' // 处理长文本数据时报错 jsencrypt.js Message too long for RSA
/* 产引入encryptlong实现数据RSA加密 */
import Encrypt from 'encryptlong' // encryptlong是基于jsencrypt扩展的长文本分段加解密功能。

// 公钥key
const PUBLIC_KEY =
  'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMN/EJFCG6YExPc5X+qWVTEXVgnkqkce\n' +
  'gC5DXKM3zs/DMOOqPbyL4MZ8xKx4aHPnqVL/fJW5YCtrH0UJF+DdiE8CAwEAAQ=='
// 私钥key
const PRIVATE_KEY =
  'MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAw38QkUIbpgTE9zlf\n' +
  '6pZVMRdWCeSqRx6ALkNcozfOz8Mw46o9vIvgxnzErHhoc+epUv98lblgK2sfRQkX\n' +
  '4N2ITwIDAQABAkBSrTYQeGjA+VtjQnxx9UnqDPhyDqaElT2YUxymWj+4VRFs39+a\n' +
  'ipsWhqzxCEVoM9o+4Q1Wt9G1omQ8m3nxHjShAiEA/aUx/wunxUh4mdk6nE9uEgoP\n' +
  'TrpM0YjJmtalsquI6HUCIQDFT6y0Ozvp035Q9NBzgRZeLYfXVNGG7+JqGQuWc7s1\n' +
  'MwIhAN0qgyrJxfNvGsHDuah8VhTisrBKIBuAs5IQ4hCmI83VAiEAg/VTNX5gBXOS\n' +
  'iSYRuw3cHcQpk1OAhdQtAnQZkdd4no0CIGOitKyBCtiW37+jEGnLuqywEQ8ifI2D\n' +
  'gDtbCfPLQE54'

export default {
  /* JSEncrypt加密 */
  rsaPublicData(data) {
    const jsencrypt = new JSEncrypt()
    jsencrypt.setPublicKey(PUBLIC_KEY)

    // 如果是对象/数组的话,需要先JSON.stringify转换成字符串
    return jsencrypt.encrypt(data)
  },
  /* JSEncrypt解密 */
  rsaPrivateData(data) {
    const jsencrypt = new JSEncrypt()
    jsencrypt.setPrivateKey(PRIVATE_KEY)

    // 如果是对象/数组的话,需要先JSON.stringify转换成字符串
    return jsencrypt.encrypt(data)
  },
  /* 加密 */
  encrypt(data) {
    const encryptor = new Encrypt()
    encryptor.setPublicKey(PUBLIC_KEY)

    // 如果是对象/数组的话,需要先JSON.stringify转换成字符串
    return encryptor.encryptLong(data)
  },
  /* 解密 - PRIVATE_KEY - 验证 */
  decrypt(data) {
    const encryptor = new Encrypt()
    encryptor.setPrivateKey(PRIVATE_KEY)

    // 如果是对象/数组的话,需要先JSON.stringify转换成字符串
    return encryptor.decryptLong(data)
  }
}