文本使用AESJava语言接口说明

AES(Advanced Encryption Standard)是一种对称加密算法,广泛应用于保护数据的安全性。在Java语言中,我们可以通过AESJava语言接口来进行AES加密和解密操作。

AESJava语言接口使用说明

加密

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

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

解密

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

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

代码示例

public class Main {
    public static void main(String[] args) {
        String plainText = "Hello, World!";
        String key = "RandomSecretKey";

        try {
            String encryptedText = AESEncryption.encrypt(plainText, key);
            System.out.println("Encrypted Text: " + encryptedText);

            String decryptedText = AESDecryption.decrypt(encryptedText, key);
            System.out.println("Decrypted Text: " + decryptedText);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

结语

通过AESJava语言接口,我们可以轻松实现对数据的加密和解密操作,确保数据的安全性。在实际开发中,我们可以使用AES算法对敏感信息进行加密存储或传输,以防止数据泄露和非法访问。希望本文对您有所帮助,谢谢阅读!