Java NumberFormatException无法在Android中将xx解析为AES128Bit中的整数

Java NumberFormatException无法在Android中将xx解析为AES128Bit中的整数,java,android,Java,Android,我想在Android应用程序中加密和解密我的数据。当我运行应用程序时,它在解密时得到NumberFormatException(无法将xx解析为整数)。我已经试过了,但它没有解密我的JSON字符串 这是我的活动代码 jsonObject.put(KEY_REQUEST_ID, win2indiaPojo.getRequestId()); jsonObject.put(KEY_REQUEST_CODE, win2indiaPojo.getRequestCode()); jsonObject.put

我想在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);
这里是日志猫信息

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()的行中得到NuberFormatException

公共静态字节[]toByte(字符串hexString)
{
int len=hexString.length()/2;
字节[]结果=新字节[len];
对于(int i=0;i
那么,当你试图将Dy读入十六进制时,你期望得到什么呢?@chrylis I,m使用这个字符串key=“Dyv6ACIDe2q+OEjztjfNDw=”;用于对json数据进行加密和解密。我想将加密数据发送到服务器,并希望从服务器获得解密后的响应。但它只是加密的。它无法解密并获取NumformatException。我必须在=encryptedJson=SimpleCrypto.encrypt(jsonObject.toString(),key)中使用字符串密钥@tazeenmulani I thnk当您调用decrypt时,您给出的参数顺序不正确。您的代码
decryptedString=SimpleCrypto.decrypt(strFinalResult,key)
但不应该是
decryptedString=SimpleCrypto.decrypt(key,strFinalResult)
因为strFinalResult是这里的加密内容?@chrylis JSONObject finalResult=new JSONObject(tokener);String strFinalResult=finalResult.getString(“响应”);字符串解密字符串=”;decryptedString=SimpleCrypto.decrypt(strFinalResult,密钥);这是base64,不是十六进制。
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);
}