I need to encrypt card details such as card no, cvv then send that data to the back-end using RSA.
This is the method that I'm using to do so:
private String encryptCard(String payload, String pubKey) {
String PAN = "";
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Cipher cipherA = Cipher.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(pubKey));
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
cipherA.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encrypted = cipherA.doFinal(payload.getBytes(StandardCharsets.UTF_8));
PAN = Base64.getEncoder().encodeToString(encrypted);
}
} catch (Exception ex) {
}
return PAN;
}
After creating the card JSONObject like below:
{
"card_number":"6565956486494648",
"csv":"648",
"expiry_date":"092023"
}
I simply then call encryptCard(String.valueOf(cardJSONObject),RSA_KEY);
However I get the response Failed to decode: No content to map due to end-of-input\n at [Source: (String)\"\"; line: 1, column: 0]
The error seems to be in the json object being stringified but I'm not sure why and how I can go around this.