使用base64在Android中进行AES加密解密

时间:2014-03-20 10:41:33

标签: android encryption base64 aes

我正在使用Base64在Android中使用AES算法加密解密数据。 我能够加密数据但是在解密数据时遇到问题(当我点击解密按钮时它没有显示结果)。 早些时候我也在PHP中使用AES算法,它工作得非常好! 我无法在这里得到什么问题(在android中)。我正在上传我在android中完成的代码,plzzz帮助我...

package com.example.nw_encrypt_decrypt;

import java.security.SecureRandom;

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

import android.os.Bundle;
import android.app.Activity;
import android.util.Base64;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

EditText data;
Button bencrypt;
Button bdecrypt;
TextView encdec;

static byte[] raw = new byte[]{'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'};
static SecureRandom rnd = new SecureRandom();
static IvParameterSpec iv = new IvParameterSpec(rnd.generateSeed(16));

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    data = (EditText) findViewById(R.id.editText1);
    String plaintext = data.getText().toString();
    bencrypt =(Button) findViewById(R.id.button1);
    bdecrypt =(Button) findViewById(R.id.button2);
    encdec  =(TextView) findViewById(R.id.textView1);

    //"1234567890123456"
    final String mencrypt = encrypt(plaintext);
    //System.out.println("decrypted value:" + (decrypt("ThisIsASecretKey", mencrypt)));



    bencrypt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub


            encdec.setText("encrypted value : "+mencrypt);
        }
    });


    bdecrypt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            encdec.setText("decrypted value : " + (decrypt("ThisIsASecretKey", mencrypt)));
        }
    });
}


public static String encrypt(String value) {
    try {

        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec,iv);
        byte[] encrypted = cipher.doFinal(value.getBytes());
        //System.out.println("encrypted string:" + Base64.encodeBase64String(encrypted));
        //encdec.setText("encrypted string:" + Base64.encodeBase64String(encrypted));
        return Base64.encodeToString(encrypted,Base64.DEFAULT);


        //return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

public static String decrypt(String key, String encrypted) {
    try {
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec,iv);
        //byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));

        byte[] original = cipher.doFinal(Base64.decode(encrypted, Base64.DEFAULT));
        //String original = cipher.doFinal(Base64.decode(encrypted, 0));

        return new String(original);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

1 个答案:

答案 0 :(得分:0)

问题在于,在decrypt()功能中,您不像raw中那样使用encrypt()键,而是在字符串上使用getBytes()调用。由于您未在getBytes()的调用中指定字符编码,因此您将获得系统默认值,可能是UTF-16。这意味着返回的字节不是'T','h'等,而是'T',0,'h',0等。

用于解密的不同密钥意味着结果将是随机查看垃圾。

解决方案是为getBytes()中的编码指定“UTF-8”,或者更好,只需像raw中那样使用encrypt()