GCM的Counter模式加密与解密java实现

GCM即Galois/Counter Mode,指的是加密采用Counter模式,并且带有GMAC消息认证码
import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Base64;

/**
 * Description: GCM即Galois/Counter Mode,指的是加密采用Counter模式,并且带有GMAC消息认证码
 *
 * @author whip
 * @version 1.0
 * @date 2023/11/28 10:06
 */
@Slf4j
public class AesGcmUtil {

    /**
     * 加密
     *
     * @param plaintext
     * @param secretString
     * @return
     * @throws Exception
     */
    public String encrypt(String plaintext, String secretString) throws Exception {
        byte[] IV = new byte[12];
        SecureRandom random = new SecureRandom();
        random.nextBytes(IV);

        byte[] secretKey = Base64.getDecoder().decode(secretString);
        SecretKeySpec keySpec = new SecretKeySpec(secretKey, "AES");
        GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128, IV);

        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec);

        byte[] cipherText = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
        ByteBuffer byteBuffer = ByteBuffer.allocate(IV.length + cipherText.length);
        byteBuffer.put(IV);
        byteBuffer.put(cipherText);
        return Base64.getEncoder().encodeToString(byteBuffer.array());
    }

    /**
     * 解密
     *
     * @param ciphertext
     * @param secretString
     * @return
     * @throws Exception
     */
    public String decrypt(String ciphertext, String secretString) throws Exception {
        byte[] encrypted = Base64.getDecoder().decode(ciphertext);
        byte[] IV = Arrays.copyOfRange(encrypted, 0, 12);
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");

        GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128, IV);
        byte[] secretKey = Base64.getDecoder().decode(secretString);
        SecretKeySpec keySpec = new SecretKeySpec(secretKey, "AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec);

        byte[] plaintext = cipher.doFinal(Arrays.copyOfRange(encrypted, 12, encrypted.length));
        return new String(plaintext);
    }

    /**
     * 产生密钥工具方法
     *
     * @throws Exception
     */
    public String generateSecret() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(256);

        SecretKey key = keyGenerator.generateKey();
        byte[] bytes = key.getEncoded();
        log.info("生成密钥:{}", Base64.getEncoder().encodeToString(bytes));
        return Base64.getEncoder().encodeToString(bytes);
    }


    public static void main(String[] args) throws Exception {

        AesGcmUtil util = new AesGcmUtil();
        // 产生密钥
        String secret = util.generateSecret();

        // 加密
        String ciphertext = util.encrypt("XXX-测试", secret);
        log.info("加密结果:{}", ciphertext);
        // 解密
        String decrypt = util.decrypt(ciphertext, secret);
        log.info("解密结果: {}", decrypt);
    }


}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值