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;}
}
- Android AES加密和解密
- Android AES 加密,解密
- Android AES加密解密
- Android AES加密解密
- Android AES加密解密
- Android AES加密 解密
- Android AES加密解密
- Android AES 加密、解密
- Android--AES加密解密
- Android中AES加密和解密
- AES加密和解密
- AES 加密和解密
- AES加密和解密
- AES加密和解密
- AES加密和解密
- AES加密解密Android版
- Android AES 文件加密解密
- Android平台 AES加密/解密
- 【Scheme归纳】2 算数运算
- JDK各个版本的新特性jdk1.5-jdk8
- php 面向对象的一个简单实例
- 用Windows命令行管理AWS资源
- SVN常用命令
- Android AES加密和解密
- Go学习笔记:匿名成员
- ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
- 不常用的标签
- 精彩课程预告:大数据实时处理–Spark及BDAS技术应用
- 堆的使用
- 新提交审核app保留检查更新入口将被拒绝
- Git 學習(2)—— 为版本库增加内容
- 连载:告诉你如何设计一个日访问量千万级别的系统,谈oracle的高级设计和开发(3)