code: public static String rsaEncrypt(String content, String publicKey,
                                      String charset) throws AlipayApiException {
          try {
              PublicKey pubKey = getPublicKeyFromX509(AlipayConstants.SIGN_TYPE_RSA,
                  new ByteArrayInputStream(publicKey.getBytes()));
              Cipher cipher = Cipher.getInstance(AlipayConstants.SIGN_TYPE_RSA);
              cipher.init(Cipher.ENCRYPT_MODE, pubKey);
              byte[] data = StringUtils.isEmpty(charset) ? content.getBytes()
                  : content.getBytes(charset);
              int inputLen = data.length;
              ByteArrayOutputStream out = new ByteArrayOutputStream();
              int offSet = 0;
              byte[] cache;
              int i = 0;
              // 对数据分段加密  
              while (inputLen - offSet > 0) {
                  if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
                      cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
                  } else {
                      cache = cipher.doFinal(data, offSet, inputLen - offSet);
                  }
                  out.write(cache, 0, cache.length);
                  i++;
                  offSet = i * MAX_ENCRYPT_BLOCK;
              }
              byte[] encryptedData = Base64.encodeBase64(out.toByteArray());
              out.close();
  
              return StringUtils.isEmpty(charset) ? new String(encryptedData)
                  : new String(encryptedData, charset);
          } catch (Exception e) {
              throw new AlipayApiException("EncryptContent = " + content + ",charset = " + charset,
                  e);
          }
      }
docs: 公钥加密
  
  @param content   待加密内容
  @param publicKey 公钥
  @param charset   字符集，如UTF-8, GBK, GB2312
  @return 密文内容
  @throws AlipayApiException