Java的压缩、解压及压缩加密、解密解压 例子
来源:互联网 发布:java restful api开发 编辑:程序博客网 时间:2024/06/10 23:45
为了节约带宽、加快传送速度,http协议支持gzip的压缩,但如果我们的app与后台不是通过http协议通讯的,那么压缩、解压这个流程需要自己写。下面给出compress和decompress的代码:
public static byte[] compress(byte[] data) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 压缩 GZIPOutputStream gos = new GZIPOutputStream(baos); gos.write(data, 0, data.length); gos.finish(); baos.flush(); baos.close(); return <span style="font-family: Arial, Helvetica, sans-serif;">baos.toByteArray()</span><span style="font-family: Arial, Helvetica, sans-serif;">; </span> }
public static byte[] decompress(byte[] data) throws Exception { GZIPInputStream bis = new GZIPInputStream(new ByteInputStream(data, data.length)); ByteArrayOutputStream bos=new ByteArrayOutputStream(); byte[] buf=new byte[20480]; int len=0; while ((len=bis.read(buf))>0){ bos.write(buf, 0, len); } bis.close(); bos.close(); return <span style="font-family: Arial, Helvetica, sans-serif;">bos.toByteArray()</span><span style="font-family: Arial, Helvetica, sans-serif;">; </span> }
尽快压缩后的数据不可视,但有心人很容易通过拦截数据包很快猜想到这是gzip压缩格式并给出解压程式,对于游戏领域、金融领域的应用,通讯过程的加密尤为重要。
Blowfish算法免费、速度快,不宜破解(关键是key数据不要泄露),在及时加密、解密中应用广泛。下面以Blowfish算法为例简单讲下数据的压缩、加密盒解密、解压过程。
1、定义keySpec,用来储存key数据的object:
static private SecretKeySpec keySpec;
byte[] key=KeyGenerator.getInstance("Blowfish").generateKey().getEncoded();
keySpec = new SecretKeySpec(key, "Blowfish");
2、再定义getCipher方法,根据mode获得加密/解密的Cipher Object:
static private Cipher getCipher (int mode) {try {Cipher cipher = Cipher.getInstance("Blowfish");cipher.init(mode, keySpec);return cipher;} catch (Exception ex) {throw new KryoException(ex);}}
3、下面是对w_str的压缩、加密和解密、解压:
public static void main(String[] args) throws IOException, NoSuchAlgorithmException{ keySpec = new SecretKeySpec(KeyGenerator.getInstance("Blowfish").generateKey().getEncoded(), "Blowfish"); String w_src="这是整數數組[1, -105, 104, 101, 108, 108, 111, 119, 111, 114, 108, 100, -17, -68, -116, -28, -72, -83, -26, -106, -121, -17, -68, -116, -25, -71, -127, -23, -85, -108, -17, -68, -116, -25, -80, -95, -23, -85, -108, -17, -68, -116, -25, -82, -128, -28, -67, -109]";Cipher cipher = getCipher(Cipher.ENCRYPT_MODE); ByteArrayOutputStream bos=new ByteArrayOutputStream(); GZIPOutputStream gos=new GZIPOutputStream(new CipherOutputStream(bos, cipher));gos.write(w_src.getBytes());gos.close();bos.close();byte[] data=bos.toByteArray();//压缩加密后data只有135byte长度,还是非常可观的。//decrypt & decompresscipher = getCipher(Cipher.DECRYPT_MODE);ByteArrayInputStream bis=new ByteArrayInputStream(data);GZIPInputStream input = new GZIPInputStream(new CipherInputStream(bis,cipher));ByteArrayOutputStream baos=new ByteArrayOutputStream();byte[] w_buffer=new byte[2048];int len=0; while ((len=input.read(w_buffer))>0){ baos.write(w_buffer, 0, len); } bis.close(); input.close(); baos.close(); byte[] w_ret=baos.toByteArray(); String w_out_str=new String(w_ret); System.out.println(w_out_str); }
当然,对于极度重要的数据,为了安全起见,权衡加解密速度、破解难度等方面,个人建议还是用AES不对称加密。
转载请注明出处:http://blog.csdn.net/rocklee
0 0
- Java的压缩、解压及压缩加密、解密解压 例子
- 压缩 加密 解压 解密
- tar压缩/解压、加密压缩/解密解压
- MD5加密解密 zip格式压缩解压
- Linux之tar 压缩解压 加密解密
- 使用Java对文件或文件夹的压缩, 解压, 加密和解密
- JAVA压缩及解压文件
- tar加密压缩解压
- JAVA 解密中解压.tb压缩包
- libjpeg移植及压缩解压bmp的例子
- 文件的加密压缩与解压(java)
- 压缩及解压
- Javascript在线美化、优化、加密、解密、压缩、解压
- 对文件或文件夹进行压缩解压加密解密
- GZip 压缩解压 --- Pako 的压缩解压
- java 压缩与解压
- java 压缩、解压
- JAVA压缩/解压ZIP
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- 关于jquery重复提交请求的原因
- IOS中定时器NSTimer的开启与关闭
- 字符编码的奥秘
- Tracking Area Update Procedure
- Java的压缩、解压及压缩加密、解密解压 例子
- 字符串全排列(字母序)
- 将windows server2008上的web应用部署到Linux环境下
- 程序员都是有强迫症的
- Android数据存储
- Hi3515移动侦测技术的设计与实现
- Android中同一个ImageView中根据状态显示不同图片
- Oracle宝典--Oracle关键字
- Dandelion - Eclipse Lisp Plugin