Java 基础教程

Java 面向对象

Java 高级教程

Java 笔记

Java FAQ

java aes加密解密


Java 中进行 AES 加密和解密通常需要使用 javax.crypto 包中的类库。我将为你提供两种常见的实现方式,一种是使用标准 Java 库,另一种是使用 Bouncy Castle 库来实现 AES 加密和解密。

使用标准 Java 库

步骤流程:

导入必要的包:

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

生成 AES 密钥:

byte[] aesKey = "MySecretKey12345".getBytes(); // 替换为你自己的密钥
SecretKey secretKey = new SecretKeySpec(aesKey, "AES");

创建 AES 加密和解密 Cipher 对象:

Cipher encryptCipher = Cipher.getInstance("AES");
Cipher decryptCipher = Cipher.getInstance("AES");
encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);

加密数据:

byte[] plaintext = "Hello, AES Encryption!".getBytes();
byte[] ciphertext = encryptCipher.doFinal(plaintext);
String encryptedText = Base64.getEncoder().encodeToString(ciphertext);

解密数据:

byte[] decryptedBytes = decryptCipher.doFinal(Base64.getDecoder().decode(encryptedText));
String decryptedText = new String(decryptedBytes);

Maven 依赖:

<dependency>
    <groupId>javax.crypto</groupId>
    <artifactId>javax.crypto-api</artifactId>
    <version>1.1.1</version>
</dependency>

Gradle 依赖:

implementation 'javax.crypto:javax.crypto-api:1.1.1'

使用 Bouncy Castle 库

Bouncy Castle 是一个流行的 Java 加密库,支持各种加密算法,包括 AES。

步骤流程:

导入 Bouncy Castle 库的依赖。请确保在项目中引入 Bouncy Castle 库的 JAR 文件或 Maven/Gradle 依赖。

Maven 依赖:

<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.68</version> <!-- 版本号可能有所变化 -->
</dependency>

Gradle 依赖:

implementation 'org.bouncycastle:bcprov-jdk15on:1.68' // 版本号可能有所变化

使用 Bouncy Castle 库进行 AES 加密和解密。以下是示例代码:

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;
import java.util.Base64;

public class AesEncryptionWithBouncyCastle {
    public static void main(String[] args) throws Exception {
        Security.addProvider(new BouncyCastleProvider());

        byte[] aesKey = "MySecretKey12345".getBytes();
        SecretKey secretKey = new SecretKeySpec(aesKey, "AES");

        Cipher encryptCipher = Cipher.getInstance("AES");
        Cipher decryptCipher = Cipher.getInstance("AES");
        encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
        decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);

        byte[] plaintext = "Hello, AES Encryption with Bouncy Castle!".getBytes();
        byte[] ciphertext = encryptCipher.doFinal(plaintext);
        String encryptedText = Base64.getEncoder().encodeToString(ciphertext);

        byte[] decryptedBytes = decryptCipher.doFinal(Base64.getDecoder().decode(encryptedText));
        String decryptedText = new String(decryptedBytes);

        System.out.println("Original Text: " + new String(plaintext));
        System.out.println("Encrypted Text: " + encryptedText);
        System.out.println("Decrypted Text: " + decryptedText);
    }
}

以上两种方式都可以用于实现 AES 加密和解密。你可以根据项目的需要选择其中一种方式来使用。不过,请注意确保在生产环境中安全地管理密钥和加密的数据。