我使用Base64在sharedpreferences中加密了字符串但我的解码错误了。我是加密新手,无法确定logcat错误。
logcat的:
11-12 03:57:06.593: W/System.err(20229): java.lang.NullPointerException: Input string was null.
11-12 03:57:06.603: W/System.err(20229): at com.SharedPreferences.Login.Base64.decode(Base64.java:1243)
11-12 03:57:06.603: W/System.err(20229): at com.SharedPreferences.Login.Base64.decode(Base64.java:1224)
11-12 03:57:06.603: W/System.err(20229): at com.SharedPreferences.Login.AccessApp.decrypt(AccessApp.java:137)
11-12 03:57:06.603: W/System.err(20229): at com.SharedPreferences.Login.AccessApp.onClick(AccessApp.java:55)
11-12 03:57:06.603: W/System.err(20229): at android.view.View.performClick(View.java:3511)
11-12 03:57:06.613: W/System.err(20229): at android.view.View$PerformClick.run(View.java:14105)
11-12 03:57:06.635: W/System.err(20229): at android.os.Handler.handleCallback(Handler.java:605)
11-12 03:57:06.643: W/System.err(20229): at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 03:57:06.669: W/System.err(20229): at android.os.Looper.loop(Looper.java:137)
11-12 03:57:06.673: W/System.err(20229): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-12 03:57:06.673: W/System.err(20229): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 03:57:06.673: W/System.err(20229): at java.lang.reflect.Method.invoke(Method.java:511)
11-12 03:57:06.695: W/System.err(20229): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-12 03:57:06.695: W/System.err(20229): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-12 03:57:06.695: W/System.err(20229): at dalvik.system.NativeStart.main(Native Method)
类检索保存字符串并解密:
public void onClick(View arg0) {
sp=this.getSharedPreferences("AccessApp", MODE_WORLD_READABLE);
byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 };
try {
String decryptedUser = decrypt(user, key);
user = sp.getString("USERNAME_KEY", decryptedUser);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String decryptedPass = decrypt(pass, key);
pass = sp.getString("PASSWORD_KEY", decryptedPass);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(lBttn.equals(arg0)){
if((uname.getText().toString().equals(user))&&
(pword.getText().toString().equals(pass)))
{
Toast.makeText(this, "You are Logged In", 20000).show();
Intent intent;
intent=new Intent(this,details.class);
startActivity(intent);
flag=1;
}
else
{
Toast.makeText(this, "Wrong Username or Password",20000).show();
flag=0;
}
}
else if(cBttn==arg0){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("Exit");
builder.setMessage("Do you want to exit");
builder.setCancelable(false);
builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
arg0.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
return super.onKeyDown(keyCode, event);
}
public static String decrypt(String encryptedText, byte[ ] key) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] toDecrypt = Base64.decode(encryptedText);
byte[] encrypted = cipher.doFinal(toDecrypt);
return new String(encrypted);
}
}
声明:
public class AccessApp extends Activity implements OnClickListener {
private SharedPreferences sp;
String user,pass;
Button lBttn,cBttn;
EditText uname,pword;
Intent i;
int flag=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lBttn=(Button)findViewById(R.id.login_button);
cBttn=(Button)findViewById(R.id.cancel_button);
uname=(EditText)findViewById(R.id.username);
pword=(EditText)findViewById(R.id.password);
lBttn.setOnClickListener(this);
cBttn.setOnClickListener(this);
}
答案 0 :(得分:0)
对不起。我正在改变我的答案。我之前说过的是完全错误的。
你得到错误的原因
java.lang.NullPointerException: Input string was null.
是因为你宣布了
String user;
您尚未初始化它并且正在调用
String decryptedUser = decrypt(user, key);
现在在函数decrypt
中,用户为空。