ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

gzip -- 加密

2022-05-01 02:32:18  阅读:255  来源: 互联网

标签:加密 java -- new gzip import data out


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有