package seed.utils; import java.security.MessageDigest; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class nCertDecryptor { public static int BLOCK_SIZE = 16; public String decrypt(String secretKey, String iv, String cipher) throws Exception { // 암호화 알고리즘/모드/패딩 String algorithm = "AES/CBC/PKCS5PADDING"; // 비밀 키 디코딩 byte[] decodedSecretKey = keyInstance(secretKey, BLOCK_SIZE); // 초기화 벡터 디코딩 byte[] decodedIv = iv.getBytes("UTF-8"); // 암호문 디코딩 byte[] decodecCipher = nCertBase64.decode(cipher); // 복호화 객체 생성 Cipher decipherInstance = Cipher.getInstance(algorithm); // 복호화 스펙 정의 SecretKeySpec keySpec = new SecretKeySpec(decodedSecretKey, "AES"); IvParameterSpec ivSpec = new IvParameterSpec(decodedIv); // 복호화 객체 초기화 decipherInstance.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); // 복호화 byte[] plain = decipherInstance.doFinal(decodecCipher); // 복호화 된 평문(byte[])을 문자로 변환 return new String(plain, "UTF-8"); } public byte[] keyInstance(String secretkey, int size) { byte[] retHashValue = new byte[size]; try { MessageDigest md = MessageDigest.getInstance("SHA1"); byte[] hash = md.digest(secretkey.getBytes("UTF-8")); size = Math.min(size, hash.length); System.arraycopy(hash, 0, retHashValue, 0, size); }catch(Exception e) { e.printStackTrace(); } return retHashValue; } }