在android中编码base 64

时间:2012-06-17 16:51:31

标签: 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;
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;

    }
}  

如果我们评论加密代码,此代码工作正常。但是使用这些行我会生成应用程序意外停止的错误。 this is what log cat shows

2 个答案:

答案 0 :(得分:1)

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

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

byte[] password=Base64.decode(usrpass, 0);

要将明文(字符串)中的密码转换为byte [],请使用:

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

答案 1 :(得分:0)

看起来你的OnClickListener中有一个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];
}

除非您正在做一些从您的代码中不明显的事情,否则如果usrpass超过密钥,您将超出范围。