其他分享
首页 > 其他分享> > gzip -- 加密

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的编码,解码成字符串:哈喽啊

标签:加密,java,--,new,gzip,import,data,out
来源: https://www.cnblogs.com/zhiqianggege/p/16212055.html