我想在Android应用程序中加密和解密我的数据。当我运行应用程序时,它在解密时获得NumberFormatException(无法将xx解析为整数)。我试过了,但它没有解密我的JSON字符串。
这是我的活动代码
jsonObject.put(KEY_REQUEST_ID, win2indiaPojo.getRequestId());
jsonObject.put(KEY_REQUEST_CODE, win2indiaPojo.getRequestCode());
jsonObject.put(KEY_CHANNEL_ID, win2indiaPojo.getChannelId());
jsonObject.put(KEY_IP_ADDRESS, win2indiaPojo.getIPAddress());
jsonObject.put(KEY_STATUS_FLAG, win2indiaPojo.getStatusFlag());
jsonObject.put(KEY_USERNAME, win2indiaPojo.getUserId());
jsonObject.put(KEY_PASSWORD, win2indiaPojo.getPassword());
json = jsonObject.toString();
System.out.println("json = " + json );
String encryptedJson="";
encryptedJson = SimpleCrypto.encrypt(json , key);
System.out.println("encryptedJson = " + encryptedJson );
JSONObject inner = new JSONObject();
inner.put(KEY_REQUEST, encryptedJson);
inner.put(KEY_VENDOR_ID, "1");
String strjson = "";
JSONObject outer = new JSONObject();
outer.put("W2INBCWS", inner);
strjson=outer.toString();
System.out.println("strjson = " + strjson);
StringEntity se = new StringEntity(strjson);
System.out.println("StringEntity = " + se);
httpPost.setEntity(se);
System.out.println("httpPost Method !!!!!");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(httpPost);
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
String jsonString = reader.readLine();
jsonString=jsonString.replace("true", "\"true\"");
JSONTokener tokener = new JSONTokener(jsonString);
JSONObject finalResult = new JSONObject(tokener);
String strFinalResult=finalResult.getString("Response");
System.out.println("Decrypted Values = ");
String decryptedString="";
decryptedString = SimpleCrypto.decrypt(strFinalResult, key);
System.out.println("decryptedString = " + decryptedString);
这是Log cat info。
01-02 16:17:45.986: W/System.err(744): java.lang.NumberFormatException: unable to parse 'Dy' as integer
01-02 16:17:45.986: W/System.err(744): at java.lang.Integer.parse(Integer.java:383)
01-02 16:17:45.994: W/System.err(744): at java.lang.Integer.parseInt(Integer.java:372)
01-02 16:17:45.994: W/System.err(744): at java.lang.Integer.valueOf(Integer.java:528)
01-02 16:17:45.994: W/System.err(744): at com.json_to_server.SimpleCrypto.toByte(SimpleCrypto.java:63)
01-02 16:17:45.994: W/System.err(744): at com.json_to_server.SimpleCrypto.decrypt(SimpleCrypto.java:20)
01-02 16:17:46.004: W/System.err(744): at com.json_to_server.EncryptDecrypt_Demo.POST(EncryptDecrypt_Demo.java:200)
01-02 16:17:46.004: W/System.err(744): at com.json_to_server.EncryptDecrypt_Demo$HttpAsyncTask.doInBackground(EncryptDecrypt_Demo.java:257)
01-02 16:17:46.004: W/System.err(744): at com.json_to_server.EncryptDecrypt_Demo$HttpAsyncTask.doInBackground(EncryptDecrypt_Demo.java:1)
01-02 16:17:46.004: W/System.err(744): at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-02 16:17:46.004: W/System.err(744): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-02 16:17:46.004: W/System.err(744): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-02 16:17:46.004: W/System.err(744): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-02 16:17:46.004: W/System.err(744): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-02 16:17:46.014: W/System.err(744): at java.lang.Thread.run(Thread.java:1019)
我在result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
public static byte[] toByte(String hexString)
{
int len = hexString.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
return result;
}
public static String decrypt(String seed, String encrypted) throws Exception
{
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}