我正在尝试使用Base64加密editText字符串,它将保存在sharedpreferences中,但是一旦字符串被编码并保存,我的.xml文件就是空白。我是一个新手程序员,非常新加密,所以我迷路了,不知道如何正确分析我的代码,以确定可能非常简单的问题。我收到了一些建议,但没有任何清楚地表明问题出在哪里,为什么它不起作用,以及如何解决它。我怀疑我的加密方法,但不知道在哪里。对于试图保护用户名和密码条目的编码人员来说,这必须是一项常见任务。本教程用于此加密项目Click Here
这是编辑,加密和保存EditText字符串的类:
public void onClick(View arg0) {
user=rName.getText().toString().trim();
pass=rPwd.getText().toString().trim();
if(arg0==regBttn){
if((user.length()!=0))
{
if((pass.length()!=0))
{
sp=getSharedPreferences("AccessApp",MODE_WORLD_WRITEABLE);
Editor myEditor=sp.edit();
byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
try {
String encryptedUser = encrypt(user, key);
myEditor.putString("USERNAME_KEY", encryptedUser);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String encryptedPass = encrypt(pass, key);
myEditor.putString("PASSWORD_KEY", encryptedPass);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myEditor.commit();
Toast.makeText(this, "Registration is successfull",10000).show();
i=new Intent(this,AccessApp.class);
startActivity(i);
}
else
{
Toast.makeText(this, "Please Enter password", 10000).show();
}}
else{
Toast.makeText(this,"Please Enter Username",10000).show();
}
}
else if(arg0==rtnBttn){
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
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
arg0.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
}
public String encrypt(String toEncrypt, byte key[]) throws Exception {
SecretKeySpec secret = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secret);
byte[] encryptedBytes = cipher.doFinal(toEncrypt.getBytes());
String encrypted = Base64.encodeBytes(encryptedBytes);
return encrypted;
}
}
logcat的:
11-11 23:41:59.873: W/System.err(11404): java.security.InvalidKeyException: Key length not 128/192/256 bits.
11-11 23:41:59.873: W/System.err(11404): at com.android.org.bouncycastle.jce.provider.JCEBlockCipher.engineInit(JCEBlockCipher.java:570)
11-11 23:41:59.894: W/System.err(11404): at com.android.org.bouncycastle.jce.provider.JCEBlockCipher.engineInit(JCEBlockCipher.java:617)
11-11 23:41:59.903: W/System.err(11404): at javax.crypto.Cipher.init(Cipher.java:519)
11-11 23:41:59.928: W/System.err(11404): at javax.crypto.Cipher.init(Cipher.java:479)
11-11 23:41:59.943: W/System.err(11404): at com.SharedPreferences.Login.SharedPrefLoginActivity.encrypt(SharedPrefLoginActivity.java:139)
11-11 23:41:59.953: W/System.err(11404): at com.SharedPreferences.Login.SharedPrefLoginActivity.onClick(SharedPrefLoginActivity.java:82)
11-11 23:41:59.953: W/System.err(11404): at android.view.View.performClick(View.java:3511)
11-11 23:41:59.953: W/System.err(11404): at android.view.View$PerformClick.run(View.java:14105)
11-11 23:41:59.973: W/System.err(11404): at android.os.Handler.handleCallback(Handler.java:605)
11-11 23:41:59.973: W/System.err(11404): at android.os.Handler.dispatchMessage(Handler.java:92)
11-11 23:42:00.034: W/System.err(11404): at android.os.Looper.loop(Looper.java:137)
11-11 23:42:00.034: W/System.err(11404): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-11 23:42:00.034: W/System.err(11404): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 23:42:00.103: W/System.err(11404): at java.lang.reflect.Method.invoke(Method.java:511)
11-11 23:42:00.103: W/System.err(11404): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-11 23:42:00.124: W/System.err(11404): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-11 23:42:00.124: W/System.err(11404): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:4)
你似乎错误地计算了你的字节:
byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
你需要一个128位密钥(16字节),但你在这里声明了17个字节。将密钥长度减少一个,我认为它应该可以工作。