public class EncryptionHelper {
public static byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
private static String key = "KEY";
public static String AES_Encode(String str) {
byte[] textBytes = new byte[0];
Cipher cipher = null;
String result = null;
try {
textBytes = str.getBytes("UTF-8");
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
result = Base64.encodeToString(cipher.doFinal(textBytes), 0);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return result.trim();
}
public static String AES_Decode(String str) {
byte[] textBytes =Base64.decode(str, 0);
String result = null;
try{
//byte[] textBytes = str.getBytes("UTF-8");
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
result = new String(cipher.doFinal(textBytes), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return result;
}
}
'Android' 카테고리의 다른 글
유튜브 아이디를 통해 유튜브 앱으로 재생하기 (0) | 2016.07.12 |
---|---|
dp를 px로 변환하기 (0) | 2016.07.12 |
Intent를 이용한 트위터 공유하기 (0) | 2016.07.12 |
Intent를 이용한 카카오스토리 공유하기 (0) | 2016.07.12 |
Intent를 이용한 페이스북 공유하기 (0) | 2016.07.12 |