package kr.co.i4way.util;

import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AES256 {

    public static String alg = "AES/CBC/PKCS5Padding";

    public static void main (String[] args) throws Exception {
    	int a = 956;
    	int b = 0;
    	if(a > 0) {
			b = a + (int)(a * 0.1);
		}
    	
    	System.out.println("A = " + a);
    	System.out.println("B = " + b);
    	//String encStr = "SHrqns7It6AOBKM+Ie91/KW3bJ/F6x8pQtTA0GR5pKcfhS+FRVXeBwHoYI/9k96actcmEQyo/3fhzXDd9sw2joz4+9kDfwactfbY6MFqkWhgtgkxVknZfQ8x+MABF5srudGg3UxcCcaCGkf5exbYXdN5FfeoQcrIxujYyHOpLGZaWk7Qtytta0AzkicokqXQ/cHSrzv8GDBBHxByE+n5taVd8m8IGzB6D+fa5oZ/LEMJhlEFEQdSjvn3Z8/stwgP+98xYH07Au9BfETaWvemYW3733uf3jYVICk6s6mNLSf88k0171wo9Mi/WBDzZNReKYrbNLVVzqKQAIHVMM9n/LPEwD7mXw+sHbuB0DWF7oLRzSnhS6Wl1LT9habCA4u5djP61h51cZ1VIxiIrFXZYpPazzUjehK8d7os2+6fwPZYG2wlLJUZ3JpYZyEOQlgWtyJsbQ9qNhBweQQPHOI2wmRYOlMaSsBmWFu857TmbKTKQxqfBqWcfYtdkBubikc4iQtlJqaLCprtFR/m07rfe47CqKjd6O1MowEcDyWw/d5myEpNKmTSUzo/oervaEF8zCzaN4eIuQcPEJcfjdb/AJSmrPWngJfr6tGa2Ni7Eg0GZTbPS+F8WsWiW4iahM89AxgQOknGy3EsLBXwVDcfxNym9JtweXdmZCp6ouKYxBMBtTbi1gmMn3lB0Rb6Fauqz4fNUQ22N5ZoBJqinB+Aq2365twE9OWqFpCO7v0p1uis+JAm3eRnxBhAFR85sdlCkVZg4ojS7NZqCBOZo+jBdD1RhINLIKib/yr62yDK5p+/lSVhY07YVQsmtvlQiS0BgSCOleT651BrWs4TcLm8MinwqbB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t60gIcpTTwfs1eNdH8F387++lHeOA==";
    	//String encStr = "tVIs1T89WnKMXFMUdO/BUA==";
    	//System.out.println(decrypt(encStr, "12345678901234567890123456789012", "1234567890123456"));
    	    	
    	
    	//String jsonstr = "{\"TENANT_DBID\": \"1\",\"EMPLOYEE_ID\": \"19395237\"}";
    	//String aaa = encrypt(jsonstr, "12345678901234567890123456789012", "1234567890123456");
    	//System.out.println(aaa);
    	
    }
    
    public static String encrypt(String text, String key, String iv) throws Exception {
        Cipher cipher = Cipher.getInstance(alg);
        SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivParamSpec = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivParamSpec);

        byte[] encrypted = cipher.doFinal(text.getBytes("UTF-8"));
        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static String decrypt(String cipherText, String key, String iv) throws Exception {
        Cipher cipher = Cipher.getInstance(alg);
        SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivParamSpec = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParamSpec);

        byte[] decodedBytes = Base64.getDecoder().decode(cipherText);
        byte[] decrypted = cipher.doFinal(decodedBytes);
        return new String(decrypted, "UTF-8");
    }

}