package com.eactive.eai.common.util;

/**
 * 1.16Byte Server Key를 셋팅한다.
 * 2.샘플을 보고 사용한다.
 * 
 */

public class Seed {

	public static void main(String[] args) throws Exception {
		//System.out.println("SEED Crypt Sample");

		String[] text = {"1410466"};
		//String text = "테스트_\t/\\1234!@#$<>?_+|";
		// 초기문자 확인

		// 2.암호화
		for (int inx = 0; inx < text.length; inx++) {
			String encryptStr = encrypt(text[inx] );
			System.out.println(text[inx] + "  " + encryptStr);
		}
		String[] codes = {"CEB,CIB,CAR,CUM,CAG,CMB,CTM,CAT,CFD",
				"LCE,LRA,LMC,LAC,LPC,LMN,LEN,LCI",
				"LEN                                                  ",
				"LRA,LMC,LAC,LPC,LEN,VMM,VMR,VPR,VBL,VST,VAC,VAP      ",
				"LRA,LMC,LAC,LPC,LEN,VMM,VMR,VPR,VBL,VST,VAC,VAP      ",
				"LRA,LMC,LAC,LPC,LEN,VMM,VMR,VPR,VBL,VST,VAC,VAP      ",
				"LRA,LMC,LAC,LPC,LEN,VMM,VMR,VPR,VBL,VST,VAC,VAP      ",
				"VMM,VMR,VPR,VBL,VST,VAC,VAP                          ",
				"GDA, GLM, GRP, GST                                   "};
		
		for (int inx = 0; inx < text.length; inx++) {
			String[] idCode = codes[inx].split(",");
			for (int jnx = 0; jnx < idCode.length; jnx++) {
				System.out.println("INSERT INTO TSEAIRM06 (USERID, BZWKDSTCD) VALUES ('" +text[inx] + "', '"+idCode[jnx].replaceAll(" ", "")+"');"  );
			}
		}

		for (int inx = 0; inx < text.length; inx++) {
			String[] idCode = codes[inx].split(",");
			for (int jnx = 0; jnx < idCode.length; jnx++) {
				System.out.println("INSERT INTO TSEAIRM06 (USERID, EAIBZWKDSTCD) VALUES ('" +text[inx] + "', '"+idCode[jnx].replaceAll(" ", "")+"');"  );
			}
		}
		
	}

	public static String encrypt(String planText) throws Exception {
		SeedCipher seed = new SeedCipher();

		String key = "ABCEaicd1234_+|>\n";// 16Byte Server Key
		// 1.값셋팅
		String text = planText;//2Byte이상의 문자 취

		// 2.암호화
		byte[] encryptSeedBytes = seed.encrypt(text, key.getBytes());
				
		char[] encryptSeedChars = Base64Coder.encode(encryptSeedBytes);
		String encryptText = new String(encryptSeedChars);
		
		return encryptText;
	}

	public static String decrypt(String encryptText) throws Exception {
		SeedCipher seed = new SeedCipher();

		String key = "ABCEaicd1234_+|>\n";// 16Byte Server Key
		
		// 3.복호화
		byte[] encryptBytes = Base64Coder.decode(encryptText);
		byte[] decryptSeedBytes = seed.decrypt(encryptBytes, key.getBytes());
		String decryptText = new String(decryptSeedBytes);
		
		// DeCrypt 확인
		return decryptText;
	}
}
