2. 컴퓨터 이야기/프로그래밍

Java 에서 SUN 기본 라이브러리를 이용한 BASE64 암호화/복호화

래빗 크리스 2009. 10. 5. 14:52


오라클 DB에서도 지원하는데, Java 에서는 못할리가 없지.. ^^>
그런데, 이걸 일일이 프로그래밍 한 분도 있던데..
오라클이 SUN 을 흡수한 걸 기념해서(?) sun.misc.BASE64Decoder 와 sun.misc.BASE64Encoder 를 이용해 보자.
뭐 특별한 것이 없이 간단한 거니까.. static 메소드로 구현해 본다..
(아래 내용은 고정폭 글꼴에서 작업해 놓은 것인데, 가변폭 글꼴로 옮기니까.. 가독성이 떨어졌다..
파라미터나 리턴값을 byte[] 로 할 수도 있겠지만.. 여기에서는 그냥 String 으로 했다.

package 하고싶은대로만든패키지;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Base64Sun {
  
 /**
  * SUN 의 BASE64Encode 를 이용한 encode.
  * @param   String Encoding 대상
  * @return  String Encoding 결과값
  * @throws  Exception
  */
 public static String encode(String strEncode){
  
  String         result = null;

  if(strEncode==null || strEncode.trim().length()==0)
   return null;

  try{
   
   BASE64Encoder     b64e  = new BASE64Encoder();
   ByteArrayInputStream bais  = new ByteArrayInputStream(strEncode.getBytes());
   ByteArrayOutputStream baos  = new ByteArrayOutputStream();
   byte[]        bytes  = (byte[])null;

   b64e.encodeBuffer(bais, baos);
   bytes             = baos.toByteArray();
   result            = (new String(bytes)).trim();
   
  }catch(Exception e){
   StringBuffer sbLog = new StringBuffer();
   sbLog.append("\n");
   sbLog.append("---.하고싶은대로만든패키지.Base64Sun.encode(String)\n");
   sbLog.append("---.Exception:" +e.toString()+ "..\n");
   System.out.println(sbLog);
  }
  
  return result;
 }

 /**
  * SUN 의 BASE64Encode 를 이용한 decode.
  * @param   encodeBytes Decoding 대상
  * @return  String Decoding 결과값
  * @throws  Exception
  */
 public static String decode(String strDecode){
  
  String         result = null;

  if(strDecode==null || strDecode.trim().length()==0)
   return null;

  try{
   
   BASE64Decoder     b64d  = new BASE64Decoder();
   ByteArrayInputStream bais  = new ByteArrayInputStream(strDecode.getBytes());
   ByteArrayOutputStream baos  = new ByteArrayOutputStream();
   byte[]        bytes  = (byte[])null;

   b64d.decodeBuffer(bais, baos);
   bytes             = baos.toByteArray();
   result            = new String(bytes);

  }catch(Exception e){
   StringBuffer sbLog = new StringBuffer();
   sbLog.append("\n");
   sbLog.append("---.하고싶은대로만든패키지.Base64Sun.decode(String)\n");
   sbLog.append("---.Exception:" +e.toString()+ "..\n");
   System.out.println(sbLog);
  }
  
  return result;
 }

  /**
   * SUN 의 BASE64Encode 를 이용한 decode/encode 예제.
   * @param     encodeBytes Decoding 대상
   * @return    byte[] Decoding 결과값, new String(byte[]) 로 전환해서 사용
   * @throws    Exception
   */
  public static void main(String[] args) throws Exception  {
     
     
    String                srcEng        = "1234abcd";
    String                srcKor        = "1234가나다";
     
    String                encEng        = Base64Sun.encode(srcEng);
    String                encKor        = Base64Sun.encode(srcKor);
   
    String                decEng        = Base64Sun.decode(encEng);
    String                decKor        = Base64Sun.decode(encKor);
   
    System.out.println("---.srcEng:"+srcEng+"..");
    System.out.println("---.srcKor:"+srcKor+"..");
   
    System.out.println("---.encEng:"+encEng+"..");
    System.out.println("---.encKor:"+encKor+"..");
   
    System.out.println("---.decEng:"+decEng+"..");
    System.out.println("---.decKor:"+decKor+"..");
   
  }
}