import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64;

public class AES {

private static final String ALGORITHM = "AES/ECB/PKCS5Padding";
private static final String CHARSET = "UTF-8";

public static String encrypt(String data, String key) throws Exception {
    byte[] keyBytes = key.getBytes(CHARSET);
    SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, keySpec);
    byte[] encryptedBytes = cipher.doFinal(data.getBytes(CHARSET));
    return Base64.encodeBase64String(encryptedBytes);
}

public static String decrypt(String data, String key) throws Exception {
    byte[] keyBytes = key.getBytes(CHARSET);
    SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, keySpec);
    byte[] decodedBytes = Base64.decodeBase64(data);
    byte[] decryptedBytes = cipher.doFinal(decodedBytes);
    return new String(decryptedBytes, CHARSET);
}

}

String key = "learnspaceaes123"; String data = "your_data_here"; byte[] paddedData = pad(data.replace(" ", "").getBytes(CHARSET), 16, "pkcs7"); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(CHARSET), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); byte[] encryptedBytes = cipher.doFinal(paddedData); String encryptedData = Base64.encodeBase64String(encryptedBytes);

key = blearnspaceaes123 aes = AESnewkey AESMODE_ECB res = aesencryptpadjsondumpsget_paramsdatareplace encodeutf-8 AESblock_size style=pkcs7转换为java

原文地址: https://www.cveoy.top/t/topic/bAKH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录