如何在Android中加密和解密json数据?

时间:2013-12-10 06:11:59

标签: java android json

我想在Android中加密和解密json数据。我添加了sun.misc.BASE64Decoder.jar,commons-anm-3.1.1.jar,commons-digester.jar,w2iEncryption.jar的jar文件,这4个jar文件在引用的库中添加,当我运行应用程序时,应用程序崩溃并发生错误和异常:

  

NoClassDeffFoundError E / AndroidRuntime(589):java.lang.NoClassDefFoundError:com.anm.cms.core.util.AES128Bit。

这是我的代码:

 class GetJSONParse extends AsyncTask<String, Void, JSONObject>
                {
                    @Override
                        protected void onPreExecute() 
                     {
                            super.onPreExecute();

                        strUserName = etextUsername.getText().toString().trim();
                        strPassWord = etextPassword.getText().toString().trim();

                         pDialog = new ProgressDialog(Login.this);

                         pDialog.setIndeterminate(true);

                         pDialog.show();

                         System.out.println("Progress Dialog!!!!!!!!!!!!!!!!!!!!!!!");

                 }

                    @Override
                    protected JSONObject doInBackground(String... params) 
                  {
                         String strUrl = "http://test.window2india.com/cms/json/w2iWS";
                         JSONParser jParser = new JSONParser();

                            // Getting JSON from URL
                            JSONObject json1 = jParser.getJSONFromUrl(strUrl, null);
                            return json1;
                    }

                @Override
                     protected void onPostExecute(JSONObject json)

                     {
                      super.onPostExecute(json);

                       try
                         {

                           int randomNum = generateUniqueId();
                           RequestId  =System.currentTimeMillis()+""+randomNum;

                           json.put(KEY_CHANNEL_ID, stringChannelId);
                           json.put(KEY_IP_ADDRESS, str_Device_IP_Address);
                           json.put(KEY_PASSWORD, strPassWord);
                           json.put(KEY_REQUEST_ID, RequestId);
                           json.put(KEY_STATUS_FLAG, strStatusFlag);
                           json.put(KEY_REQUEST_CODE, RequestCode);
                           json.put(KEY_USERNAME, strUserName);

                            String jstr = json.toString();
                            System.out.println("jstr =" +jstr);

                                String reqvalues=AES128Bit.encrypt(jstr,key);

                                System.out.println("reqvalues =" +reqvalues);

                                JSONObject jSONObject = new JSONObject();
                                jSONObject.put(KEY_REQUEST, reqvalues);
                                jSONObject.put(KEY_VENDOR_ID, "1");
                                String jsonString = jSONObject.toString();
                                System.out.println("jsonString =" +jsonString);


                                JSONObject outer = new JSONObject();
                                outer.put("W2INBCWS", jsonString);
                                String outerJSONObjectResponse=outer.toString();
                                System.out.println("outerJSONObjectResponse =" +outerJSONObjectResponse);


                               //  outerJSONObjectResponse=outerJSONObjectResponse.replace("true", "\"true\"");
                               // JSONObject dataJSONObject = new JSONObject(outerJSONObjectResponse);
                               // String strGetResponseFromJson=dataJSONObject.getString("Response");
                               String strGetResponseFromJson = AES128Bit.decrypt( outerJSONObjectResponse ,key); 
                               System.out.println("strGetResponseFromJson =" +strGetResponseFromJson);



                         } 

                         catch (JSONException e)

                         {
                            e.printStackTrace();
                         } 

                         catch (Exception e) 
                         {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                       pDialog.dismiss();

                     }

                     }

Hare是我的日志猫堆栈跟踪信息

12-10 11:25:15.890: E/AndroidRuntime(589): FATAL EXCEPTION: main
12-10 11:25:15.890: E/AndroidRuntime(589): java.lang.NoClassDefFoundError: com.anm.cms.core.util.AES128Bit
12-10 11:25:15.890: E/AndroidRuntime(589):  at com.window2india.Login$GetJSONParse.onPostExecute(Login.java:202)
12-10 11:25:15.890: E/AndroidRuntime(589):  at com.window2india.Login$GetJSONParse.onPostExecute(Login.java:1)
12-10 11:25:15.890: E/AndroidRuntime(589):  at android.os.AsyncTask.finish(AsyncTask.java:417)
12-10 11:25:15.890: E/AndroidRuntime(589):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
12-10 11:25:15.890: E/AndroidRuntime(589):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
12-10 11:25:15.890: E/AndroidRuntime(589):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-10 11:25:15.890: E/AndroidRuntime(589):  at android.os.Looper.loop(Looper.java:130)
12-10 11:25:15.890: E/AndroidRuntime(589):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-10 11:25:15.890: E/AndroidRuntime(589):  at java.lang.reflect.Method.invokeNative(Native Method)
12-10 11:25:15.890: E/AndroidRuntime(589):  at java.lang.reflect.Method.invoke(Method.java:507)
12-10 11:25:15.890: E/AndroidRuntime(589):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-10 11:25:15.890: E/AndroidRuntime(589):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-10 11:25:15.890: E/AndroidRuntime(589):  at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案