Use JSEncrypt for segmented encryption

tags: encrypt and decode

Summary

Recently, rsa encryption is required to transmit data in the background in the project, but some data is too long, the key is 2048 bits, so only 256 characters can be encrypted, so I want to use segmented encryption to encrypt.

problem analysis

Because the encrypted data contains Chinese and English, it is too troublesome to use charAt to extract each character to determine the bytes occupied by the character, so I use base64 to encode the data so that the original data will not contain Chinese. Can carry out simple length division, thus go to encrypt directly.

Use JSEncrypt.js library for encryption, filling algorithm library is pkcs1pad2 filling algorithm

Below is my own method of segment encryption

JSEncrypt.prototype.encryptLong = function (string) {
        var k = this.getKey();
        var strings = base64.encode(string);
        var encrypArray = new Array();
        try {
          var ct = "";
          if (strings.length > 245) {
            for (var i = 0; i < strings.length; i = i + 245) {
              var str;
              if (strings.length >= (i + 245)) {
                str = strings.substring(i, i + 245);
              } else {
                str = strings.substring(i);
              }
              var t1 = k.encrypt(str);
              encrypArray.push(hex2b64(t1));
            }
            return encrypArray;
          }
          var t = k.encrypt(strings);
          encrypArray.push(hex2b64(t));
          return encrypArray;
        } catch (ex) {
          return false;
        }
      };

My own segmented decryption method

JSEncrypt.prototype.decryptLong = function (encrypArray) {
        var key = this.getKey();
        try {
          var t3 = "";
          for(var j=0; j < encrypArray.length; j++){
            var t4 = key.decrypt(b64tohex(encrypArray[j]));
            t3 += t4;
          }
          var strings = String(base64.decode(t3));
          return t3;
        } catch (ex) {
          return false;
        }
      };

Since the maximum encryption length when JSEncrypt is used for encryption is 245, because the minimum 11 bytes need to be set aside for pkcs1pad2 padding, so I use the length of 245 to intercept and encrypt the data. After the encryption of each segment is completed, I call hex2b64 () Convert hexadecimal to base64, and then put it in the array, so that we can get an array with complete encrypted data.

Simple research on pkcs1pad2 filling

function pkcs1pad2(s,n) {
  if(n < s.length + 11) { // TODO: fix for utf-8
    console.error("Message too long for RSA");
    return null;
  }
  var ba = new Array();
  var i = s.length - 1;
  while(i >= 0 && n > 0) {
    var c = s.charCodeAt(i--);
    if(c < 128) { // encode using utf-8
      ba[--n] = c;
    }
    else if((c > 127) && (c < 2048)) {
      ba[--n] = (c & 63) | 128;
      ba[--n] = (c >> 6) | 192;
    }
    else {
      ba[--n] = (c & 63) | 128;
      ba[--n] = ((c >> 6) & 63) | 128;
      ba[--n] = (c >> 12) | 224;
    }
  }
  ba[--n] = 0;
  var rng = new SecureRandom();
  var x = new Array();
  while(n > 2) { // random non-zero pad
    x[0] = 0;
    while(x[0] == 0) rng.nextBytes(x);
    ba[--n] = x[0];
  }
  ba[--n] = 2;
  ba[--n] = 0;
  return new BigInteger(ba);
}

After the data is encrypted, there will be at least 11 bytes left for non-zero padding, and the first and second bits filled must be 0, 2, and the eleventh bit must be 0. Presumably this is the distinction when decrypting. Which are the real data or the basis for filling the data.

Intelligent Recommendation

Jsencrypt encryption decryption string

Encrypt and decrypt strings with jsencrypt's RSA algorithm Foreword First attach the github address of the source code:https://github.com/travist/jsencrypt Instructions encryption Decrypt If the key u...

uniapp uses jsencrypt encryption

npm download jsencrypt Put the jsencrypt file under components Create a new jsencrypt.js file Introduce where needed...

Simulated jsencrypt encryption

refer to: https://www.bilibili.com/video/BV1xq4y1H7ud...

Reptile RSA (jsencrypt) encryption

Result: Under RSA: Asymmetric Cleaning Algorithm (known as: Public Private Key Encryption) explain in detail: Crack two ways of cracking this algorithm: Write a JS code based on the front end encrypti...

Front end jsencrypt encryption

1. Online generation asymmetric encrypted key private key pair, online generation public private key pair, RSA Key Pair Create, generate RSA key pairs http://web.chacuo.net/netrsakeypair 2, automatica...

More Recommendation

Vue Encryption (jsencrypt)

first step: cnpm install jsencrypt --dep Step 2: SRC / UTILS / RSAENCrypt.js Step 3: Used in Methods this.encryptedData(this.form.password);...

jsencrypt encryption decryption

The first step, download The second step is to establish a jsencrypt.js file, the content is as follows Through the public key encryption, the private key decryption, which page needs to introduce Uti...

Jsencrypt: asymmetric encryption RSA

1. Modify the source code and search: "T.prototype.getPublickey = Function (", then add 3 functions later 2. Use Pubulickey to encrypt the front end. const publicKey = ''; const ins = new JS...

Vue -jsencrypt encryption

1 Introduction In the project, there is a scene where the password is transmitted to the back end. If the transmission is directly transmitted, there is a risk of leakage in the password, so the passw...

vue jsencrypt for RSA encryption

1. Use jsencrypt for RSA encryption (non-global) 1. Installation dependencies 2. Create a js file Tip: For example: @/libs/encrypt.js The code is as follows (example): 3. Use it on the Forgot Password...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top