개발노트... 2022

Han Gyul Kang·2022년 4월 11일
0

개발노트_2022

목록 보기
1/3

주니어 백엔드 개발자로 틀린 부분이 있다면 댓글로 혼내주셔도 좋습니다 제발



2022 04 11 : java.util.Base64

  • getUrlDecode()를 사용할 경우 illegal base64 character 2f 에러 발생
  • getDecoder()를 사용할 경우 illegal base64 character 5f 에러 발생
// 소스 원본
Byte[] byteArr = Base64.getDecoder().decode(image.getString(str_data));

public class Base64 {
    public static class Encoder {
        /**
         * This array is a lookup table that translates 6-bit positive integer
         * index values into their "Base64 Alphabet" equivalents as specified
         * in "Table 1: The Base64 Alphabet" of RFC 2045 (and RFC 4648).
         */
        private static final char[] toBase64 = {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
        };
 
        /**
         * It's the lookup table for "URL and Filename safe Base64" as specified
         * in Table 2 of the RFC 4648, with the '+' and '/' changed to '-' and
         * '_'. This table is used when BASE64_URL is specified.
         */
        private static final char[] toBase64URL = {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
        };
    }
}

  • Base64
    바이너리 데이터를 문자 코드에 영향을 받지 않는 공통 ASCII 문자로 표현하기 위해 만들어진 인코딩이다. 네이버 지식iN 등의 URL에서 자주 볼 수 있는 형태의 바로 그것. ASCII 문자 하나가 64진법의 숫자 하나를 의미하기 때문에 BASE64라는 이름을 가졌다.

  • toBase64에 '-', '_'가 없기 때문에 각각 '+', '/'로 치환하여 조치
// 소스 수정본
Byte[] byteArr = Base64.getDecoder().decode(image.getString(str_data)
						.replace('-', '+')
					    .replace('_', '/'));
profile
피아노 치는 개발자

0개의 댓글