import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESUtils {
public static String DEFAULT_KEY="minglie@network1";
public static String encrypt(String data, String key) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedData, String key) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
return new String(decryptedBytes);
}
public static String encrypt(String data) throws Exception {
return AESUtils.encrypt(data,DEFAULT_KEY);
}
public static String decrypt(String encryptedData) throws Exception{
return AESUtils.decrypt(encryptedData,DEFAULT_KEY);
}
public static void main(String[] args) throws Exception {
String data = "Hello, Worlddddddddd!";
String encryptedData = encrypt(data);
System.out.println("Encrypted Data: " + encryptedData);
String decryptedData = decrypt(encryptedData);
System.out.println("Decrypted Data: " + decryptedData);
}
}