Android String Decrypt Base64

时间:2012-11-28 19:36:24

标签: android base64 encryption

尝试解密登录页面的base64字符串并收到错误“输入字符串为空”。 Logcat指向第55行和第139行,但我不确定如何重新编码。我是java和加密的新手,但知道解决方案很简单。请参阅下面的logcat和解密类。

logcat的:

11-12 04:51:49.273: W/System.err(22312): java.lang.NullPointerException: Input string was null.
11-12 04:51:49.273: W/System.err(22312):    at com.SharedPreferences.Login.Base64.decode(Base64.java:1243)
11-12 04:51:49.283: W/System.err(22312):    at com.SharedPreferences.Login.Base64.decode(Base64.java:1224)
11-12 04:51:49.283: W/System.err(22312):    at com.SharedPreferences.Login.AccessApp.decrypt(AccessApp.java:139)
11-12 04:51:49.293: W/System.err(22312):    at com.SharedPreferences.Login.AccessApp.onClick(AccessApp.java:55)
11-12 04:51:49.293: W/System.err(22312):    at android.view.View.performClick(View.java:3511)
11-12 04:51:49.293: W/System.err(22312):    at android.view.View$PerformClick.run(View.java:14105)
11-12 04:51:49.303: W/System.err(22312):    at android.os.Handler.handleCallback(Handler.java:605)
11-12 04:51:49.303: W/System.err(22312):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 04:51:49.303: W/System.err(22312):    at android.os.Looper.loop(Looper.java:137)
11-12 04:51:49.303: W/System.err(22312):    at android.app.ActivityThread.main(ActivityThread.java:4424)
11-12 04:51:49.303: W/System.err(22312):    at java.lang.reflect.Method.invokeNative(Native Method)
11-12 04:51:49.313: W/System.err(22312):    at java.lang.reflect.Method.invoke(Method.java:511)
11-12 04:51:49.313: W/System.err(22312):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-12 04:51:49.313: W/System.err(22312):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-12 04:51:49.313: W/System.err(22312):    at dalvik.system.NativeStart.main(Native Method)

解密类(AccessApp):

44. public void onClick(View arg0) {
45. 
46.   sp=this.getSharedPreferences("AccessApp", MODE_WORLD_READABLE);
47.   
48.    
49.    
50.    
51.   byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 };
52.    
53.    
54.   try {
55.        String decryptedUser = decrypt(user, key);  
56.         user = sp.getString("USERNAME_KEY", decryptedUser);
57.          
58.   }
59. catch (Exception e) {
60.    // TODO Auto-generated catch block
61.   e.printStackTrace();
62.   }   
63.   try {
64.        String decryptedPass = decrypt(pass, key);  
65.        pass = sp.getString("PASSWORD_KEY", decryptedPass);
66.         
67.
68.   } catch (Exception e) {
69.     // TODO Auto-generated catch block
70.    e.printStackTrace();
71.   }
72.   
73.   if(lBttn.equals(arg0)){
74.     
75.      if((uname.getText().toString().equals(user))&& 
76.        (pword.getText().toString().equals(pass)))
77.       
78.            {
79.          Toast.makeText(this, "You are Logged In", 20000).show();
80.                 
81.               Intent intent;
82.                intent=new Intent(this,details.class);
83.                startActivity(intent);
84.              flag=1;
85.            }




135.         public static String decrypt(String encryptedText, byte[ ] key) throws Exception   {
136.    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
137.    Cipher cipher = Cipher.getInstance("AES");
138.    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
139.    byte[] toDecrypt = Base64.decode(encryptedText);
140.    byte[] encrypted = cipher.doFinal(toDecrypt);
141.    return new String(encrypted);
142.   }
143.  }

1 个答案:

答案 0 :(得分:0)

如果您在SharedPreferences中存储了加密的用户名和密码,则需要在之前获取编码版,然后尝试对其进行解码。

56.        user = sp.getString("USERNAME_KEY", null);
55.        String decryptedUser = decrypt(user, key);
....
65.        pass = sp.getString("PASSWORD_KEY", null);
64.        String decryptedPass = decrypt(pass, key);