gzip -- 加密

java的gzip加密:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;


public class Hello {
    public static void main(String[] args) {
      try {
          String data = "哈喽啊";
          // gzip压缩
          ByteArrayOutputStream v0_1 = new ByteArrayOutputStream();
          GZIPOutputStream v1 = new GZIPOutputStream(v0_1);
          v1.write(data.getBytes());
          v1.close();
          byte[] arg6 = v0_1.toByteArray();
          System.out.println(Arrays.toString(arg6)); // 打印结果:[31, -117, 8, 0, 0, 0, 0, 0, 0, 0, 123, 58, -71, -29, -23, -76, -67, 79, -89, 118, 1, 0, 97, 15, -5, -43, 9, 0, 0, 0]

          // gzip解压缩
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          ByteArrayInputStream in = new ByteArrayInputStream(arg6);
          GZIPInputStream ungzip = new GZIPInputStream(in);
          byte[] buffer = new byte[256];
          int n;
          while ((n = ungzip.read(buffer)) >= 0) {
              out.write(buffer,0,n);
          }
          byte[] res = out.toByteArray();
          System.out.println(Arrays.toString(res));//打印结果:[-27, -109, -120, -27, -106, -67, -27, -107, -118]
          System.out.println(out.toString("utf-8"));//打印结果:哈喽啊
      }catch (Exception e){
          System.out.println(e);
      }
    }
    }

python的gzip加密:

import gzip

#压缩
data_in = "哈喽啊".encode('utf-8')
data_out = gzip.compress(data_in)
print(data_out)#打印结果:b'\x1f\x8b\x08\x00\x98wmb\x02\xff{:\xb9\xe3\xe9\xb4\xbdO\xa7v\x01\x00a\x0f\xfb\xd5\t\x00\x00\x00'
print(data_out.hex()) #打印16进制的加密数据:1f8b0800b9776d6202ff7b3ab9e3e9b4bd4fa7760100610ffbd509000000

#解压缩
res = gzip.decompress(data_out)
print(res) #打印解压缩后的utf-8编码:b'\xe5\x93\x88\xe5\x96\xbd\xe5\x95\x8a'
print(res.decode('utf-8')) # 将utf-8的编码,解码成字符串:哈喽啊
posted @   志强爱璇璇  阅读(615)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示