code: public static byte[] encrypt(RSAPublicKey key, byte[] data, String cipherTransformation,
              int paddingSizeInBytes) throws NoSuchAlgorithmException, NoSuchPaddingException,
              IOException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
          if (StringUtils.isBlank(cipherTransformation)) {
              cipherTransformation = DEFAULT_CIPHER_TRANSFORMATION;
              paddingSizeInBytes = DEFAULT_PADDING_SIZE;
          }
          Cipher cipher = Cipher.getInstance(cipherTransformation);
          cipher.init(Cipher.ENCRYPT_MODE, key);
          try (ByteArrayInputStream bois = new ByteArrayInputStream(data)) {
              try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                  int bytesRead;
                  byte[] buf = new byte[key.getModulus().bitLength() / 8 - paddingSizeInBytes];
                  while ((bytesRead = bois.read(buf)) != -1) {
                      baos.write(cipher.doFinal(buf, 0, bytesRead));
                  }
                  return baos.toByteArray();
              }
          }
      }
docs: Encrypt data with RSA public key.
  
  <p>
  Note: input data is divided into chunks of
  {@code size = key's size (in bytes) - paddingSizeInBytes}, so that long input data can be
  encrypted.
  </p>
  
  @param key
  @param data
  @param cipherTransformation
  cipher-transformation to use. If empty, {@link #DEFAULT_CIPHER_TRANSFORMATION}
  will be used.
  @param paddingSizeInBytes
  @return
  @see https://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html#Cipher
  @throws NoSuchAlgorithmException
  @throws NoSuchPaddingException
  @throws IOException
  @throws InvalidKeyException
  @throws IllegalBlockSizeException
  @throws BadPaddingException