Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

java aes解密工具类

作者:_o隨缘   发布日期:2026-02-23   浏览:51

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

public class AESUtil {

    private static final String KEY_ALGORITHM = "AES";
    private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    /**
     * 生成密钥
     *
     * @return 密钥
     * @throws Exception 异常
     */
    public static String generateKey() throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance(KEY_ALGORITHM);
        keyGen.init(128); // 可以选择128, 192或256位密钥长度
        SecretKey secretKey = keyGen.generateKey();
        return Base64.getEncoder().encodeToString(secretKey.getEncoded());
    }

    /**
     * 解密
     *
     * @param content 待解密内容
     * @param key     密钥
     * @return 解密后的内容
     * @throws Exception 异常
     */
    public static String decrypt(String content, String key) throws Exception {
        byte[] encryptedBytes = Base64.getDecoder().decode(content);
        byte[] raw = Base64.getDecoder().decode(key);
        SecretKeySpec secretKeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes);
    }

    /**
     * 加密
     *
     * @param content 待加密内容
     * @param key     密钥
     * @return 加密后的内容
     * @throws Exception 异常
     */
    public static String encrypt(String content, String key) throws Exception {
        byte[] raw = Base64.getDecoder().decode(key);
        SecretKeySpec secretKeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] encryptedBytes = cipher.doFinal(content.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static void main(String[] args) {
        try {
            // 生成密钥
            String key = generateKey();
            System.out.println("Generated Key: " + key);

            // 测试加密和解密
            String originalContent = "Hello, World!";
            String encryptedContent = encrypt(originalContent, key);
            System.out.println("Encrypted Content: " + encryptedContent);

            String decryptedContent = decrypt(encryptedContent, key);
            System.out.println("Decrypted Content: " + decryptedContent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

代码解释说明:

  1. 生成密钥

    • 使用 KeyGenerator 类生成一个 AES 密钥。
    • 将生成的密钥编码为 Base64 字符串以便存储和传输。
  2. 加密

    • 使用 Cipher 类进行加密操作。
    • 首先将 Base64 编码的密钥解码为字节数组,然后创建 SecretKeySpec 对象。
    • 初始化 Cipher 对象为加密模式,并使用该对象对输入字符串进行加密。
    • 最后将加密后的字节数组编码为 Base64 字符串返回。
  3. 解密

    • 同样使用 Cipher 类进行解密操作。
    • 首先将 Base64 编码的密钥解码为字节数组,然后创建 SecretKeySpec 对象。
    • 初始化 Cipher 对象为解密模式,并使用该对象对输入的 Base64 编码字符串进行解密。
    • 最后将解密后的字节数组转换为字符串返回。
  4. 测试

    • main 方法中生成密钥,并测试加密和解密功能。

上一篇:java arrays.sort

下一篇:java list对象排序

大家都在看

java url decode

java判断是windows还是linux

java连接数据库的代码

java date类型比较大小

java djl

ubuntu 卸载java

es java api

java list 查找

java 解压rar

java读取excel中的图片

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站