代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
实现android的AES加密技术
作者:
DDT
/ 发布于
2012/10/22
/
1264
实现android的AES加密技
import java.security.SecureRandom; </div> <div></div> import javax.crypto.Cipher; </div> import javax.crypto.KeyGenerator; </div> import javax.crypto.SecretKey; </div> import javax.crypto.spec.SecretKeySpec; </div> <div></div> <div></div> <div>public class SimpleCrypto { </div> <div></div> <div> public static String encrypt(String seed, String cleartext) throws Exception { </div> <div> byte[] rawKey = getRawKey(seed.getBytes()); </div> <div> byte[] result = encrypt(rawKey, cleartext.getBytes()); </div> <div> return toHex(result); </div> <div> } </div> <div> </div> <div> public static String decrypt(String seed, String encrypted) throws Exception { </div> <div> byte[] rawKey = getRawKey(seed.getBytes()); </div> <div> byte[] enc = toByte(encrypted); </div> <div> byte[] result = decrypt(rawKey, enc); </div> <div> return new String(result); </div> <div> } </div> <div></div> <div> private static byte[] getRawKey(byte[] seed) throws Exception { </div> <div> KeyGenerator kgen = KeyGenerator.getInstance("AES"); </div> <div> SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); </div> <div> sr.setSeed(seed); </div> <div> kgen.init(128, sr); // 192 and 256 bits may not be available </div> <div> SecretKey skey = kgen.generateKey(); </div> <div> byte[] raw = skey.getEncoded(); </div> <div> return raw; </div> <div> } </div> <div></div> <div> </div> <div> private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception { </div> <div> SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); </div> <div> Cipher cipher = Cipher.getInstance("AES"); </div> <div> cipher.init(Cipher.ENCRYPT_MODE, skeySpec); </div> <div> byte[] encrypted = cipher.doFinal(clear); </div> <div> return encrypted; </div> <div> } </div> <div></div> <div> private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception { </div> <div> SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); </div> <div> Cipher cipher = Cipher.getInstance("AES"); </div> <div> cipher.init(Cipher.DECRYPT_MODE, skeySpec); </div> <div> byte[] decrypted = cipher.doFinal(encrypted); </div> <div> return decrypted; </div> <div> } </div> <div></div> <div> public static String toHex(String txt) { </div> <div> return toHex(txt.getBytes()); </div> <div> } </div> <div> public static String fromHex(String hex) { </div> <div> return new String(toByte(hex)); </div> <div> } </div> <div> </div> <div> public static byte[] toByte(String hexString) { </div> <div> int len = hexString.length()/2; </div> <div> byte[] result = new byte[len]; </div> <div> for (int i = 0; i < len; i++) </div> <div> result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).bytue(); </div> <div> return result; </div> <div> } </div> <div></div> <div> public static String toHex(byte[] buf) { </div> <div> if (buf == null) </div> <div> return ""; </div> <div> StringBuffer result = new StringBuffer(2*buf.length); </div> <div> for (int i = 0; i < buf.length; i++) { </div> <div> appendHex(result, buf[i]); </div> <div> } </div> <div> return result.toString(); </div> <div> } </div> <div> private final static String HEX = "0123456789ABCDEF"; </div> <div> private static void appendHex(StringBuffer sb, byte b) { </div> <div> sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f)); </div> <div> } </div> <div> </div> <div>}
试试其它关键字
AES加密技术
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
DDT
贡献的其它代码
(
160
)
.
Oracle统计表的数据行和数据块信息
.
html标签闭合检测与修复
.
Powershell日期计算
.
Powershell的Base64编解码
.
Powershell并行循环
.
Powershell目录中搜索文本
.
Powershell枚举远程机器上的本地权限组
.
VBScript解析csv文件
.
快速排序之Powershell
.
批处理输出格式化时间字符串
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3