SHARE
TWEET

Android AES

Nesswit Apr 19th, 2013 1,085 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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top