0

I am writing an Android version of a Java program that I use to keep track of passwords.

I have the following function that I use to decode one line of the encrypted data:

public String decrypt(String text) {
    byte[] decryptedText = null;
    String rtn = "";
    String[] byteValues = text.substring(1, text.length() - 1).split(",");
    byte[] bytes = new byte[byteValues.length];
    for (int i=0, len=bytes.length; i<len; i++) {
        bytes[i] = Byte.valueOf(byteValues[i].trim());     
    }
    try {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        decryptedText = cipher.doFinal(bytes);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    if (decryptedText != null) {
        try {
            rtn = new String(decryptedText, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    return rtn;
}

ALGORITHM is set to:

public static final String ALGORITHM = "RSA";

The input text looks something like this (there are 128 values):

[12, -27, -31, 2, -65, -119, -104, -9, -42, 6, -32, -116, 35, 14, -30, -51, 117, 21, -33, -98, 21, 41, -11, 118, 85, -46, -67, -19, 47, -10, -42, -18, -116, 87, 49, 76, 25, 114, 88, -100, 41, 74, -70, 73, -103, 78, 117, 123, 127, -4, 98, -6, -124, -69, -112, -51, 27, 46, -83, 5, -84, 22, -64, -92, 15, -85, 34, -44, -50, 28, -69, 28, 106, -75, -7, 63, 112, 22, 112, -85, -28, 29, 97, -51, 112, -52, -2, 60, -55, -57, 41, 78, -90, -55, -1, -128, 12, 10, 73, -95, -105, 38, -125, 123, 89, 35, 63, 100, 124, 64, -4, 11, -50, -100, -81, -66, -64, 94, -93, -72, 67, 3, 22, -126, -125, 24, 127, -74]

When I run this function as part of a Java program (both Jave 1.6 & 1.7), I get back the original string as I expect. But, when I run this as part of my Android application the decryptedText array, instead of having a length of about 30, always has a length of 127. The resulting return string has all sorts of garbage characters in the front, but the last part of the string is the correctly decoded text.

Should I be calling the doFinal differently on Android than in Java?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
VingInMedina
  • 713
  • 1
  • 6
  • 16
  • Why is it that as soon as you ask a question, you find the answer yourself? The problem was the setting for ALGORITHM. When I changed it to this: public static final String ALGORITHM = "RSA/ECB/PKCS1Padding"; Everything worked fine. – VingInMedina Nov 11 '13 at 16:38
  • Formulating the question helps you to think again about the problem. Often, this lets you find the answer. – Henry Nov 11 '13 at 16:45
  • 1
    @VingInMedina Please either post that conclusion as an answer or delete your question. With regards to your solution - I always recommend you specify the full `algorithm/mode/padding` as different providers will use different defaults (as you've discovered!). – Duncan Jones Nov 12 '13 at 08:17

1 Answers1

1

The problem was the setting for ALGORITHM. When I changed it to this:

public static final String ALGORITHM = "RSA/ECB/PKCS1Padding";

Everything worked fine.

VingInMedina
  • 713
  • 1
  • 6
  • 16