Android 加密解密字符串


Android 加密解密字符串:

  1. package eoe.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.   
  38. private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {   
  39. SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");   
  40. Cipher cipher = Cipher.getInstance("AES");   
  41. cipher.init(Cipher.ENCRYPT_MODE, skeySpec);   
  42. byte[] encrypted = cipher.doFinal(clear);   
  43. return encrypted;   
  44. }   
  45. private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {   
  46. SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");   
  47. Cipher cipher = Cipher.getInstance("AES");   
  48. cipher.init(Cipher.DECRYPT_MODE, skeySpec);   
  49. byte[] decrypted = cipher.doFinal(encrypted);   
  50. return decrypted;   
  51. }   
  52. public static String toHex(String txt) {   
  53. return toHex(txt.getBytes());   
  54. }   
  55. public static String fromHex(String hex) {   
  56. return new String(toByte(hex));   
  57. }   
  58. public static byte[] toByte(String hexString) {   
  59. int len = hexString.length()/2;   
  60. byte[] result = new byte[len];   
  61. for (int i = 0; i < len; i++)   
  62. result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();   
  63. return result;   
  64. }   
  65. public static String toHex(byte[] buf) {   
  66. if (buf == null)   
  67. return "";   
  68. StringBuffer result = new StringBuffer(2*buf.length);   
  69. for (int i = 0; i < buf.length; i++) {   
  70. appendHex(result, buf[i]);   
  71. }   
  72. return result.toString();   
  73. }   
  74. private final static String HEX = "0123456789ABCDEF";   
  75. private static void appendHex(StringBuffer sb, byte b) {   
  76. sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));   
  77. }   
  78. }  

相关内容