AES/GCM/NoPadding 加密(java)

AES/GCM/NoPadding 加密(java)

之前使用AES加密,sonar扫描规定使用AES/GCM/NoPadding方式加密,踩了一些坑,以下是代码。

key值:

	public static final String AES_KEY = "sOWGI8LvFnvyH19rs2DytdIIrUOL6ott";
	public  final static int GCM_IV_LENGTH = 12;
	public  final static int GCM_TAG_LENGTH = 16;

加密:

      public static String encrypt(String privateString, String key)  {
        key = garbleSalt(key);
        try {
            byte[] raw = key.getBytes();
            byte[] iv = new byte[GCM_IV_LENGTH];
            (new SecureRandom()).nextBytes(iv);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_AES);
            Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");

        GCMParameterSpec ivSpec = new GCMParameterSpec(GCM_TAG_LENGTH * Byte.SIZE, iv);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);

        byte[] ciphertext = cipher.doFinal(privateString.getBytes("UTF8"));
        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);

        String encoded = Base64.getEncoder().encodeToString(encrypted);

        return encoded;
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }

}

获得256位key

private static String  garbleSalt(String src) {
        if (StringUtils.isEmpty(src)) {
            return AES_KEY;
        } else {
            src += AES_KEY;
            System.out.println(src);
            return src.substring(0, 32);
        }
    }

解密:

    public static String decrypt(String encrypted,String key) {
        key = garbleSalt(key);
        System.out.println("key=" + key + "keylength=" + key.length() );
        try {
            byte[] raw = key.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_AES);
            byte[] decoded = Base64.getMimeDecoder().decode(encrypted);

            byte[] iv = Arrays.copyOfRange(decoded, 0, GCM_IV_LENGTH);

            Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
            GCMParameterSpec ivSpec = new GCMParameterSpec(GCM_TAG_LENGTH * Byte.SIZE, iv);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
            System.out.println("decoded.length" + decoded.length);
            System.out.println("GCM_IV_LENGTH" + (decoded.length - GCM_IV_LENGTH));
            System.out.println("decoded" + decoded);
            byte[] ciphertext = cipher.doFinal(decoded, GCM_IV_LENGTH, decoded.length - GCM_IV_LENGTH);

            String result = new String(ciphertext, "UTF8");

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值