当前位置: 代码迷 >> 综合 >> Kryo 进阶学习:数据的压缩与加密
  详细解决方案

Kryo 进阶学习:数据的压缩与加密

热度:56   发布时间:2023-12-15 20:48:48.0

      压缩用Kryo提供的DeflateSerializer类,然后写入到CipherOutputStream。

Kryo kryo=new Kryo();kryo.setReferences(false);kryo.setRegistrationRequired(true);String w_str1="繁體中文,English,12345,12345,123451234512345,12345,12345,12345,12345,12345,12345,12345";		//GZipEncryptor gz=null;try {gz = new GZipEncryptor();			} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();}kryo.register(String.class, new DeflateSerializer(new StringSerializer()));ByteArrayOutputStream bos=new ByteArrayOutputStream();CipherOutputStream cos=gz.newCipherOutputStream(bos);Output output =  new Output(cos,1);kryo.writeClassAndObject(output, w_str1);try {output.flush();cos.close();bos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}				byte[] out =bos.toByteArray();


out为压缩、加密后的数据。


逆向还原:

<span style="white-space:pre">		</span>Kryo kryo1=new Kryo();kryo1.setRegistrationRequired(true);kryo1.setReferences(false);kryo1.register(String.class, new DeflateSerializer(new StringSerializer()));CipherInputStream cis=gz.newCipherInputStream(new ByteArrayInputStream(out));Input input=new Input(cis);		String w_str2=(String)kryo1.readClassAndObject(input);System.out.println(w_str2);