【问题标题】:SharedPreferences Base64 encoding assistance needed需要 SharedPreferences Base64 编码帮助
【发布时间】:2012-10-11 16:32:22
【问题描述】:

我的应用程序应该从 EditText 字段接收用户名和密码,使用 base64 对其进行加密并将它们存储在 sharedPreferences 中它无法正常工作。该程序编译并运行没有错误,但不加密数据。数据以明文形式存储。不知道这段代码哪里出错了。

代码:

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();

                try {
                    myEditor.putString("USERNAME_KEY", user);
                    byte[ ] superSecretKeyBytes = Base64.decode(user);
                    byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
                    for (int i = 0; i < pass.length(); i++) {
                        key[i] = superSecretKeyBytes[i];
                    }
                    } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    myEditor.putString("PASSWORD_KEY", pass);
                    byte[ ] superSecretKeyBytes = Base64.decode(pass);
                    byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
                    for (int i = 0; i < pass.length(); i++) {
                        key[i] = superSecretKeyBytes[i];
                    }
                    } catch (IOException 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;

}

【问题讨论】:

  • 我没有看到对您的加密函数的调用。它被称为?你有调试器吗?

标签: android encryption base64 sharedpreferences


【解决方案1】:

我相信你犯了两个错误:

  1. 在将值写入字符串 user 之前,您正在调用 myEditor.putString("USERNAME_KEY", user);。密码也是如此。
  2. 当您想对字符串 userpass 进行编码时,您调用的是 Base64.decode() 而不是 Base64.encode()

【讨论】:

  • 感谢您的关注。当我从 Base64.decode() 更改为 Base64.encode() 时,我收到以下错误“Base64 类型中的方法 encode(ByteBuffer, ByteBuffer) 不适用于参数 (String)”
  • 您需要将 String 转换为 ByteBuffer。请参阅developer.android.com/reference/android/util/Base64.html 中的文档。问候
猜你喜欢
  • 1970-01-01
  • 2014-04-06
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 2011-09-12
  • 2014-07-10
  • 2019-02-12
  • 2020-08-08
相关资源
最近更新 更多