IT/JAVA
[JAVA] BASE64 인코딩,디코딩 사용하기
Kanzler
2016. 11. 16. 07:00
자바를 이용하다 보면 BASE64를 많이 사용 하게 됩니다. BASE64는 8비트 이진 데이터를 문자코드에 영향을 받지 않는 공통 ASCII영역의 문자들로만 이루어진 일련의 문자열로 바꾸는 인코딩 방식입니다. 쉽게 말하면 일종의 암호화 방식이라고도 할 수 있습니다.
BASE64를 사용 하는 간단한 예제에 대해서 소개해드리겠습니다.
apache.commons.codec 라이브러리를 이용한 예제 이므로 사용 하시전에 apache lib를 임포트 후 사용 하셔야 합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import org.apache.commons.codec.binary.Base64; /** * base64 encode, decode * */ public class Base64Test { public static void main(String args[]) { String text = "hello java"; /* base64 encoding */ byte[] encoded = Base64.encodeBase64(text.getBytes()); /* base64 decoding */ byte[] decoded = Base64.decodeBase64(encoded); System.out.println("원문 : " + text); System.out.println("Base 64 인코딩 후 : " + new String(encoded)); System.out.println("Base 64디코딩 후 : " + new String(decoded)); } } | cs |
1 2 3 4 5 | 결과 값 원문 : hello java Base 64 인코딩 후 : aGVsbG8gamF2YQ== Base 64디코딩 후 : hello java | cs |