[Java] Android通过AES128加密解密字符串 →→→→→进入此内容的聊天室

来自 , 2020-03-21, 写在 Java, 查看 130 次.
URL http://www.code666.cn/view/82edc5c9
  1. package net.sf.andhsli.hotspotlogin;
  2.  
  3. import java.security.SecureRandom;
  4.  
  5. import javax.crypto.Cipher;
  6. import javax.crypto.KeyGenerator;
  7. import javax.crypto.SecretKey;
  8. import javax.crypto.spec.SecretKeySpec;
  9.  
  10. /** download from http://www.sharejs.com
  11.  * Usage:
  12.  * <pre>
  13.  * String crypto = SimpleCrypto.encrypt(masterpassword, cleartext)
  14.  * ...
  15.  * String cleartext = SimpleCrypto.decrypt(masterpassword, crypto)
  16.  * </pre>
  17.  * @author ferenc.hechler
  18.  */
  19. public class SimpleCrypto {
  20.  
  21.         public static String encrypt(String seed, String cleartext) throws Exception {
  22.                 byte[] rawKey = getRawKey(seed.getBytes());
  23.                 byte[] result = encrypt(rawKey, cleartext.getBytes());
  24.                 return toHex(result);
  25.         }
  26.        
  27.         public static String decrypt(String seed, String encrypted) throws Exception {
  28.                 byte[] rawKey = getRawKey(seed.getBytes());
  29.                 byte[] enc = toByte(encrypted);
  30.                 byte[] result = decrypt(rawKey, enc);
  31.                 return new String(result);
  32.         }
  33.  
  34.         private static byte[] getRawKey(byte[] seed) throws Exception {
  35.                 KeyGenerator kgen = KeyGenerator.getInstance("AES");
  36.                 SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
  37.                 sr.setSeed(seed);
  38.             kgen.init(128, sr); // 192 and 256 bits may not be available
  39.             SecretKey skey = kgen.generateKey();
  40.             byte[] raw = skey.getEncoded();
  41.             return raw;
  42.         }
  43.  
  44.        
  45.         private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
  46.             SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  47.                 Cipher cipher = Cipher.getInstance("AES");
  48.             cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  49.             byte[] encrypted = cipher.doFinal(clear);
  50.                 return encrypted;
  51.         }
  52.  
  53.         private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
  54.             SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  55.                 Cipher cipher = Cipher.getInstance("AES");
  56.             cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  57.             byte[] decrypted = cipher.doFinal(encrypted);
  58.                 return decrypted;
  59.         }
  60.  
  61.         public static String toHex(String txt) {
  62.                 return toHex(txt.getBytes());
  63.         }
  64.         public static String fromHex(String hex) {
  65.                 return new String(toByte(hex));
  66.         }
  67.        
  68.         public static byte[] toByte(String hexString) {
  69.                 int len = hexString.length()/2;
  70.                 byte[] result = new byte[len];
  71.                 for (int i = 0; i < len; i++)
  72.                         result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
  73.                 return result;
  74.         }
  75.  
  76.         public static String toHex(byte[] buf) {
  77.                 if (buf == null)
  78.                         return "";
  79.                 StringBuffer result = new StringBuffer(2*buf.length);
  80.                 for (int i = 0; i < buf.length; i++) {
  81.                         appendHex(result, buf[i]);
  82.                 }
  83.                 return result.toString();
  84.         }
  85.         private final static String HEX = "0123456789ABCDEF";
  86.         private static void appendHex(StringBuffer sb, byte b) {
  87.                 sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
  88.         }
  89.        
  90. }
  91.  
  92.  
  93. //java/8700

回复 "Android通过AES128加密解密字符串"

这儿你可以回复上面这条便签

captcha