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. *

  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. }

你可能感兴趣的:(android)