Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

java - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...