jsencrypt icon indicating copy to clipboard operation
jsencrypt copied to clipboard

bug for the string when it's too long

Open shacaisheng opened this issue 9 years ago • 6 comments
trafficstars

hello Author. It had a problem that the string when it's too long , the decrypt funtion is return "null". how can i solve this problem?

shacaisheng avatar Oct 15 '16 14:10 shacaisheng

same problem, please help

sreerajkarippala avatar Nov 30 '16 12:11 sreerajkarippala

i think i found an answer at http://security.stackexchange.com/a/33445

sreerajkarippala avatar Dec 02 '16 04:12 sreerajkarippala

Not a bug close this

lax1dude avatar Aug 28 '17 05:08 lax1dude

https://github.com/lsqswl/rsaencrypt

lsqswl avatar Oct 12 '18 09:10 lsqswl

You can try the following code 你可以尝试以下代码,通过分块加密

        /**
         * 分段加密长字符串
         * @param {string} str the string to encrypt
         * @return {string} the encrypted string encoded in base64
         * @public
         */
        encryptLong: function(str,$key) {
            var encryptor = new JSEncrypt();
            encryptor.setPublicKey($key);
            var maxChunkLength = 100,
                output = '',
                inOffset = 0,
                outOffset = 0;
            while (inOffset < str.length) {
                console.log(str.substring(inOffset, inOffset + maxChunkLength));
                output += encryptor.encrypt(str.substring(inOffset, inOffset + maxChunkLength));
                inOffset += maxChunkLength;
            }
            return output;
        },
        /**
         * 长文本解密
         * @param {string} string 加密后的base64编码
         * @returns {string} 解密后的原文
         */
        decryptLong: function(string,$key) {
            var decryptor = new JSEncrypt();
            decryptor.setPrivateKey($key);
            var maxChunkLength = 172,
                output = '',
                inOffset = 0,
                outOffset = 0;
            while (inOffset < string.length) {
                console.log(string.substring(inOffset, inOffset + maxChunkLength));
                output += decryptor.decrypt(string.substring(inOffset, inOffset + maxChunkLength));
                inOffset += maxChunkLength;
            }
            return output;
        },

CreatorEdition avatar Apr 02 '23 10:04 CreatorEdition

您好!您的来信已收到,谢谢!

shacaisheng avatar Apr 02 '23 10:04 shacaisheng