Android AES加密和解密

来源:互联网 发布:淘宝最火情趣丝袜模特 编辑:程序博客网 时间:2024/06/09 16:43

在移动开发中,不论IOS还是Android都支持基于PCK5Padding的AES加密,本文提供实现方法。也算自己造轮子了
Demo下载地址:http://download.csdn.net/detail/u010375704/8494565
MainActivity.java
package com.hongyan.encryption_aes;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
* The Demo is designed to test the encryption and decryption
*/
public class MainActivity extends Activity implements OnClickListener {

private TextView tv_jiami, tv_jiemi;private Button btn_jiami, btn_jiemi, btn_reset;private EditText et_code;private static final String PASSWORD = "hongyan";private String encryptCode = "";private String decryptCode = "";@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    initViews();}private void initViews() {    tv_jiami = (TextView) findViewById(R.id.tv1);    tv_jiemi = (TextView) findViewById(R.id.tv2);    btn_jiami = (Button) findViewById(R.id.btn_Encryption);    btn_jiemi = (Button) findViewById(R.id.btn_Decryption);    btn_reset = (Button) findViewById(R.id.btn_reset);    et_code = (EditText) findViewById(R.id.edittext1);    btn_jiami.setOnClickListener(this);    btn_jiemi.setOnClickListener(this);    btn_reset.setOnClickListener(this);}@Overridepublic void onClick(View v) {    switch (v.getId()) {    case R.id.btn_Encryption:// 加密        String str = et_code.getText().toString();        if (str == null || "".equals(str)) {            Toast.makeText(MainActivity.this, "请输入要加密的字符串",                    Toast.LENGTH_LONG).show();            return;        }        encryptCode = AESHelper.encrypt(str, PASSWORD);        tv_jiami.setText(encryptCode);        break;    case R.id.btn_Decryption:// 解密        if (encryptCode.equals("")) {            Toast.makeText(MainActivity.this, "请先加密", Toast.LENGTH_LONG)                    .show();            return;        }        decryptCode = AESHelper.decrypt(encryptCode, PASSWORD);        tv_jiemi.setText(decryptCode);        break;    case R.id.btn_reset:// 重置        encryptCode = "";        decryptCode = "";        et_code.setText("");        tv_jiami.setText("");        tv_jiemi.setText("");        break;    }}

}

AESHelper.java
package com.hongyan.encryption_aes;

import java.io.UnsupportedEncodingException;

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

///* AES对称加密解密类 */
public class AESHelper {

private static final String CipherMode = "AES/ECB/PKCS5Padding";// /** 创建密钥 **/private static SecretKeySpec createKey(String password) {    byte[] data = null;    if (password == null) {        password = "";    }    StringBuffer sb = new StringBuffer(32);    sb.append(password);    while (sb.length() < 32) {        sb.append("0");    }    if (sb.length() > 32) {        sb.setLength(32);    }    try {        data = sb.toString().getBytes("UTF-8");    } catch (UnsupportedEncodingException e) {        e.printStackTrace();    }    return new SecretKeySpec(data, "AES");}// /** 加密字节数据 **/public static byte[] encrypt(byte[] content, String password) {    try {        SecretKeySpec key = createKey(password);        System.out.println(key);        Cipher cipher = Cipher.getInstance(CipherMode);        cipher.init(Cipher.ENCRYPT_MODE, key);        byte[] result = cipher.doFinal(content);        return result;    } catch (Exception e) {        e.printStackTrace();    }    return null;}// /** 加密(结果为16进制字符串) **/public static String encrypt(String content, String password) {    byte[] data = null;    try {        data = content.getBytes("UTF-8");    } catch (Exception e) {        e.printStackTrace();    }    data = encrypt(data, password);    String result = byte2hex(data);    return result;}// /** 解密字节数组 **/public static byte[] decrypt(byte[] content, String password) {    try {        SecretKeySpec key = createKey(password);        Cipher cipher = Cipher.getInstance(CipherMode);        cipher.init(Cipher.DECRYPT_MODE, key);        byte[] result = cipher.doFinal(content);        return result;    } catch (Exception e) {        e.printStackTrace();    }    return null;}// /** 解密16进制的字符串为字符串 **/public static String decrypt(String content, String password) {    byte[] data = null;    try {        data = hex2byte(content);    } catch (Exception e) {        e.printStackTrace();    }    data = decrypt(data, password);    if (data == null)        return null;    String result = null;    try {        result = new String(data, "UTF-8");    } catch (UnsupportedEncodingException e) {        e.printStackTrace();    }    return result;}// /** 字节数组转成16进制字符串 **/public static String byte2hex(byte[] b) { // 一个字节的数,    StringBuffer sb = new StringBuffer(b.length * 2);    String tmp = "";    for (int n = 0; n < b.length; n++) {        // 整数转成十六进制表示        tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));        if (tmp.length() == 1) {            sb.append("0");        }        sb.append(tmp);    }    return sb.toString().toUpperCase(); // 转成大写}// /** 将hex字符串转换成字节数组 **/private static byte[] hex2byte(String inputString) {    if (inputString == null || inputString.length() < 2) {        return new byte[0];    }    inputString = inputString.toLowerCase();    int l = inputString.length() / 2;    byte[] result = new byte[l];    for (int i = 0; i < l; ++i) {        String tmp = inputString.substring(2 * i, 2 * i + 2);        result[i] = (byte) (Integer.parseInt(tmp, 16) & 0xFF);    }    return result;}

}

这是应用截图

1 0
原创粉丝点击