jsencrypt icon indicating copy to clipboard operation
jsencrypt copied to clipboard

Suggest to add judgment when string too long

Open CreatorEdition opened this issue 2 years ago • 0 comments

Suggest to add judgment when string too long If the string is too long, it should be encrypted by dividing it into blocks. I've seen many people react and think that this is a bug.

        /**
         * Encrypt long strings by segmenting them.
         * @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;
        },
        /**
         * Decrypting long texts
         * @param {string} string The base64 encoding of the encrypted message.
         * @returns {string} Decrypted original text
         */
        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 03 '23 07:04 CreatorEdition