Public Key Based
A few of the Rest APIs require an encrypted request payload. The merchant application must use the RSA algorithm to encrypt the request payload from the "Public Key."
Rocketfuel uses the RSA algorithm for encryption and decryption in the REST APIs.
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2e4stIYooUrKHVQmwztC
/l0YktX6uz4bE1iDtA2qu4OaXx+IKkwBWa0hO2mzv6dAoawyzxa2jmN01vrpMkMj
rB+Dxmoq7tRvRTx1hXzZWaKuv37BAYosOIKjom8S8axM1j6zPkX1zpMLE8ys3dUX
FN5Dl/kBfeCTwGRV4PZjP4a+QwgFRzZVVfnpcRI/O6zhfkdlRah8MrAPWYSoGBpG
CPiAjUeHO/4JA5zZ6IdfZuy/DKxbcOlt9H+z14iJwB7eVUByoeCE+Bkw+QE4msKs
aIn4xl9GBoyfDZKajTzL50W/oeoE1UcuvVfaULZ9DWnHOy6idCFH1WbYDxYYIWLi
AQIDAQAB
-----END PUBLIC KEY-----
export const encryptedReq = async (toEncrypt, publicKey) => {
const buffer = Buffer.from(toEncrypt);
const encrypted = crypto.publicEncrypt(publicKey, buffer);
return encrypted.toString('base64');
};
public static String encryptData(String text) {
String encoded = "";
byte[] encrypted;
String s1 = PUBLIC_KEY.replaceAll("^.*\n|\n-+END PUBLIC KEY-+$", "");
try {
byte[] publicBytes = Base64.decode(s1, Base64.NO_WRAP);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey pubKey = keyFactory.generatePublic(keySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
encrypted = cipher.doFinal(text.getBytes());
encoded = Base64.encodeToString(encrypted, Base64.DEFAULT);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return encoded;
}
function encrypt_data(string $to_crypt,string $certificate):string
{
$encryptedText = '';
$formatted_cert = str_replace('\n', '', $certificate);
$public_key = openssl_pkey_get_public($formatted_cert); //extract public key from certificate
$key_details = openssl_pkey_get_details($public_key); // get public key details
$part_len = $key_details['bits'] / 8 - 11;
$parts = str_split($to_crypt, $part_len); // split string data into parts
foreach ($parts as $part) { //encrypt in part
$encrypted_temp = '';
openssl_public_encrypt($part, $encrypted_temp, $public_key, OPENSSL_PKCS1_OAEP_PADDING);
$encryptedText .= $encrypted_temp;
}
return base64_encode($encryptedText); //encode cipher text to base64
}
Last updated