AEAD_AES_256_GCM 解密 加密文件

 微信发的加密文件参数,在网上找了的都不能用,总是提示 tag mismatch 自己摸索着整理了下来

 测试类

public class DecryptDemo {

    private static String key_base64 = "2Wrn4xJ9Fdd+8hiQ15+KEzLDV5qz0d7fVDsOjtxZ4v4=";
    private static String iv_base64 = "mCb5U+uGOnYaIALZ";
    private static String tag_base64 = "PSIbHJTVTCcstYf+ggCD+g==";

    public static void main(String[] args) throws Exception {
        String filepath = "D:\\workspace\\demo\\src\\test\\resources\\demo\\demo.pdf";
        String downloadFilePath = "D:\\workspace\\demo\\src\\test\\resources\\demo\\stodownload";
        DownloadFileUtil.downloadFile("https://mmae.qpic.cn/204/20303/stodownload?filekey=30350201010421301f020200cc0402534804106acbf4ddacea8daa158e1637d1041bc20203019269040d00000004627466730000000131&hy=SH&storeid=32303231313031343134343035393030306430316463303137343736323838336536346630393030303030306363&bizid=1023",downloadFilePath);
        File file = new File(downloadFilePath);
        byte[] content = FileUtils.readFileToByteArray(file);
        byte[] bytes = AesUtil.decryptToByte(content,Base64.decodeBase64(key_base64),Base64.decodeBase64(iv_base64),Base64.decodeBase64(tag_base64));
        FileUtils.writeByteArrayToFile(new File(filepath),bytes);
    }


}

解密工具类

public class AesUtil {


    public static byte[] decryptToByte(byte[] content,byte[] aesKey,byte[] iv, byte[] tag)
            throws GeneralSecurityException {
        try {
            Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
            SecretKeySpec key = new SecretKeySpec(aesKey, "AES");
            GCMParameterSpec spec = new GCMParameterSpec(tag.length * Byte.SIZE, iv);
            cipher.init(Cipher.DECRYPT_MODE, key, spec);
            cipher.update(content);
            return cipher.doFinal(tag);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            throw new IllegalStateException(e);
        } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
            throw new IllegalArgumentException(e);
        }
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值