1. package EOE.android.demo;

  2. import java.security.SecureRandom;

  3. import javax.crypto.Cipher;
  4. import javax.crypto.KeyGenerator;
  5. import javax.crypto.SecretKey;
  6. import javax.crypto.spec.SecretKeySpec;

  7. /**
  8. * Usage://用法
  9. *

  10. * String crypto = SimpleCrypto.encrypt(masterpassword, cleartext)//加密方法
  11. * ...
  12. * String cleartext = SimpleCrypto.decrypt(masterpassword, crypto)//解密方法
  13. *
  14. * @author ferenc.hechler
  15. */
  16. public class SimpleCrypto {

  17. public static String encrypt(String seed, String cleartext) throws Exception {
  18. byte[] rawKey = getRawKey(seed.getBytes());
  19. byte[] result = encrypt(rawKey, cleartext.getBytes());
  20. return toHex(result);
  21. }

  22. public static String decrypt(String seed, String encrypted) throws Exception {
  23. byte[] rawKey = getRawKey(seed.getBytes());
  24. byte[] enc = toByte(encrypted);
  25. byte[] result = decrypt(rawKey, enc);
  26. return new String(result);
  27. }

  28. private static byte[] getRawKey(byte[] seed) throws Exception {
  29. KeyGenerator kgen = KeyGenerator.getInstance("AES");
  30. SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
  31. sr.setSeed(seed);
  32. kgen.init(128, sr); // 192 and 256 bits may not be available
  33. SecretKey skey = kgen.generateKey();
  34. byte[] raw = skey.getEncoded();
  35. return raw;
  36. }


  37. private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
  38. SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  39. Cipher cipher = Cipher.getInstance("AES");
  40. cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  41. byte[] encrypted = cipher.doFinal(clear);
  42. return encrypted;
  43. }

  44. private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
  45. SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  46. Cipher cipher = Cipher.getInstance("AES");
  47. cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  48. byte[] decrypted = cipher.doFinal(encrypted);
  49. return decrypted;
  50. }

  51. public static String toHex(String txt) {
  52. return toHex(txt.getBytes());
  53. }
  54. public static String fromHex(String hex) {
  55. return new String(toByte(hex));
  56. }

  57. public static byte[] toByte(String hexString) {
  58. int len = hexString.length()/2;
  59. byte[] result = new byte[len];
  60. for (int i = 0; i < len; i++)
  61. result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
  62. return result;
  63. }

  64. public static String toHex(byte[] buf) {
  65. if (buf == null)
  66. return "";
  67. StringBuffer result = new StringBuffer(2*buf.length);
  68. for (int i = 0; i < buf.length; i++) {
  69. appendHex(result, buf[i]);
  70. }
  71. return result.toString();
  72. }
  73. private final static String HEX = "0123456789ABCDEF";
  74. private static void appendHex(StringBuffer sb, byte b) {
  75. sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
  76. }

  77. }

更多相关文章

  1. android图像变为黑白
  2. android两种方法操作Sqlite数据库
  3. Android(安卓)SQLiteOpenHelper
  4. Android之Activity--Loaders
  5. Android(安卓)- LayoutInflater 的使用
  6. Android高级工程师每日一面试题精选!(1——15题)持续更新!
  7. 《第一行代码Android》笔记
  8. android 笔记 --- 自定义Android主题风格theme.xml方法
  9. Android(安卓)数字版权保护播放器开发

随机推荐

  1. Android高德地图使用之地点关键词的输入
  2. Android(安卓)组件化实现
  3. Android中批处理drawable-xxx目录中图片
  4. Android(安卓)Studio 和 Unity 之间实现
  5. Android开发的第一个例子(内附Sdk、Androi
  6. 滑轮控件研究二、GestureDetector的深入
  7. android SoftKeypad 软键盘的问题
  8. Android(安卓)TextClock的坑(系统12小时强
  9. Android(安卓)Studio - 安卓开发工具 打
  10. Android调用H5的方法.Js交互