本文主要是介绍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的编码,解码成字符串:哈喽啊
这篇关于gzip -- 加密的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!