• 沒有找到結果。

2.5 函数 API 脚本开发

2.5.28 RSAUtils 类说明

路径

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

说明

提供RSA加解密方法。

使用示例

通过以下java代码生成公钥和私钥:

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;

}

} }

将上述公钥和私钥填入到下面代码中:

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 }

构造器详情

public RSAUtils()

构造一个无参数的RSAUtils

方法列表

返回类型 方法和说明

static byte[]

decodeBase64(String base64)

BASE64字符串解码为二进制数据

static byte[]

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

encryptData) RSA解密

static String

decrypt(String source,

java.security.interfaces.RSAPrivateKey privateKey) RSA解密(source为base64编码)

static String

decrypt(String source,

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

RSA解密(source为base64编码)

static String

decrypt(byte[] source,

java.security.interfaces.RSAPrivateKey privateKey) RSA解密

返回类型 方法和说明

static String

decrypt(byte[] source,

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

RSA解密

static String

encodeBase64(byte[] bytes)

二进制数据编码为BASE64字符串

static byte[]

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

RSA加密

static String

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

RSA加密(source为base64编码,返回加密数据为base64编 码)

static String

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

Map<String, String> config)

RSA加密(source为base64编码,返回加密数据为base64编 码)

static String

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

RSA加密(返回加密数据为base64编码)

static String

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

Map<String, String> config)

RSA加密(返回加密数据为base64编码)

static

java.security.interf aces.RSAPrivateKe y

getPrivateKey(byte[] privateKeyByte)

通过私钥字节数组创建RSA私钥

static

java.security.interf aces.RSAPrivateKe y

getPrivateKey(String privateKeyByte)

通过base64编码的私钥创建RSA私钥

static

java.security.interf aces.RSAPrivateKe y

getPrivateKey(String modulus, String exponent)

通过模数和指数创建RSA私钥

static

java.security.interf aces.RSAPublicKey

getPublicKey(byte[] publicKeyByte)

通过公钥字节数组创建RSA公钥

static

java.security.interf aces.RSAPublicKey

getPublicKey(String publicKeyByte)

通过base64编码的公钥创建RSA公钥

返回类型 方法和说明 static

java.security.Publi cKey

getPublicKey(String modulus, String exponent)

通过模数和指数创建RSA公钥

方法详情

public static byte[] decodeBase64(String base64) BASE64字符串解码为二进制数据

输入参数

base64:base64编码的数据 返回信息

返回base64解码后的数据

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

encryptData) RSA解密 输入参数

– privateKey:私钥

– encryptData:待解密数据 返回信息

解密后的数据

public static String decrypt(String source,

java.security.interfaces.RSAPrivateKey privateKey) RSA解密

输入参数

– source: 待解密数据的base64编码 – privateKey:私钥

返回信息 解密后的数据

public static String decrypt(String source,

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

RSA解密 输入参数

– source: 带解密数据的base64编码 – privateKey:私钥

– config: 解密配置,配置项可以为:

public static String decrypt(byte[] source,

java.security.interfaces.RSAPrivateKey privateKey) RSA解密

输入参数

– source: 带解密数据 – privateKey:私钥 返回信息

解密后的数据

public static String decrypt(byte[] source,

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

RSA解密 输入参数

– source: 带解密数据 – privateKey:私钥

– config: 解密配置,配置项可以为:

public static String encodeBase64(byte[] bytes) 二进制数据编码为BASE64字符串

输入参数

bytes:待编码数据 返回信息

BASE64编码

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

source) RSA加密 输入参数

– publicKey:公钥

– source:需要加密的内容 返回信息

加密后的数据内容

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

RSA加密 输入参数

– source:待加密数据的base64编码 – publicKey:公钥

返回信息

加密后的数据内容的base64编码

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

RSA加密 输入参数

– source:待加密数据的base64编码 – publicKey:公钥

– config:加密选项,配置项可以为:

transformation:指定加密的算法/模式/填充。例如:"RSA/ECB/

OAEPPadding"。详见https://docs.oracle.com/javase/8/docs/api/javax/

crypto/Cipher.html#getInstance-java.lang.String-● public static String encrypt(byte[] source, java.security.PublicKey publicKey)

RSA加密 输入参数

– source:需要加密的内容 – publicKey:公钥

返回信息

加密后的数据内容的base64编码

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

RSA加密 输入参数

– source:需要加密的内容 – publicKey:公钥

– config:加密选项,配置项可以为:

transformation:指定加密的算法/模式/填充。例如:"RSA/ECB/

OAEPPadding"。详见https://docs.oracle.com/javase/8/docs/api/javax/

crypto/Cipher.html#getInstance-java.lang.String-返回信息

加密后的数据内容的base64编码

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

privateKeyByte)

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

通过x509格式编码的私钥创建RSA私钥 输入参数

privateKeyByte:通过x509格式编码的私钥

返回信息 私钥

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

通过模数和指数创建RSA私钥 输入参数

– modulus:生成私钥需要的模数 – exponent:生成私钥需要的指数 返回信息

返回RSA私钥

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

publicKeyByte)

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

通过模数和指数创建RSA公钥 输入参数

– modulus:生成公钥需要的模数 – exponent:生成公钥需要的指数 返回信息

返回RSA公钥