마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

파일 복호화 시 IllegalBlockSizeException 발생

0 추천

 

AES/ECB/PKCS5Padding  모드로 파일 암호화/복호화 중입니다.

암호화는 정상적으로 되었는데, 복호화 경우 아래와 같이  exception이 발생되네요.

다른 프로젝트에서는 정상적으로 사용중인데, 어떤 부분에서 놓치고 있을까요?

해당내용으로 알아보아도 Base64 관련하여 많이 보이는데, Base64는 쓰고 있지 않습니다.

 

08-25 17:09:08.104: W/System.err(12595): javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
 
 
public static final String algorithm = "AES";
	public static final String transformation = algorithm + "/ECB/PKCS5Padding";
	
	private Key key;

	public FileCoder(Key key) {
		this.key = key;
	}
	
	/**
	 * 파일 암호화
	 * 
	 * @param source 원본 파일
	 * @param dest 대상 파일
	 * @throws Exception
	 */
	public void encrypt(File source, File dest) throws Exception {
		crypt(Cipher.ENCRYPT_MODE, source, dest);
	}
	
	/**
	 * 파일 복호화
	 * 
	 * @param source 원본 파일
	 * @param dest 대상 파일
	 * @throws Exception
	 */
	public void decrypt(File source, File dest) throws Exception {
		crypt(Cipher.DECRYPT_MODE, source, dest);
	}
	
	/**
	 * 파일을 암호화/복호화해서 Clone 파일을 만든다.
	 * 
	 * @param mode 암/복호화 모드
	 * @param source 원본 파일
	 * @param dest 대상 파일
	 * @throws Exception
	 */
	private void crypt(int mode, File source, File dest) throws Exception {
		Cipher cipher = Cipher.getInstance(transformation);
		cipher.init(mode, key);
		InputStream input = null;
		OutputStream output = null;
		try {
			input = new BufferedInputStream(new FileInputStream(source));
			output = new BufferedOutputStream(new FileOutputStream(dest));
			byte[] buffer = new byte[1024];
			int read = -1;
			while ((read = input.read(buffer)) != -1) {
				output.write(cipher.update(buffer, 0, read));
			}
			output.write(cipher.doFinal());
		} finally {
			if (output != null) {
				try { output.close(); } catch(IOException ie) {}
			}
			if (input != null) {
				try { input.close(); } catch(IOException ie) {}
			}
		}
	}
	
	
	/**
	 * 문자열을 바이트배열로 치환
	 * 
	 * @param digits 문자열
	 * @param radix 진수
	 * @return
	 * @throws IllegalArgumentException
	 * @throws NumberFormatException
	 */
	public static byte[] toBytes(String digits, int radix) throws IllegalArgumentException, NumberFormatException {
		if (digits == null) {
			return null;
		}
		if (radix != 16 && radix != 10 && radix != 8) {
			throw new IllegalArgumentException("For input radix: \"" + radix + "\"");
		}
		int divLen = (radix == 16) ? 2 : 3;
    	int length = digits.length();
    	if (length % divLen == 1) {
    		throw new IllegalArgumentException("For input string: \"" + digits + "\"");
    	}
    	length = length / divLen;
    	byte[] bytes = new byte[length];
    	for (int i = 0; i < length; i++) {
    		int index = i * divLen;
    		bytes[i] = (byte)(Short.parseShort(digits.substring(index, index+divLen), radix));
    	}
    	return bytes;
	}

 

 

개발자초심 (21,220 포인트) 님이 2015년 8월 25일 질문

1개의 답변

0 추천
복호화 하는 데이터 길이를 확인 해보세요.

패딩이 있기 때문에 무조건 16의 배수가 되어야 하는데, 아닐 경우 오류가 날 수 있을 듯 합니다.
익명사용자 님이 2015년 8월 26일 답변
답변 감사합니다.
똑같은 소스로하여 단순 암호화/복호화만 새 프로젝트에서 진행하니 정상적으로 복호화가 진행되네요.
한번 더 살펴 봐야겠습니다.
...