其他分享
首页 > 其他分享> > AEAD_AES_256_GCM 解密 加密文件

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);
        }
    }

}

标签:AES,String,base64,GCM,tag,static,new,AEAD,byte
来源: https://blog.csdn.net/u014732534/article/details/121117940