Android 加密解密

  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. * <pre>
  10. * String crypto = SimpleCrypto.encrypt(masterpassword, cleartext)//加密方法
  11. * ...
  12. * String cleartext = SimpleCrypto.decrypt(masterpassword, crypto)//解密方法
  13. * </pre>
  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. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值