Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NumberFormatException on valid number String

I have seen some other questions about this but the errors were related to a leading 0 in the string. This unfortunately is not my case.

I am receiving encrypted data from an external source in base64 format, I then decode it (using an included Base64 library because the android sdk version is 7), decrypt the message, and after all that I have a simple string in a number format.

When I try to cast it to Long or Integer I get this error:

java.lang.NumberFormatException: Invalid long: "2551122"
    at java.lang.Long.invalidLong(Long.java:125)
    at java.lang.Long.parse(Long.java:362)
    at java.lang.Long.parseLong(Long.java:353)
    at java.lang.Long.parseLong(Long.java:319)
    at com.nzn.lol.LoginActivity$LoginTask.doInBackground(LoginActivity.java:98)
    at com.nzn.lol.LoginActivity$LoginTask.doInBackground(LoginActivity.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:264)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)

To check the input I used prints and it really is the string "2551122". When I try to check for equality, it is also not correct

"2551122".equals(numberAsString) // Gives me false

I thought it was an encoding issue and tried taking the decoded bytes and creating strings in several encodings, also tried to decode the bytes from the base64 string with these same several encodings and still have no idea of what is causing this error.

Please any help is appreciated

UPDATE

This is the code for decrypting the string (Encryptor class):

private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance(encryptionAlgorithim);
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(iVector));
    byte[] decrypted = cipher.doFinal(encrypted);
    return decrypted;
}

public String decrypt(String encryptedString, String key) {

    byte[] keyBytes = key.getBytes();
    byte[] decoded = Base64.decode(encryptedString); // Decodes the string from base64 to byte[]
    byte[] result = decrypt(keyBytes, decoded);
    return new String(result);
}

This is how the error is raised:

Encryptor encryptor = new Encryptor();
Long.parseLong(encryptor.decrypt(base64String, secretKey)) // Throws me the error
like image 997
Draiken Avatar asked Jul 06 '12 16:07

Draiken


People also ask

What is NumberFormatException for input string?

The NumberFormatException is an unchecked exception in Java that occurs when an attempt is made to convert a string with an incorrect format to a numeric value. Therefore, this exception is thrown when it is not possible to convert a string to a numeric type (e.g. int, float).

What does Java Lang NumberFormatException mean?

The java. lang. NumberFormatException comes when you try to parse a non-numeric String to a Number like Short, Integer, Float, Double etc. For example, if you try to convert . "null" to an integer then you will get NumberFormatException.

What is NumberFormatException in Java with example?

The NumberFormatException occurs when an attempt is made to convert a string with improper format into a numeric value. That means, when it is not possible to convert a string in any numeric type (float, int, etc), this exception is thrown. It is a Runtime Exception (Unchecked Exception) in Java.


1 Answers

The clear text probably contains characters that look like ASCII digits, but are not ASCII digits. See http://www.fileformat.info/info/unicode/category/Nd/list.htm for a list of digits which are not ASCII digits.

To confirm that, execute the following method on the decrypted text and on the hard-coded long as string, and compare the results:

public static String displayCharValues(String s) {
    StringBuilder sb = new StringBuilder();
    for (char c : s.toCharArray()) {
        sb.append((int) c).append(",");
    }
    return sb.toString();
}

EDIT: it appears that the clear text starts with a BOM (byte order mark) which is an invisible character.

like image 196
JB Nizet Avatar answered Oct 25 '22 01:10

JB Nizet