프로그래밍/Android 짜투리 지식
[android] encrypt decrypt
선제
2012. 11. 29. 21:26
decrypt에 사용하는 클래스 : Cipher
decrypt 과정
SecretKeySpec keySpec = new SecretKeySpec(keyArr, "AES"); // AES 암호화 알고리즘으로 Cipher 초기화 Cipher cipher = Cipher.getInstance("AES"); // decrypt 모드로, 비밀키를 keySpec으로 지정 cipher.init(Cipher.DECRYPT_MODE, keySpec); // 복호화 진행(doFinal) byte[] decBytes = cipher.doFinal(toByte(source));
여기서 중요한점은 SecretKey 설정인데,
secretKey가 왔다고 그걸 그대로 getByte()로 변환해서는 안된다.
public static byte[] toByte(String hex) { byte[] ba = new byte[hex.length() / 2]; for(int i = 0; i < ba.length; i++) { ba[i] = (byte)Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16); } return ba; }