1

我正在尝试使用 Base64 加密 editText 字符串 wnich 将保存在 sharedpreferences 中,但是一旦字符串被编码并保存,我的 .xml 文件就是空白的。我是一名新手程序员,对加密非常陌生,所以我迷路了,不知道如何正确分析我的代码以确定可能非常简单的问题。我收到了一些建议,但没有清楚地表明问题出在哪里,为什么它不起作用,以及如何解决它。我怀疑我的加密方法,但不确定在哪里。对于试图保护用户名和密码条目的编码人员来说,这一定是一项常见任务。本教程用于此加密项目单击此处

这是用于编码、加密和保存 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;

}

}

日志猫:

 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)
4

1 回答 1

5

您似乎数错了字节数:

byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };

您需要一个 128 位密钥(16 个字节),但您在这里声明了 17 个字节。将密钥长度减少一,我认为它应该可以工作。

于 2012-11-13T17:49:50.493 回答