iT邦幫忙

0

message too long for rsa [solution]

rsa
  • 分享至 

  • xImage
  •  
相關依賴包
https://www.npmjs.com/package/jsencrypt

// 私钥
const privateKey = `
-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQCAxKwtmALgQK9kEWjVpqbuBB8icjb64UF4Bq/GpKTRN7ilOuUZ
AkCh0bDmFMayab4BQguvl+7eFma97h/A2OErNFzaK5zuWdc7/DZybB7UOzhfTgSm
88pPdjnQGb2mNFeQ1Gz2oxkix+Muk/iXwrMaexFDFjN237cXuW3FUndgfwIDAQAB
AoGAAPLyHefn5YU0hbVHI4P16P0oyZUz6Idudxb9hqZB/LS/bKBRfeyxUz/7YMa4
bCoxV0sIUQP6o7JgkXfGziILDsa1rcbMjR8JZypvMqkhDk+HvT8qi3uPjmxGT3us
SMM+WtT3wTmwKq9cb5ldrDRdDukH/EJVSEVZtFsr8q0UtUkCQQCQO9qnPXgPdyJ+
jvXZhGmDroE5FIOer9VkR/dddddddddddddddddOfGgSY1n4rVHh6nfRKZOHvQ0P
uKPicMgdAkEA5IzdV61q2TQAItP1PnrpnHslJ5lmls4pmLYVy6cRgPMDtTdfxErq
KuTxA0tEIVYznXJu4JPnP5vafyULsQPASwJAKAYT9Nif2lrK3ae6HTQbO4lPHpHM
vFo6sustUrpnFCf1MxBQVvThyyvGjWO4ccNWt4Qi343T/JS/bR9lis6VGQJAT3Ay
wvXQzJwJbBGWDVh1eGFJIcFs6f/Qb3bijn2MVPCimksb60NKrwwSRV3U2W131ULg
DQj7eJ14TXcX4Df4jwJAZMKmzojHUuFOsjo2VWIgwhjglYTVUknON+D2skatjyk1
jPSYEhquhaa7cyHfMioFrMjnZEjRiyLUYcSabcdf123123
-----END RSA PRIVATE KEY-----
`;

// 公钥
const publicKey = `
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCAxKwtmALgQK9kEWjVpqbuBB8i
cjb64UF4Bq/GpKTRN7ilOuUZAkCh0bDmaddfdb4BQguvl+7eFma97h/A2OErNFza
K5zuWdc7/DZybB7UOzhfTgSm88pPdjnQGb2mNFeQ1Gz2oxkix+abc/iXwrMaexFD
FjN237cXuW3FUndgf12312313
-----END PUBLIC KEY-----
`;

// 长文本加密处理
JSEncrypt.prototype.encryptLong = function (text) {
    let k = this.getKey();
    let maxLength = (((k.n.bitLength() + 7) >> 3) - 11);

    try {
        if (text.length > maxLength) {
            let lt = text.match(/.{1,117}/g), ct = '';
            lt.forEach((entry) => { ct += k.encrypt(entry); });
            return ct;
        }

        return k.encrypt(text);
    } catch (ex) {
        console.warn('encryptLong error:', ex);

        return false;
    }
}

// 长文本解密处理
JSEncrypt.prototype.decryptLong = function (text) {
    let k = this.getKey();
    let maxLength = ((k.n.bitLength() + 7) >> 3);

    try {
        if (text.length > maxLength) {
            let lt = text.match(/.{1,256}/g), ct = '';
            lt.forEach((entry) => { ct += k.decrypt(entry); });
            return ct;
        }

        return k.decrypt(text);
    } catch (ex) {
        console.warn('decryptLong error:', ex);

        return false;
    }
}

// 加密
function RsaEncrypt(encrypted) {
    const encryptor = new JSEncrypt()
    encryptor.setPublicKey(publicKey)
    return encryptor.encryptLong(encrypted)
}

// 解密
function RsaDecrypt(encrypted) {
    const decrypt = new JSEncrypt()
    decrypt.setPrivateKey(privateKey)
    return decrypt.decryptLong(encrypted)
}

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言