如何在Flutter中使用gzip编码的数据解密json响应? [英] How to decrypt a json response with gzip encoded data in flutter?

查看:108
本文介绍了如何在Flutter中使用gzip编码的数据解密json响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Json响应具有已加密的对象,必须对其进行解密才能获得实际的进程.在android中使用了GZip.如何实现此示例Json如下所述.对我们的帮助非常感谢.

The Json response has encrypted objects which has to be decrypted to get the actual process. In android GZip was used .How can I achieve this The sample Json is as mentioned below.Any help is really appreciated.


            {
                "Data": "1.´ABCD´1150275,11028´01-Jan-2021´8,000.00´",
                "Data": [
                    {
                      "Element": "8iMAAB+LCAAAAAAABADt1T8zBxwHgkefKcGh98Zcdz8FSqj9DMzK4d+L0Nj1tveNR2w6M8rRs3PJWBFDy"
                    },
                    {
                     "Element": "B1AV4bGp6JzQJI8ChnxzixrlT8vKnYHPwRM8zykKVn2gkceAFdxMwU0to"
                    }
                ],

    "Status": 1,
    "Msg": "Success",
    "APIVersion": "1.4"
}

基本上如何解密Gzip字符串.在android中完成了相同的过程,但我还是扑朔迷离附加了Android Java代码.我想在扑扑中实现类似的目标

Basically how to decrypt a Gzip string. The same process was done in android ,but im new to flutter Android java code is attached. i want to achieve something like that in flutter

    public static String decompress(String zipText) throws IOException {
        byte[] compressed = Base64.decode(zipText, Base64.DEFAULT);
        if (compressed.length > 4) {
            GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(compressed, 4,compressed.length - 4));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            for (int value = 0; value != -1; ) {
                value = gzipInputStream.read();
                if (value != -1) {
                    baos.write(value);
                }
            }
            gzipInputStream.close();
            baos.close();
            return new String(baos.toByteArray(), StandardCharsets.UTF_8);
        } else {
            return "";
        }
    }

我尝试过的方式

  List<int> data  = utf8.encode(zipText);
  var deCompressedString = GZipDecoder().decodeBytes(data);
  print(deCompressedString);

哪个抛出异常

Unhandled Exception: FormatException: Invalid GZip Signature

推荐答案

用于解密压缩文件: 编辑

像这样解压缩

 String decompress(String zipText) {
  final List<int> compressed = base64Decode(zipText);
  if (compressed.length > 4) {
   Uint8List uint8list = GZipDecoder().decodeBytes(compressed.sublist(4, compressed.length - 4));
   // print( String.fromCharCodes(uint8list));
   return String.fromCharCodes(uint8list);
  } else {
   return "";
  }
 }

这篇关于如何在Flutter中使用gzip编码的数据解密json响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆