KISA SEED CBC 암호화 알고리즘에 Base64 Encoding 적용
웹 API 서비스 구현 시 보안을 위해 암호화 알고리즘을 적용할 필요가 생겼다.
암/복호화에 비공개 키를 사용하므로, 다수의 사용자에게 제공할 수 있는 암호화 방식은 아니다.
따라서, 서버 대 서버간 암호화 통신을 위한 수단으로 사용하면 좋다.
개발중인 프로젝트에선 HTTP/HTTPS 프로토콜을 통한 웹 기반 서비스를 위주로 다루고 있기에,
암호화한 String에 Base64로 인코딩하여, 웹 구간에서 텍스트가 손상되는것을 방지한다.
public static String encrypt(String userKey, String text) throws UnsupportedEncodingException {
byte pbUserKey[] = null;
byte pbData[] = null;
byte bszIV[] = {
(byte)0x026, (byte)0x08d, (byte)0x066, (byte)0x0a7,
(byte)0x035, (byte)0x0a8, (byte)0x01a, (byte)0x081,
(byte)0x06f, (byte)0x0ba, (byte)0x0d9, (byte)0x0fa,
(byte)0x036, (byte)0x016, (byte)0x025, (byte)0x001
};
pbUserKey = userKey.getBytes();
pbData = text.getBytes();
int PLAINTEXT_LENGTH = pbData.length;
byte[] cipherText = SEED_CBC_Encrypt(pbUserKey, bszIV, pbData, 0, PLAINTEXT_LENGTH);
byte[] encodeText;
String encryptText = "";
Encoder encode = Base64.getEncoder();
encodeText = encode.encode(cipherText);
encryptText = new String(encodeText, "UTF-8");
return encryptText;
}
public static String decrypt(String userKey, String text) throws UnsupportedEncodingException {
byte pbUserKey[] = null;
byte cipherData[] = null;
byte bszIV[] = {
(byte)0x026, (byte)0x08d, (byte)0x066, (byte)0x0a7,
(byte)0x035, (byte)0x0a8, (byte)0x01a, (byte)0x081,
(byte)0x06f, (byte)0x0ba, (byte)0x0d9, (byte)0x0fa,
(byte)0x036, (byte)0x016, (byte)0x025, (byte)0x001
};
Decoder decode = Base64.getDecoder();
pbUserKey = userKey.getBytes();
cipherData = decode.decode(text);
int CIPHERTEXT_LENGTH = cipherData.length;
byte[] plainText = SEED_CBC_Decrypt(pbUserKey, bszIV, cipherData, 0, CIPHERTEXT_LENGTH);
String decryptText = new String(plainText, "UTF-8");
return decryptText;
}
public static void main(String[] args) throws UnsupportedEncodingException
{
String encryptText = "[암호화 텍스트]";
System.out.println(encryptText);
String decryptText = decrypt("userkey012345678", encryptText);
System.out.println(decryptText);
String pureText = "{\"LOGIN_ID\":\"marko\",\"LOGIN_PW\":\"12345678\"}";
System.out.println(pureText);
String encryptText2 = encrypt("userkey012345678", pureText);
System.out.println(encryptText2);
}