android常用的AES加解密
来源:互联网 发布:非诚勿扰网络播出时间 编辑:程序博客网 时间:2024/03/28 19:51
AES是android中一种常用的对称加密算法,通常用来传输一些私密的数据来与服务端的通信,加加密之前通常会对key或者要加密的值进行base64或者其他的方式包装,再进行加密,那解密时则需要先将包装还原再进行解密,今天记录一种常用的AES标准代码。
/** * 加密 */ public static String aesEncrypt(String encrypted, String keyWord) throws Exception { String encryptStr = null; try { SecretKeySpec key = new SecretKeySpec(keyWord.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 创建密码器 cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 byte[] result = cipher.doFinal(encrypted.getBytes("utf-8")); encryptStr = parseByte2HexStr(result); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return encryptStr; } /** * 解密 */ public static String AesDecrypt(String decrypted,String keyWord) throws Exception { Key secretKey = new SecretKeySpec(keyWord.getBytes("utf-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); //初始化,设置为解密模式 cipher.init(Cipher.DECRYPT_MODE, secretKey); //执行操作 byte[] sb = HexString2Bytes(decrypted); return new String(cipher.doFinal(sb)); } /** * 将二进制转换成16进制 */ public static String parseByte2HexStr(final byte buf[]) { StringBuffer sb = new StringBuffer(); for (byte element : buf) { String hex = Integer.toHexString(element & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.toUpperCase()); } return sb.toString(); } /** * 从十六进制字符串到二进制字节数组转换 */ public static byte[] HexString2Bytes(String hexstr) { byte[] b = new byte[hexstr.length() / 2]; int j = 0; for (int i = 0; i < b.length; i++) { char c0 = hexstr.charAt(j++); char c1 = hexstr.charAt(j++); b[i] = (byte) ((parse(c0) << 4) | parse(c1)); } return b; }
0 0
- android常用的AES加解密
- 基于openssl的AES加解密 android
- AES加解密在Android 4.2上的Encryption error
- AES加解密在Android 4.2上的Encryption error
- ios与android与java通用的AES加解密
- android中使用AES加解密
- Android AES 16字节加解密
- AES加解密算法的模式介绍
- 使用AES对文件的加解密
- Java实现的AES加解密程序
- AES加解密算法的模式介绍
- 使用OpenSSL库的AES加解密
- AES方式加解密的简单介绍
- 【AES】使用OpenSSL库的AES加解密
- AES加解密算法
- aes加解密
- iOS-AES加解密
- erlang aes 加解密
- rb_tree 源码
- 两位数字金额转化为大写格式
- lcd timing的理解
- 冒泡排序
- SQL Server 2005通过LinkServer访问MySQL数据库
- android常用的AES加解密
- Linux 进程间通讯之创建无名管道和读写无名管道
- 如何理解输入输出流
- 数组的初始化
- iOS开发:无限循环轮播图
- 【重大更新】DevExpress v16.2新版亮点(Win10篇)
- 关于webservice的几个问题
- select 控件操作
- C++中的容器类【by徐玉丽】