package com.ozs.stateSecrets.utils;

import org.apache.commons.codec.binary.Base64;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class SM4Utils
{
	public String iv = "";

	private boolean hexString = false;
	public static Pattern p = Pattern.compile("\\s*|\t|\r|\n");
	public SM4Utils()
	{
	}

	public static String encryptData_ECB(String plainText, String secretKey)
	{
		try
		{
			SM4Context ctx = new SM4Context();
			ctx.isPadding = true;
			ctx.mode = SM4.SM4_ENCRYPT;
			byte[] keyBytes;
			keyBytes = secretKey.getBytes(StandardCharsets.UTF_8);
			SM4 sm4 = new SM4();
			sm4.sm4_setkey_enc(ctx, keyBytes);
			byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainText.getBytes(StandardCharsets.UTF_8));
			String cipherText = Base64.encodeBase64String(encrypted);
			if (cipherText != null && cipherText.trim().length() > 0)
			{
				Matcher m = p.matcher(cipherText);
				cipherText = m.replaceAll("");
			}
			return cipherText;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public static  String decryptData_ECB(String cipherText, String secretKey)
	{
		try
		{
			SM4Context ctx = new SM4Context();
			ctx.isPadding = true;
			ctx.mode = SM4.SM4_DECRYPT;
			byte[] keyBytes;
			keyBytes = secretKey.getBytes(StandardCharsets.UTF_8);
			SM4 sm4 = new SM4();
			sm4.sm4_setkey_dec(ctx, keyBytes);
			byte[] decrypted = sm4.sm4_crypt_ecb(ctx, Base64.decodeBase64(cipherText));
			return new String(decrypted, StandardCharsets.UTF_8);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public String encryptData_CBC(String plainText, String secretKey)
	{
		try
		{
			SM4Context ctx = new SM4Context();
			ctx.isPadding = true;
			ctx.mode = SM4.SM4_ENCRYPT;

			byte[] keyBytes;
			byte[] ivBytes;
			if (hexString)
			{
				keyBytes = Util.hexStringToBytes(secretKey);
				ivBytes = Util.hexStringToBytes(iv);
			}
			else
			{
				keyBytes = secretKey.getBytes(StandardCharsets.UTF_8);
				ivBytes = iv.getBytes(StandardCharsets.UTF_8);
			}

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_enc(ctx, keyBytes);
			byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, plainText.getBytes(StandardCharsets.UTF_8));
			String cipherText = Base64.encodeBase64String(encrypted);
			if (cipherText != null && cipherText.trim().length() > 0)
			{
				Matcher m = p.matcher(cipherText);
				cipherText = m.replaceAll("");
			}
			return cipherText;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public String decryptData_CBC(String cipherText, String secretKey)
	{
		try
		{
			SM4Context ctx = new SM4Context();
			ctx.isPadding = true;
			ctx.mode = SM4.SM4_DECRYPT;

			byte[] keyBytes;
			byte[] ivBytes;
			if (hexString)
			{
				keyBytes = Util.hexStringToBytes(secretKey);
				ivBytes = Util.hexStringToBytes(iv);
			}
			else
			{
				keyBytes = secretKey.getBytes(StandardCharsets.UTF_8);
				ivBytes = iv.getBytes(StandardCharsets.UTF_8);
			}

			SM4 sm4 = new SM4();
			sm4.sm4_setkey_dec(ctx, keyBytes);
			byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, Base64.decodeBase64(cipherText));
			return new String(decrypted, StandardCharsets.UTF_8);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) throws IOException
	{
		String plainText = "abcd";

		SM4Utils sm4 = new SM4Utils();
		sm4.hexString = false;

		System.out.println("ECBģʽ");
		String cipherText = sm4.encryptData_ECB(plainText,"4370780c9a8c43e5");
		System.out.println("加密后: " + cipherText);
		System.out.println("");

		plainText = sm4.decryptData_ECB(cipherText,"4370780c9a8c43e5");
		System.out.println("解密后: " + plainText);
		System.out.println("");

		System.out.println("大");
		sm4.iv = "UISwD9fW6cFh9SNS";
		cipherText = sm4.encryptData_CBC(plainText,"4370780c9a8c43e5");
		System.out.println("加密后: " + cipherText);
		System.out.println("");

		plainText = sm4.decryptData_CBC(cipherText,"4370780c9a8c43e5");
		System.out.println("解密后: " + plainText);
	}

}
