<pre name="code" class="plain"><span style="font-family:Microsoft YaHei;font-size:18px;">前面写了经常使用了MD5加密算法。一个肯定不能满足本屌丝的胃口,近期发现AES加密算法貌似挺牛逼的样子。还是是美国联邦政府採用的一种区块高级加密标准。一看到“高级”就把我吓尿了,果然牛逼,废话少说,先学会用再说。

</span>

/**
* @param
* @return AES加密算法加密
* @throws Exception
*/
public static String encrypt(String seed, String cleartext)
throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
/**
* @param
* @return AES加密算法加密
* @throws Exception
*/
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static String toHex(byte[] buf) {
final String HEX = "0123456789ABCDEF";
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2 * buf.length);
for (int i = 0; i < buf.length; i++) {
result.append(HEX.charAt((buf[i] >> 4) & 0x0f)).append(
HEX.charAt(buf[i] & 0x0f));
}
return result.toString();
}
/**
* @param raw
* @param encrypted
* @return AES加密算法解密
* @throws Exception
*/
public static String decrypt(String seed, String encrypted)
throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}
private static byte[] decrypt(byte[] raw, byte[] encrypted)
throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
private static byte[] toByte(String hexString) {
int len = hexString.length() / 2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
16).byteValue();
return result;
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
測试代码:
<span style="font-family:Courier New;font-size:18px;">        String masterPassword = "test";
String originalText = "0123456789";
String encryptingCode = SimpleCrypto.encrypt(masterPassword,originalText);
Log.i("加密结果为 ",encryptingCode);
String decryptingCode = SimpleCrypto.decrypt(masterPassword, encryptingCode);
Log.i("解密结果",decryptingCode); </span>

PS:AES属于对称加密方法,可破解有木有!

最新文章

  1. Android进度条学习
  2. H5中REM中使用的规则
  3. &lt;Interview Problem&gt;最小的“不重复数”
  4. 曼慧尼特u检验(两个样本数据间有无差异)
  5. BizTalk开发系列(二十一) Mapping 扩展开发
  6. geckodriver v0.11.0 github上下载的
  7. Interoperability between Java and SharePoint 2013 on Premises
  8. Firefox浏览器设置字符编码格式
  9. h2database源码浅析:SQL语句的执行
  10. 大型Web应用运行时 PHP负载均衡指南
  11. Ubuntu14.04 weblogic11g集群环境测试
  12. iOS 之 支付
  13. window下安装 node ,并搭建 vue 项目
  14. NumsCount
  15. 密码发生器|2012年蓝桥杯B组题解析第八题-fishers
  16. MATLAB中mexFunction函数的接口规范(转)
  17. 使用nexus 管理pip 私有包
  18. RecyclerView.Adapter封装,最简单实用的BaseRecyclerViewAdapter;只需重写一个方法,设置数据链式调用;
  19. UI的设计,适配器,以及RecyclerView无法加载的解决办法
  20. Apache Kafka系列(一) 起步

热门文章

  1. RMQ问题心得
  2. Hibernate 配置文件与实体类
  3. POJ 3608 Bridge Across Islands (旋转卡壳)
  4. 【枚举】【前缀和】【map】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly&#39;s Chemicals
  5. python3 开发面试题(去重保持原来的顺序)6.2
  6. 10.3(Java学习笔记)JDBC时间操作
  7. @RequestParam注解的使用
  8. 使用openssl生成密钥、加密和签名
  9. [Bug]CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temp
  10. 转载:oracle11G 已开启监听,但远程连接依旧无监听解决过程