Java android中的Base64编码

Java android中的Base64编码,java,android,encryption,Java,Android,Encryption,我正在尝试对AES加密密码进行Base64编码。 我已经在src文件夹中添加了该文件。这是代码 package code.finalwork; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; impor

我正在尝试对AES加密密码进行Base64编码。 我已经在src文件夹中添加了该文件。这是代码

package code.finalwork;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class FinalWorkActivity extends Activity {
    private String pref_file = "pref.xml";

    TextView pass;
    TextView pass_cnf;
    TextView err_msg;
    Button done;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        pass = (TextView) findViewById(R.id.pass);
        pass_cnf = (TextView) findViewById(R.id.pass_cnf);
        err_msg = (TextView) findViewById(R.id.error_pass);
        done = (Button) findViewById(R.id.btn_done);

        SharedPreferences pref = getSharedPreferences(pref_file,
                Context.MODE_PRIVATE);
        Boolean val = pref.getBoolean("firstuse", true);
        if (val) {
            SharedPreferences.Editor mod = pref.edit();
            mod.putBoolean("firstuse", false);
            mod.commit();

        }
    }

    // ///////////////////////////////////////////////////////////////////////
    public void onclick(View view) {
        switch (view.getId()) {
        case R.id.btn_done:
            String usrpass = pass.getText().toString();
            String cnfrmpass = pass_cnf.getText().toString();
            if (usrpass.equals(cnfrmpass)) {
                byte[] password = Base64.decode(usrpass, 0);
                byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,
                        6 };
                for (int i = 0; i < usrpass.length(); i++) {
                    key[i] = password[i];
                }
                try {
                    String passtostore = encrypt(usrpass, key);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                err_msg.setText("Password added");
                err_msg.setVisibility(View.VISIBLE);
            } else {
                err_msg.setText("Password Must Match");
                err_msg.setVisibility(View.VISIBLE);
            }
            break;
        }
    }

    // //////////////////////////////////////////////////////////////////////

    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.encodeToString(encryptedbytes, 0);
        return encrypted;

    }
}  
package code.finalwork;
导入javax.crypto.Cipher;
导入javax.crypto.spec.SecretKeySpec;
导入android.app.Activity;
导入android.content.Context;
导入android.content.SharedReferences;
导入android.os.Bundle;
导入android.util.Base64;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
公共类FinalWorkActivity扩展活动{
私有字符串pref_file=“pref.xml”;
文本视图通行证;
TextView pass_cnf;
文本视图错误消息;
按钮完成;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pass=(TextView)findViewById(R.id.pass);
pass\u cnf=(TextView)findViewById(R.id.pass\u cnf);
err_msg=(TextView)findViewById(R.id.error_pass);
完成=(按钮)findViewById(R.id.btn_完成);
SharedReferences pref=GetSharedReferences(pref_文件,
上下文。模式(私人);
Boolean val=pref.getBoolean(“firstuse”,true);
if(val){
SharedReferences.Editor mod=pref.edit();
mod.putBoolean(“firstuse”,false);
mod.commit();
}
}
// ///////////////////////////////////////////////////////////////////////
公共void onclick(视图){
开关(view.getId()){
案例R.id.btn_完成:
字符串usrpass=pass.getText().toString();
字符串cnfrmpass=pass_cnf.getText().toString();
如果(usrpass.等于(cnfrmpass)){
字节[]密码=Base64.decode(usrpass,0);
字节[]键={0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,
6 };
对于(int i=0;i
如果我们对加密代码进行注释,则此代码工作正常。但有了这些行,我产生了一个错误,应用程序意外停止。

Base64用于将二进制数据编码为ASCII-7的64字符子集,可以通过基于文本的协议(如SMTP或HTTP)安全传输

这里的一个潜在问题是,您正在尝试对用户输入进行Base64解码,这只是代码此行中的一个普通字符串:

byte[] password=Base64.decode(usrpass, 0);
要将明文(字符串)密码转换为字节[],请使用:

byte[] password =  userpass.getBytes("UTF-8");  

看起来您的OnClick侦听器中有ArrayIndexOutOfBoundsException。除了@maasg所写的内容外,这些行看起来非常可疑

byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
for (int i = 0; i < usrpass.length(); i++) {
    key[i] = password[i];
}
byte[]key={0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};
对于(int i=0;i

除非您正在做的事情在代码中并不明显,否则如果usrpass比key长,您将越界

Base64不是加密。你不应该为了保守秘密而使用它。Base64是一种将任意数据编码为字符串的方法,任何人都可以获得原始内容。这与将密码保存为明文相同。问题的标题具有误导性。Base64正确用于AES加密密码的文本编码(cfr.
encrypt
)是的,谢谢。但是你能解决问题吗。你是否尝试删除
Base64.decode(userpass,0)
并用
userpass.getBytes(…)
替换它?Base64编码需要一个长度倍数为4的字符串,可能是导致边界数组的原因(取决于impl)是的,我使用了password.legth并运行了它。虽然我目前不知道输出是否正确。实际上,Base64.decode也有一个字符串版本@MikaelOhlson是的,但这对本案没有帮助。Base64解码纯文本字符串将失败(除非字符串大小是4的倍数,并且字符属于标准Base64使用的64字符子集,在这种情况下,它会给您带来一些垃圾),您是对的。我误读了你的陈述。我的错。尽管Base64解码纯文本字符串仍然不应给出ArrayIndexOutOfBoundsException,因为输入被截断,并且状态机跟踪我们是否最终处于错误状态。删除我的负一。:)我只输入了3个字符长的密码。但问题仍然存在。@Kamran,好吧,那么我的答案也不正确,尽管您确实应该检查正在操作的数组的长度。您尝试过使用password.length吗?是的,我使用了password.legth并运行了它。尽管我目前不知道输出是否正确。