Aes加密 GCM java

Aes加密 GCM java

Aes加密 GCM模式在Java中的应用

AES(高级加密标准)是一种流行的对称加密算法。它广泛应用于各种安全通信场景。GCM(Galois/Counter Mode)是一种AES的工作模式,它不仅提供加密功能,还能确保数据完整性和认证。在这篇文章中,我们将探讨如何在Java中使用AES-GCM进行数据加密,并提供相关代码示例。

AES-GCM简介

AES-GCM模式结合了AES加密和GMAC(Galois Message Authentication Code)认证。它的优点包括:

安全性高:提供机密性和数据完整性。

性能优越:适合并行操作,最高效地利用现代CPU的能力。

易用性:Java提供了内建的支持,可以方便地进行操作。

AES-GCM的工作流程

在使用AES-GCM模式进行加密和解密时,主要的工作流程如下:

实现AES-GCM加密的代码示例

下面的代码示例演示了如何在Java中实现AES-GCM加密与解密。

代码示例
java 复制代码
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;

public class AesGcmExample {

    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES/GCM/NoPadding";
    private static final int AES_KEY_SIZE = 256;
    private static final int GCM_TAG_LENGTH = 16; // 128 bit
    private static final int GCM_IV_LENGTH = 12; // 96 bit

    // 生成AES密钥
    public static SecretKey generateKey() throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
        keyGen.init(AES_KEY_SIZE);
        return keyGen.generateKey();
    }

    // 加密明文
    public static String encrypt(String plaintext, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        byte[] iv = new byte[GCM_IV_LENGTH];
        SecureRandom random = new SecureRandom();
        random.nextBytes(iv);

        GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv);
        cipher.init(Cipher.ENCRYPT_MODE, key, spec);

        byte[] ciphertext = cipher.doFinal(plaintext.getBytes());
        byte[] encrypted = new byte[iv.length + ciphertext.length];
        System.arraycopy(iv, 0, encrypted, 0, iv.length);
        System.arraycopy(ciphertext, 0, encrypted, iv.length, ciphertext.length);

        return Base64.getEncoder().encodeToString(encrypted);
    }

    // 解密密文
    public static String decrypt(String encrypted, SecretKey key) throws Exception {
        byte[] decoded = Base64.getDecoder().decode(encrypted);
        byte[] iv = new byte[GCM_IV_LENGTH];
        System.arraycopy(decoded, 0, iv, 0, iv.length);
        
        GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv);
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, key, spec);

        byte[] ciphertext = new byte[decoded.length - GCM_IV_LENGTH];
        System.arraycopy(decoded, GCM_IV_LENGTH, ciphertext, 0, ciphertext.length);

        byte[] plaintext = cipher.doFinal(ciphertext);
        return new String(plaintext);
    }

    public static void main(String[] args) throws Exception {
        SecretKey key = generateKey();
        String originalText = "Hello, AES-GCM!";
        
        String encryptedText = encrypt(originalText, key);
        System.out.println("Encrypted: " + encryptedText);
        
        String decryptedText = decrypt(encryptedText, key);
        System.out.println("Decrypted: " + decryptedText);
    }
}

代码分析

生成密钥:通过KeyGenerator生成AES密钥。

IV生成:使用安全随机数生成器生成一个随机的初始化向量(IV)。

加密和解密:利用Cipher对象进行加密和解密操作。

数据完整性:GCM模式自动处理认证标签,确保解密时数据的完整性。

总结

通过上述示例,我们展示了在Java中如何实现AES-GCM加密和解密。AES-GCM结合了AES的强大和高效性,同时提供了数据完整性验证功能。在处理敏感数据时,使用AES-GCM将有效提升系统的安全性。希望这篇文章能够帮助您更好地理解AES-GCM的应用。

相关推荐
疯狂成瘾者2 分钟前
什么是多 Agent,多Agent是如何协作的?
java
witAI9 分钟前
**AI仿真人剧制作2025推荐,专业团队与创新技术引领未来**
人工智能·python
he___H9 分钟前
Spring中的设计模式
java·spring·设计模式
liuyao_xianhui16 分钟前
优选算法_最小基因变化_bfs_C++
java·开发语言·数据结构·c++·算法·哈希算法·宽度优先
做一个AK梦17 分钟前
计算机系统概论知识点(软件设计师)
java·开发语言
♪-Interpretation25 分钟前
第五节:Python的流程控制语句
python
東雪木35 分钟前
Java学习——一访问修饰符(public/protected/default/private)的权限控制本质
java·开发语言·学习·java面试
cch891839 分钟前
易语言与C++:编程语言终极对决
开发语言·c++
两点王爷1 小时前
docker 创建和使用存储卷相关内容
java·docker·容器
boonya1 小时前
Embedding模型与向量维度动态切换完整方案
java·数据库·embedding·动态切换大模型