Advertisement
Nesswit

Android AES

Apr 19th, 2013
1,222
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * 참조한 문서
  3.  * http://stackoverflow.com/questions/13389870/android-4-2-broke-my-aes-encrypt-decrypt-code
  4.  */
  5. public class SimpleCrypto {
  6.     private final static String HEX = "0123456789ABCDEF";
  7.     private final static int JELLY_BEAN_4_2 = 17;
  8.    
  9.     // 암호화에 사용할 키. 원하는 값으로 바꿔주자.
  10.     private final static byte[] key = {(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
  11.  
  12.     /**
  13.      * AES 암호화
  14.      * @param seed 암호화에 사용할 seed
  15.      * @param cleartext 암호화 할 문자열
  16.      * @return 암호화된 문자열
  17.      * @throws Exception 암호화 key 사이즈가 192bit 또는 128bit로 감소함.
  18.      */
  19.     public static String encrypt(String seed, String cleartext) throws Exception {
  20.         if (cleartext == null || cleartext == "") return null;
  21.         byte[] rawKey = getRawKey(seed.getBytes());
  22.         byte[] result = encrypt(rawKey, cleartext.getBytes());
  23.         String fromHex = toHex(result);
  24.         String base64 = new String(Base64.encodeToString(fromHex.getBytes(), 0));
  25.         return base64;
  26.     }
  27.  
  28.     /**
  29.      * AES 복호화
  30.      * @param seed 복호화에 사용할 seed
  31.      * @param encrypted 복호화 할 문자열
  32.      * @return 복호화한 문자열
  33.      * @throws Exception 암호화 key 사이즈가 192bit 또는 128bit로 감소함.
  34.      */
  35.     public static String decrypt(String seed, String encrypted) throws Exception {
  36.         if (encrypted == null || encrypted == "") return null;
  37.         byte[] seedByte = seed.getBytes();
  38.         System.arraycopy(seedByte, 0, key, 0, ((seedByte.length < 16) ? seedByte.length : 16));
  39.         String base64 = new String(Base64.decode(encrypted, 0));
  40.         byte[] rawKey = getRawKey(seedByte);
  41.         byte[] enc = toByte(base64);
  42.         byte[] result = decrypt(rawKey, enc);
  43.         return new String(result);
  44.     }
  45.  
  46.  
  47.     public static byte[] encryptBytes(String seed, byte[] cleartext) throws Exception {
  48.         byte[] rawKey = getRawKey(seed.getBytes());
  49.         byte[] result = encrypt(rawKey, cleartext);
  50.         return result;
  51.     }
  52.  
  53.     public static byte[] decryptBytes(String seed, byte[] encrypted) throws Exception {
  54.         byte[] rawKey = getRawKey(seed.getBytes());
  55.         byte[] result = decrypt(rawKey, encrypted);
  56.         return result;
  57.     }
  58.  
  59.     private static byte[] getRawKey(byte[] seed) throws Exception {
  60.         KeyGenerator kgen = KeyGenerator.getInstance("AES");
  61.         SecureRandom sr = null;
  62.         if (android.os.Build.VERSION.SDK_INT >= JELLY_BEAN_4_2) {
  63.             sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
  64.         } else {
  65.             sr = SecureRandom.getInstance("SHA1PRNG");
  66.         }
  67.         sr.setSeed(seed);
  68.         try {
  69.             kgen.init(256, sr);
  70.             // kgen.init(128, sr);
  71.         } catch (Exception e) {
  72.             // Log.w(LOG, "This device doesn't suppor 256bits, trying 192bits.");
  73.             try {
  74.                 kgen.init(192, sr);
  75.             } catch (Exception e1) {
  76.                 // Log.w(LOG, "This device doesn't suppor 192bits, trying 128bits.");
  77.                 kgen.init(128, sr);
  78.             }
  79.         }
  80.         SecretKey skey = kgen.generateKey();
  81.         byte[] raw = skey.getEncoded();
  82.         return raw;
  83.     }
  84.  
  85.     private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
  86.         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  87.         Cipher cipher = Cipher.getInstance("AES");
  88.         cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  89.         byte[] encrypted = cipher.doFinal(clear);
  90.         return encrypted;
  91.     }
  92.  
  93.     private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
  94.         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  95.         Cipher cipher = Cipher.getInstance("AES");
  96.         cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  97.         byte[] decrypted = cipher.doFinal(encrypted);
  98.         return decrypted;
  99.     }
  100.  
  101.     public static String toHex(String txt) {
  102.         return toHex(txt.getBytes());
  103.     }
  104.    
  105.     public static String fromHex(String hex) {
  106.         return new String(toByte(hex));
  107.     }
  108.    
  109.     public static byte[] toByte(String hexString) {
  110.         int len = hexString.length()/2;
  111.         byte[] result = new byte[len];
  112.         for (int i = 0; i < len; i++)
  113.             result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
  114.         return result;
  115.     }
  116.  
  117.     public static String toHex(byte[] buf) {
  118.         if (buf == null)
  119.             return "";
  120.         StringBuffer result = new StringBuffer(2*buf.length);
  121.         for (int i = 0; i < buf.length; i++) {
  122.             appendHex(result, buf[i]);
  123.         }
  124.         return result.toString();
  125.     }
  126.  
  127.     private static void appendHex(StringBuffer sb, byte b) {
  128.         sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
  129.     }
  130.    
  131. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement