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的应用。

相关推荐
weixin_462446232 小时前
使用 Go 实现 SSE 流式推送 + 打字机效果(模拟 Coze Chat)
开发语言·后端·golang
JIngJaneIL2 小时前
基于springboot + vue古城景区管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
敲敲了个代码2 小时前
隐式类型转换:哈基米 == 猫 ? true :false
开发语言·前端·javascript·学习·面试·web
专注VB编程开发20年2 小时前
C#全面超越JAVA,主要还是跨平台用的人少
java·c#·.net·跨平台
小信啊啊2 小时前
Go语言切片slice
开发语言·后端·golang
阿华hhh2 小时前
Linux系统编程(标准io)
linux·开发语言·c++
南_山无梅落3 小时前
9.Python3集合(set)增删改查和推导式
java·开发语言
sg_knight3 小时前
拥抱未来:ECMAScript Modules (ESM) 深度解析
开发语言·前端·javascript·vue·ecmascript·web·esm