欢迎访问宙启技术站
智能推送

利用Java函数实现AES加密算法的示例代码和使用方法

发布时间:2023-06-14 12:52:23

AES(Advanced Encryption Standard)是一种对称加密算法,它是目前最常用的加密算法之一。在Java中,可以使用javax.crypto库中提供的类来实现AES加密算法。下面就介绍一下如何使用Java函数实现AES加密算法。

1. 生成密钥

在使用AES加密算法之前,需要生成密钥。可以使用KeyGenerator类来生成AES密钥。示例代码如下:

KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128); // 密钥长度为128位
SecretKey secretKey = keyGen.generateKey();

2. 加密数据

使用Cipher类可以实现AES的加密和解密。在加密数据时,需要指定加密模式和填充方式。常用的加密模式有ECB、CBC、CTR等,常用的填充方式有PKCS5Padding、ISO10126Padding等。下面是一个使用AES加密算法的示例代码:

public static byte[] encryptData(String data, SecretKey secretKey)
        throws NoSuchPaddingException, NoSuchAlgorithmException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
    byte[] encryptedData = cipher.doFinal(dataBytes);
    return encryptedData;
}

在这个示例代码中,我们使用了ECB模式和PKCS5Padding填充方式。加密后的数据以字节数组的形式返回。

3. 解密数据

解密数据与加密数据类似,需要指定解密模式和填充方式。下面是一个解密数据的示例代码:

public static String decryptData(byte[] data, SecretKey secretKey)
        throws NoSuchPaddingException, NoSuchAlgorithmException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] decryptedData = cipher.doFinal(data);
    return new String(decryptedData, StandardCharsets.UTF_8);
}

4. 示例代码

综合以上2、3步骤,以下是一个完整的示例代码:

import javax.crypto.*;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class AESUtil {

    public static void main(String[] args) throws NoSuchPaddingException, NoSuchAlgorithmException,
                                    InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        String originalString = "Hello world!";
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(128); // 密钥长度为128位
        SecretKey secretKey = keyGen.generateKey();
        byte[] encryptedBytes = AESUtil.encryptData(originalString, secretKey);
        String decryptedString = AESUtil.decryptData(encryptedBytes, secretKey);
        System.out.println("Original string: " + originalString);
        System.out.println("Encrypted bytes: " + new String(encryptedBytes, StandardCharsets.UTF_8));
        System.out.println("Decrypted string: " + decryptedString);
    }

    public static byte[] encryptData(String data, SecretKey secretKey)
            throws NoSuchPaddingException, NoSuchAlgorithmException,
            InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
        byte[] encryptedData = cipher.doFinal(dataBytes);
        return encryptedData;
    }

    public static String decryptData(byte[] data, SecretKey secretKey)
            throws NoSuchPaddingException, NoSuchAlgorithmException,
            InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedData = cipher.doFinal(data);
        return new String(decryptedData, StandardCharsets.UTF_8);
    }
}

在这个示例代码中,我们使用了128位的密钥。加密后的数据以字节数组的形式输出,解密后的数据以字符串的形式输出。

总之,使用Java函数实现AES加密算法非常简单,只要掌握好生成密钥、选择合适的加密模式和填充方式,并使用Cipher类实现加密和解密操作。