You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
169 lines
5.4 KiB
169 lines
5.4 KiB
/* |
|
* To change this template, choose Tools | Templates |
|
* and open the template in the editor. |
|
*/ |
|
|
|
package kr.co.kihyun.util; |
|
|
|
import java.io.ByteArrayInputStream; |
|
import java.io.ByteArrayOutputStream; |
|
import java.io.IOException; |
|
import java.net.URLDecoder; |
|
import java.net.URLEncoder; |
|
import sun.misc.*; |
|
/** |
|
* |
|
* @author Kts |
|
*/ |
|
public class Base64 { |
|
|
|
public Base64() { |
|
|
|
} |
|
|
|
/** |
|
* Base64로 인코딩 하기 위해 Byte 배열로 변환 후 Base64Encoding 방식으로 바이트 배열을 아스키 문자열로 인코딩한다. |
|
* |
|
* @return String |
|
*/ |
|
private static String getEnCodeBase64Str(String xmlfilename) { |
|
|
|
String encode = null; |
|
String tmpName = null; |
|
byte[] byteOrgin = null; |
|
|
|
try { |
|
tmpName = xmlfilename; |
|
byteOrgin = tmpName.getBytes(); |
|
encode = encodeBase64FromByte(byteOrgin); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
|
|
return encode; |
|
} |
|
|
|
/** |
|
* Base64Encoding 방식으로 바이트 배열을 아스키 문자열로 인코딩한다. In-Binany, Out-Ascii |
|
* |
|
* @param encodeBytes |
|
* 인코딩할 바이트 배열(byte[]) |
|
* @return 인코딩된 아스키 문자열(String) |
|
*/ |
|
public static String encodeBase64FromByte(byte[] encodeBytes) { |
|
byte[] buf = null; |
|
String strResult = null; |
|
BASE64Encoder base64Encoder = new BASE64Encoder(); |
|
ByteArrayInputStream bin = new ByteArrayInputStream(encodeBytes); |
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
|
|
|
try { |
|
base64Encoder.encodeBuffer(bin, bout); |
|
//44.적절하지 않은 예외처리(광범위예외클래스)_CWE-754 : Add by YOUNGJUN,CHO |
|
} catch (IOException ioex) { |
|
ioex.printStackTrace(); |
|
//++++++++++++++++++++++++++++++++++++++++++++++++ |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
|
|
buf = bout.toByteArray(); |
|
strResult = new String(buf).trim(); |
|
|
|
return strResult; |
|
} |
|
|
|
//46.메소드이름 생성규칙 위반 Update by YOUNGJUN,CHO |
|
//public static String Base64encoding(String str) { |
|
public static String base64encoding(String str) { |
|
//================================================ |
|
if(str == null || str.equals("")) { |
|
return ""; |
|
} else { |
|
try { |
|
BASE64Encoder en = new BASE64Encoder(); |
|
str = URLEncoder.encode(str, "UTF-8"); |
|
str = en.encode(str.getBytes()); |
|
//44.적절하지 않은 예외처리(광범위예외클래스)_CWE-754 : Add by YOUNGJUN,CHO |
|
} catch (IOException ioex) { |
|
ioex.printStackTrace(); |
|
//++++++++++++++++++++++++++++++++++++++++++++++++ |
|
} catch(Exception e) { |
|
e.printStackTrace(); |
|
} |
|
} |
|
return str; |
|
} |
|
|
|
/* |
|
* Base 64 한글 Decoding |
|
*/ |
|
//46.메소드이름 생성규칙 위반 Update by YOUNGJUN,CHO |
|
//public static String Base64decoding(String str) { |
|
public static String base64decoding(String str) { |
|
//================================================ |
|
|
|
if(str == null || str.equals("")) { |
|
return ""; |
|
} else { |
|
try { |
|
BASE64Decoder de = new BASE64Decoder(); |
|
byte[] deStr = de.decodeBuffer(str); |
|
str = URLDecoder.decode(deStr.toString(), "UTF-8"); |
|
//44.적절하지 않은 예외처리(광범위예외클래스)_CWE-754 : Add by YOUNGJUN,CHO |
|
} catch (IOException ioex) { |
|
ioex.printStackTrace(); |
|
//++++++++++++++++++++++++++++++++++++++++++++++++ |
|
} catch(Exception e) { |
|
e.printStackTrace(); |
|
} |
|
} |
|
return str; |
|
} |
|
|
|
public static String encode(byte[] encodeBytes) { |
|
byte[] buf = null; |
|
String strResult = null; |
|
|
|
BASE64Encoder base64Encoder = new BASE64Encoder(); |
|
ByteArrayInputStream bin = new ByteArrayInputStream(encodeBytes); |
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
|
|
|
try { |
|
base64Encoder.encodeBuffer(bin, bout); |
|
|
|
//44.적절하지 않은 예외처리(광범위예외클래스)_CWE-754 : Add by YOUNGJUN,CHO |
|
} catch (IOException ioex) { |
|
ioex.printStackTrace(); |
|
//++++++++++++++++++++++++++++++++++++++++++++++++ |
|
} catch (Exception e) { |
|
System.out.println("Exception"); |
|
e.printStackTrace(); |
|
} |
|
buf = bout.toByteArray(); |
|
strResult = new String(buf).trim(); |
|
return strResult; |
|
} |
|
|
|
public static byte[] decode(String strDecode) { |
|
byte[] buf = null; |
|
|
|
BASE64Decoder base64Decoder = new BASE64Decoder(); |
|
ByteArrayInputStream bin = new ByteArrayInputStream(strDecode.getBytes()); |
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
|
|
|
try { |
|
base64Decoder.decodeBuffer(bin, bout); |
|
//44.적절하지 않은 예외처리(광범위예외클래스)_CWE-754 : Add by YOUNGJUN,CHO |
|
} catch (IOException ioex) { |
|
ioex.printStackTrace(); |
|
//++++++++++++++++++++++++++++++++++++++++++++++++ |
|
} catch (Exception e) { |
|
System.out.println("Exception"); |
|
e.printStackTrace(); |
|
} |
|
|
|
buf = bout.toByteArray(); |
|
return buf; |
|
} |
|
}
|
|
|