package com.eactive.eai.custom.adapter.http.util;

import com.eactive.eai.util.TestModeChecker;

import com.cubeone.CubeOneAPI;

public class CubeOneCryptoUtil {
	private static boolean testMode = TestModeChecker.isTestMode();

	public static String encryptPI(String plainText) throws Exception {
		// FIXME : kbank - kbank내애서는 testMode 체크 제거 검토
		if (testMode) {
			return plainText;
		} else {
			String encryptedText = null;
			byte errbyte[] = new byte[5];
			byte[] inbyte;
			try {
				inbyte = plainText.getBytes();
				encryptedText = CubeOneAPI.coencbytes(inbyte, inbyte.length, "AES_PI", 11, null, null, errbyte);
			} catch (UnsatisfiedLinkError e) {
				throw new Exception("encryptPI error - ", e);
			} catch (Exception e) {
				throw new Exception("encryptPI error - ", e);
			}
			// 
			
			if (errbyte[0] != 48) {
				throw new Exception("encryptPI error - " + new String(errbyte));
			}
			return encryptedText;
		}
	}

	public static String decryptPI(String encryptedText) throws Exception {
		// FIXME : kbank - kbank내애서는 testMode 체크 제거 검토
		if (testMode) {
			return encryptedText;
		} else {
			String decryptedText = null;
			byte errbyte[] = new byte[5];
			byte[] decbyte;
			decbyte = CubeOneAPI.codecbyte(encryptedText, "AES_PI", 11, null, null, errbyte);
			if (decbyte != null) {
				decryptedText = new String(decbyte);
			}
			if (errbyte[0] != 48) {
				throw new Exception("decryptPI error - " + new String(errbyte));
			}
			return decryptedText;
		}
	}
}
