0

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.

3
  • stackoverflow.com/questions/12471999/… Commented Jul 27, 2019 at 15:31
  • The problem is actually not the encryption but stringifying the JSON results in the error posted
    – Boron
    Commented Jul 27, 2019 at 18:21
  • @Boron do you mean that the String.valueOf(cardJSONObject) is failing? If so I suggest you specify how you generate the JSONObject. And just reduce your example to creating the JSONObject, then putting the stringify in a try catch and show the exception from the catch block.
    – pcoates
    Commented Jul 27, 2019 at 20:02

0

Browse other questions tagged or ask your own question.