Function Java code:
package com.test.fn;
import java.security.*;
import java.security.spec.*;
import java.util.Base64;
import javax.crypto.Cipher;
import java.util.logging.*;
public class RSAEncryptDecrypt {
private static final Logger logger = Logger.getLogger(RSAEncryptDecrypt.class.getName());
public static class Input {
public String message;
public String secretKeyBase64;
public String rsaMode;
public String actionType; // ENCRYPT or DECRYPT
}
public static class Result {
public String message;
public String executionInfo;
}
public Result handleRequest(Input input) throws Exception {
logger.log(Level.INFO, "OIC - message:", input.message);
logger.log(Level.INFO, "OIC - secretKeyBase64:", input.secretKeyBase64);
logger.log(Level.INFO, "OIC - rsaMode:", input.rsaMode);
logger.log(Level.INFO, "OIC - actionType:", input.actionType);
Result result = null;
if ("DECRYPT".equals(input.actionType)) {
result = decryptMyMessage(input);
} else if ("ENCRYPT".equals(input.actionType)) {
result = encryptMyMessage(input);
} else {
result = new Result();
result.executionInfo = "ERROR: No proper action found , possible value "
+ "is ENCRYPT or DECRYPT , recieved value:"
+ input.actionType;
}
return result;
}
// Method to encrypt plaintext using the RSA public key
public Result encryptMyMessage(Input input) throws Exception {
Result result = new Result();
try {
PublicKey publicKey = loadPublicKey(input.secretKeyBase64);
Cipher cipher = Cipher.getInstance(input.rsaMode); // RSA encryption scheme
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(input.message.getBytes());
result.message = Base64.getEncoder().encodeToString(encryptedBytes);
result.executionInfo = "SUCCESS";
} catch (Exception e) {
result.executionInfo = e.getMessage();
logger.log(Level.INFO, "Error Details:", e.getMessage());
}
return result; // return as Base64 string
}
public Result decryptMyMessage(Input input) throws Exception {
Result result = new Result();
try {
Cipher decryptCipher =
Cipher.getInstance(input.rsaMode);
PrivateKey privateKey = getPrivateKeyFromString(input.secretKeyBase64); decryptCipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes =
decryptCipher.doFinal( Base64.getDecoder().decode(input.message));
String myDecryptedMsg =
new String(decryptedBytes, "UTF-8");
result.message = myDecryptedMsg;
result.executionInfo = "SUCCESS";
} catch (Exception e) {
result.executionInfo = e.getMessage();
logger.log(Level.INFO, "Error Details:", e.getMessage());
}
return result; // return as Base64 string
}
// Converts a Base64-encoded public key string to PublicKey object
public static PublicKey loadPublicKey(String base64PublicKey) throws Exception {
byte[] keyBytes = Base64.getDecoder().decode(base64PublicKey);
X509EncodedKeySpec spec =
new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory =
KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(spec);
}
// Convert Base64 string to PrivateKey
public static PrivateKey getPrivateKeyFromString(String base64PrivateKey)
throws Exception {
byte[] keyBytes = Base64.getDecoder().decode(base64PrivateKey);
PKCS8EncodedKeySpec spec =
new PKCS8EncodedKeySpec(keyBytes);
KeyFactory factory =
KeyFactory.getInstance("RSA");
return factory.generatePrivate(spec);
}
}
Java code Screenshots:





































