ㅇ.ㅇ
[Java] Error - Illegal hexadecimal character q at index 0 본문
상황
javascript로 작성된 aesUtil 파일을 rust 파일로 변환 후 encrypt 된 값을 받아와 자바에서 decrypt 하는 과정 중에 에러가 발생하였다.
에러 내용
org.apache.commons.codec.DecoderException : Illegal hexadecimal character q at index 0
문제 원인
에러내용 자체를 파악해보자면,
인덱스 0자리에서 16진수 문자열에 맞지 않는 'q'가 들어갔다는 에러이다.
당연하다.
왜냐하면 16진법 문자열은 0부터 9까지의 숫자와 A부터 F까지의 알파벳 대문자로 구성되기 때문에 'q'는 포함되지 않는다. 따라서 코드를 검토하고 유효하지 않은 16진수 문자를 찾아 수정해야지 에러를 수정할 수 있다.
결론은 javascript로 작성된 aesUtil 파일에서는 iv와 salt 값이 'CryptoJS.enc.Hex' 로 인코딩 즉, 16진수 문자열로 나왔다. 그렇지만 rust로 작성된 aesUtil에서는 hex로 변환하지 않았기 때문에 16진수가 아닌 값이 들어갈 수 있었고, 그대로 자바로 넘어와서 hex(salt)라는 메서드를 만나 이런 에러를 뱉어낼 것 같다.
그 후, rust 소스를 내가 짠 게 아니라서 수정 된 사항을 보기만 했는데 'hex::encode(iv)' 이렇게 16진수 문자열로 인코딩하도록 수정한 걸 확인하였다.
java의 DecoderException 살펴보기
아래는 자바의 org.apache.commons.codec.DecoderException 중 해당 에러를 뱉어내고 있는 부분이다.
/**
* Converts a hexadecimal character to an integer.
*
* @param ch
* A character to convert to an integer digit
* @param index
* The index of the character in the source
* @return An integer
* @throws DecoderException
* Thrown if ch is an illegal hex character
*/
protected static int toDigit(final char ch, final int index) throws DecoderException {
final int digit = Character.digit(ch, 16);
if (digit == -1) {
throw new DecoderException("Illegal hexadecimal character " + ch + " at index " + index);
}
return digit;
}
참고 : http://www.massapi.com/class/org/apache/commons/codec/DecoderException-1.html
Examples of org.apache.commons.codec.DecoderException | massapi.com
Copyright © 2018 www.massapicom. All rights reserved. All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.
www.massapi.com