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

相关推荐
草履虫建模11 小时前
力扣算法 1768. 交替合并字符串
java·开发语言·算法·leetcode·职场和发展·idea·基础
naruto_lnq13 小时前
分布式系统安全通信
开发语言·c++·算法
qq_2975746713 小时前
【实战教程】SpringBoot 实现多文件批量下载并打包为 ZIP 压缩包
java·spring boot·后端
老毛肚13 小时前
MyBatis插件原理及Spring集成
java·spring·mybatis
学嵌入式的小杨同学13 小时前
【Linux 封神之路】信号编程全解析:从信号基础到 MP3 播放器实战(含核心 API 与避坑指南)
java·linux·c语言·开发语言·vscode·vim·ux
lang2015092814 小时前
JSR-340 :高性能Web开发新标准
java·前端·servlet
Re.不晚14 小时前
Java入门17——异常
java·开发语言
缘空如是14 小时前
基础工具包之JSON 工厂类
java·json·json切换
精彩极了吧14 小时前
C语言基本语法-自定义类型:结构体&联合体&枚举
c语言·开发语言·枚举·结构体·内存对齐·位段·联合
追逐梦想的张小年14 小时前
JUC编程04
java·idea