package com.ruoyi.cai.util;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class AES {

    public static void main(String[] args) throws Exception {
        String jsonData ="zTmtrzE4t6jFriRIde3X+C3rgTo2LdYZSgiP8yg6cXY/0/05hSvBlfa97rZKGrDfVWWqcKdnivwnvoAzzUnxsr5YDkYPgoFHVSzcsBFXTCbPJfMkinjtY5nFsf4oXMATOe1WTHOpcAqfQ9bHcEB1oAVSt5SQvIFiw9uUPTS6IgS6waZtJT5/h9SJElisYboJ/47YavVUz8j4oDrUcwzB2KyJqJrjBlV0YSkT1/OAtlau1yJIuNX3THeN6BBA9dpmhn8f3YC/sqzsmGl7p2nbV4WxQxdI8bV855/fUn0/7lOAQM+s82hEal79J+3VQ+cexhWejit3uR0/l0T+p+u5WlICW/V0O65LGiZYNL2MGotVai1Tjog5G223VrWwf3VDOLlOuKK1HbH8MEXM6kahJ5EnxgIi4hKa0k63r7WOgJKLMxFzJsxRRy6lQfems3NOELwNCHzKaMa1ZgHTRzkOUvcPdYj1JuLIRZkMyPgKJbW10QJJbkeGuqr5YN9kl7zdA2omJFVL/0l9v0i1TC6WV7yME+2YU04OCSNWYDISPeDw4D9k1XurHbD1iQDFjPaDiBA/euhgKDUz4V7MeDqUsMUKq0G9mrXHwJx2Euf7yWoaKr0N4v5a9dsqf+yHQm/Iww1VWRGL+qW2ko265rpH+cDyhZia9r2ovvgbXgjW07GQGEXW7i4BbF27bdNRXz6Zl0eLAfgSHM4uLEVYLnahCM3qZc2d2mWpo3oQWp3Hs/CaeUAbFstBfZTCUB28PunGVA7XRMXqU/1fVn4owxf8Y0Y3TQ2AN7Ou8PO+8VH1uUEeyBy+NPT4EPTI19gzrwMZ11go7wNv7PnFiLNp/PRJB5r9TcmmgIYcjOQ0S1WdqFh8vXSKlbxPDtSPS2RrTjxLqJvp2ES3lm8ZpGt9jJmI3uiRIpy3RB6uSNPfEJgj0iDYI4Lv51iihHivP4mpp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H5AbsikBGsOFNEfJOeYw1WPe08N/w2nvxOubTXah3TpiogVpl3Lf1tntuF3tFu8n3z/YMSuKWe4bPLOTTcUN65L7z41iod+7EqzfRqMfL/4MwBR2ZxAkX/BLIasOXLpa/uFQMiAjU747VUnDKqty+ymLDI5y9F+LO4is+hiXRtqou07z+nqE+Vnlu6l3MXHn3k/K0VlmVk63S0Uu4WZv4MAE1DZxpXTA8oPRtQS7uBnWgS47f5NoGD2kxIRaGYM3EGNX/RtmQ4KvUZ0hCVs9Z/0YTq+iEUskTsw4vSRJrRYndHWbyXRZ7hs8s5NNxQ3rkvvPjWKhh7FhEF6Hqbbr/1vt4CQsUiRf8Eshqw5culr+4VAyICNTvjtVScMqq3L7KYsMjnL0X4s7iKz6GJdG2qi7TvP6eoT5WeW7qXcxcefeT8rRWWZWTrdLRS7hZm/gwATUNnGlTMYMdW/5icrsjddgU9zCsTcCfjYArCAQnjzEjkzOm6s9A6iET/P0LetlyupSe+zH";
//        String jsonData = "123";
        String key = "K2AwvosrwtoAgOEP";
        String encod = encrypt(jsonData, key);
        System.out.println(decrypt(jsonData,key));
    }

    public static String encrypt(String data, String key) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decrypt(String data,String key) throws Exception{
        byte[] decode = Base64.getDecoder().decode(data);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] encryptedBytes = cipher.doFinal(decode);
        return new String(encryptedBytes,StandardCharsets.UTF_8);

    }
}
