Updated on 2023-02-28 GMT+08:00

RSAUtils

Path

com.roma.apic.livedata.common.v1.RSAUtils

Description

This class is used to provide the RSA encryption and decryption methods.

Example

Use the following Java code to generate a public key and a private key:

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;

public class Main {

    public static void main(String[] args) {
        try {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(1024);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            PublicKey publicKey = keyPair.getPublic();
            System.out.println("publicKey:" + new String(Base64.getEncoder().encode(publicKey.getEncoded())));

            PrivateKey privateKey = keyPair.getPrivate();
            System.out.println("privateKey:" + new String(Base64.getEncoder().encode(privateKey.getEncoded())));
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }
}

Add the public key and private key to the following code:

importClass(com.roma.apic.livedata.common.v1.RSAUtils);
importClass(com.roma.apic.livedata.common.v1.Base64Utils);

function execute(data) {
    var publicKeyString = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDd4CRRppmYVlFl3dX4iVGN+2Twy5gLeEPRbvhOko/xFipGF7XV0weTp4wCakgdnm+DR4gBBrQtfAuKwYIBPIr+C1FI5sKYA3NxazDWUcXR3xlPM5D0DWjacjcMjnaj2v21WZxGpwHZHQ9TLd4OBBq3fva1r/cE8s1Lji5QeFiklwIDAQAB";

    var privateKeyString = "**********";

    var publicKey = RSAUtils.getPublicKey(publicKeyString)
    var privateKey = RSAUtils.getPrivateKey(privateKeyString)

    var origin = "hello rsa"
    var encrypted = RSAUtils.encrypt(Base64Utils.encode(origin), publicKey)

    var decrypted = RSAUtils.decrypt(encrypted, privateKey)
    return decrypted
}

Constructor Details

public RSAUtils()

Constructs an RSAUtils class without parameters.

Method List

Returned Type

Method and Description

static byte[]

decodeBase64(String base64)

Decode a Base64 character string to binary data.

static byte[]

decrypt(java.security.PrivateKey privateKey, byte[] encryptData)

Decrypt data using the RSA algorithm.

static String

decrypt(String source, java.security.interfaces.RSAPrivateKey privateKey)

Decrypt data using the RSA algorithm (the source is encoded using Base64).

static String

decrypt(String source, java.security.interfaces.RSAPrivateKey privateKey, Map<String, String> config)

Decrypt data using the RSA algorithm (the source is encoded using Base64).

static String

decrypt(byte[] source, java.security.interfaces.RSAPrivateKey privateKey)

Decrypt data using the RSA algorithm.

static String

decrypt(byte[] source, java.security.interfaces.RSAPrivateKey privateKey, Map<String, String> config)

Decrypt data using the RSA algorithm.

static String

encodeBase64(byte[] bytes)

Encode binary data to a Base64 character string.

static byte[]

encrypt(java.security.PublicKey publicKey, byte[] source)

Encrypt data using the RSA algorithm.

static String

encrypt(String source, java.security.PublicKey publicKey)

Encrypt data using the RSA algorithm (both the source and returned data is encoded using Base64).

static String

encrypt(String source, java.security.PublicKey publicKey, Map<String, String> config)

Encrypt data using the RSA algorithm (both the source and returned data is encoded using Base64).

static String

encrypt(byte[] source, java.security.PublicKey publicKey)

Encrypt data using the RSA algorithm (the returned data is encoded using Base64).

static String

encrypt(byte[] source, java.security.PublicKey publicKey, Map<String, String> config)

Encrypt data using the RSA algorithm (the returned data is encoded using Base64).

static java.security.interfaces.RSAPrivateKey

getPrivateKey(byte[] privateKeyByte)

Create an RSA private key by using a private key byte array.

static java.security.interfaces.RSAPrivateKey

getPrivateKey(String privateKeyByte)

Create an RSA private key by using a Base64-encoded private key.

static java.security.interfaces.RSAPrivateKey

getPrivateKey(String modulus, String exponent)

Create an RSA private key by using the modulus and exponent.

static java.security.interfaces.RSAPublicKey

getPublicKey(byte[] publicKeyByte)

Create an RSA public key by using a public key byte array.

static java.security.interfaces.RSAPublicKey

getPublicKey(String publicKeyByte)

Create an RSA public key by using a Base64-encoded public key.

static java.security.PublicKey

getPublicKey(String modulus, String exponent)

Create an RSA public key by using the modulus and exponent.

Method Details

  • public static byte[] decodeBase64(String base64)

    Decode a Base64 character string to binary data.

    Input Parameter

    base64 indicates the data encoded using Base64.

    Returns

    Data decoded by using Base64

  • public static byte[] decrypt(java.security.PrivateKey privateKey, byte[] encryptData)

    Decrypt data using the RSA algorithm.

    Input Parameter

    • privateKey indicates a private key.
    • encryptData indicates the data to be decrypted.

    Returns

    Decrypted data.

  • public static String decrypt(String source, java.security.interfaces.RSAPrivateKey privateKey)

    Decrypt data using the RSA algorithm.

    Input Parameter

    • source indicates the Base64 code of the data to be decrypted.
    • privateKey indicates a private key.

    Returns

    Decrypted data.

  • public static String decrypt(String source, java.security.interfaces.RSAPrivateKey privateKey, Map<String,String>config)

    Decrypt data using the RSA algorithm.

    Input Parameter

    • source indicates the Base64 code of the data to be decrypted.
    • privateKey indicates a private key.
    • config indicates the decryption configuration. The options are as follows:

      transformation: specifies the decryption algorithm/mode/padding, for example, RSA/ECB/OAEPPadding. For details, see the parameter description.

    Returns

    Decrypted data.

  • public static String decrypt(byte[] source, java.security.interfaces.RSAPrivateKey privateKey)

    Decrypt data using the RSA algorithm.

    Input Parameter

    • source indicates data to be decrypted.
    • privateKey indicates a private key.

    Returns

    Decrypted data.

  • public static String decrypt(byte[] source, java.security.interfaces.RSAPrivateKey privateKey, Map<String,String>config)

    Decrypt data using the RSA algorithm.

    Input Parameter

    • source indicates data to be decrypted.
    • privateKey indicates a private key.
    • config indicates the decryption configuration. The options are as follows:

      transformation: specifies the decryption algorithm/mode/padding, for example, RSA/ECB/OAEPPadding. For details, see the parameter description.

    Returns

    Decrypted data.

  • public static String encodeBase64(byte[] bytes)

    Encode binary data to a Base64 character string.

    Input Parameter

    bytes indicates data to be encoded.

    Returns

    Base64 encoding.

  • public static byte[] encrypt(java.security.PublicKey publicKey, byte[] source)

    Encrypt data using the RSA algorithm.

    Input Parameter

    • publicKey indicates a public key.
    • source indicates the content to be encrypted.

    Returns

    Encrypted data content.

  • public static String encrypt(String source, java.security.PublicKey publicKey)

    Encrypt data using the RSA algorithm.

    Input Parameter

    • source indicates the Base64 code of the data to be encrypted.
    • publicKey indicates a public key.

    Returns

    Base64 code of the encrypted data content.

  • public static String encrypt(String source, java.security.PublicKey publicKey, Map<String, String> config)

    Encrypt data using the RSA algorithm.

    Input Parameter

    • source indicates the Base64 code of the data to be encrypted.
    • publicKey indicates a public key.
    • config indicates the encryption option. The options are as follows:

      transformation: specifies the encryption algorithm/mode/padding, for example, RSA/ECB/OAEPPadding. For details, see the parameter description.

  • public static String encrypt(byte[] source, java.security.PublicKey publicKey)

    Encrypt data using the RSA algorithm.

    Input Parameter

    • source indicates the content to be encrypted.
    • publicKey indicates a public key.

    Returns

    Base64 code of the encrypted data content.

  • public static String encrypt(byte[] source, java.security.PublicKey publicKey, Map<String, String> config)

    Encrypt data using the RSA algorithm.

    Input Parameter

    • source indicates the content to be encrypted.
    • publicKey indicates a public key.
    • config indicates the encryption option. The options are as follows:

      transformation: specifies the encryption algorithm/mode/padding, for example, RSA/ECB/OAEPPadding. For details, see the parameter description.

    Returns

    Base64 code of the encrypted data content.

  • public static java.security.interfaces.RSAPrivateKey getPrivateKey(byte[] privateKeyByte)

    Create an RSA private key by using the private key of the x509 format.

    Input Parameter

    privateKeyByte indicates the private key encoded in x509 format

    Returns

    Private key.

  • public static java.security.interfaces.RSAPrivateKey getPrivateKey(String privateKeyByte)

    Create an RSA private key by using the private key of the x509 format.

    Input Parameter

    privateKeyByte indicates the private key encoded in x509 format

    Returns

    Private key.

  • public static java.security.interfaces.RSAPrivateKey getPrivateKey(String modulus, String exponent)

    Create an RSA private key by using the modulus and exponent.

    Input Parameter

    • modulus indicates the modulus required for generating a private key.
    • exponent indicates the exponent required for generating a private key.

    Returns

    RSA private key.

  • public static java.security.interfaces.RSAPublicKey getPublicKey(byte[] publicKeyByte)

    Create an RSA public key by using a public key encoded in x509 format.

    Input Parameter

    publicKeyByte indicates the public key encoded in x509 format.

    Returns

    Public key.

  • public static java.security.PublicKey getPublicKey(String modulus, String exponent)

    Create an RSA public key by using the modulus and exponent.

    Input Parameter

    • modulus indicates the modulus required for generating a public key.
    • exponent indicates the exponent required for generating a public key.

    Returns

    RSA public key.