java字符串压缩解压示例
我测试的字符串是jquery源码。
明文长度:78082
压缩后:26566
加密长度:54746
再压缩:41647
-----------------------------
密文长度:41647
解压缩:54746
解密后:26566
再解压:78082
-----------------------------
比对成功
des需要jar:sun.misc.base64decoder.jar
test
public static void main(string[] args) throws exception {
string cont = "";
string cont2=jm(yjy(cont));
if(cont.equals(cont2)){
system.out.println("比对成功");
}else{
system.out.println("比对失败");
}
}
public static string yjy(string cont) throws exception {
system.out.println("明文长度:" + cont.length());
// 第一次压缩
cont = ziputil2.compress(cont);
system.out.println("压缩后:" + cont.length());
// 第一次加密
cont = desutil.encrypt(cont, desutil.pwd_key);
system.out.println("加密长度:" + cont.length());
// 第二次压缩
cont = ziputil2.compress(cont);
system.out.println("再压缩:" + cont.length());
return cont;
}
public static string jm(string cont) throws exception {
system.out.println("-----------------------------");
system.out.println("密文长度:" + cont.length());
// 第一次解压缩
cont = ziputil2.uncompress(cont);
system.out.println("解压缩:" + cont.length());
// 第一次解密
cont = desutil.decrypt(cont, desutil.pwd_key);
system.out.println("解密后:" + cont.length());
// 第二次解压缩
cont = ziputil2.uncompress(cont);
system.out.println("再解压:" + cont.length());
return cont;
}
desutil
import java.io.ioexception;
import java.security.securerandom;
import javax.crypto.cipher;
import javax.crypto.secretkey;
import javax.crypto.secretkeyfactory;
import javax.crypto.spec.deskeyspec;
import decoder.base64decoder;
import decoder.base64encoder;
public class desutil {
private final static string des = "des";
public final static string pwd_key = "mzthpwdjm";
public final static string id_key = "mzthidjm";
public static void main(string[] args) throws exception {
string data = "xkajsdasdk'al;ks'dl;kasl;d";
system.err.println("加密:"+encrypt(data, pwd_key));
system.err.println("解密:" +decrypt(encrypt(data, pwd_key), pwd_key));
}
/**
* description 根据键值进行加密
*
* @param data
* @param key
* 加密键byte数组
* @return
* @throws exception
*/
public static string encrypt(string data, string key) throws exception {
byte[] bt = encrypt(data.getbytes(), key.getbytes());
string strs = new base64encoder().encode(bt);
return strs;
}
/**
* description 根据键值进行解密
*
* @param data
* @param key
* 加密键byte数组
* @return
* @throws ioexception
* @throws exception
*/
public static string decrypt(string data, string key) throws ioexception,
exception {
if (data == null)
return null;
base64decoder decoder = new base64decoder();
byte[] buf = decoder.decodebuffer(data);
byte[] bt = decrypt(buf, key.getbytes());
return new string(bt);
}
/**
* description 根据键值进行加密
*
* @param data
* @param key
* 加密键byte数组
* @return
* @throws exception
*/
private static byte[] encrypt(byte[] data, byte[] key) throws exception {
// 生成一个可信任的随机数源
securerandom sr = new securerandom();
// 从原始密钥数据创建deskeyspec对象
deskeyspec dks = new deskeyspec(key);
// 创建一个密钥工厂,然后用它把deskeyspec转换成secretkey对象
secretkeyfactory keyfactory = secretkeyfactory.getinstance(des);
secretkey securekey = keyfactory.generatesecret(dks);
// cipher对象实际完成加密操作
cipher cipher = cipher.getinstance(des);
// 用密钥初始化cipher对象
cipher.init(cipher.encrypt_mode, securekey, sr);
return cipher.dofinal(data);
}
/**
* description 根据键值进行解密
*
* @param data
* @param key
* 加密键byte数组
* @return
* @throws exception
*/
private static byte[] decrypt(byte[] data, byte[] key) throws exception {
// 生成一个可信任的随机数源
securerandom sr = new securerandom();
// 从原始密钥数据创建deskeyspec对象
deskeyspec dks = new deskeyspec(key);
// 创建一个密钥工厂,然后用它把deskeyspec转换成secretkey对象
secretkeyfactory keyfactory = secretkeyfactory.getinstance(des);
secretkey securekey = keyfactory.generatesecret(dks);
// cipher对象实际完成解密操作
cipher cipher = cipher.getinstance(des);
// 用密钥初始化cipher对象
cipher.init(cipher.decrypt_mode, securekey, sr);
return cipher.dofinal(data);
}
}
ziputil2
.
import java.io.bytearrayinputstream;
import java.io.bytearrayoutputstream;
import java.io.ioexception;
import java.util.zip.gzipinputstream;
import java.util.zip.gzipoutputstream;
// 将一个字符串按照zip方式压缩和解压缩
public class ziputil2 {
// 测试方法
public static void main(string[] args) throws ioexception {
// 测试字符串
string str = "";
system.out.println("原长度:" + str.length());
system.out.println("压缩后:" + ziputil2.compress(str).length());
system.out
.println("解压缩:" + ziputil2.uncompress(ziputil2.compress(str)));
}
// 压缩
public static string compress(string str) throws ioexception {
if (str == null || str.length() == 0) {
return str;
}
bytearrayoutputstream out = new bytearrayoutputstream();
gzipoutputstream gzip = new gzipoutputstream(out);
gzip.write(str.getbytes());
gzip.close();
return out.tostring("iso-8859-1");
}
// 解压缩
public static string uncompress(string str) throws ioexception {
if (str == null || str.length() == 0) {
return str;
}
bytearrayoutputstream out = new bytearrayoutputstream();
bytearrayinputstream in = new bytearrayinputstream(
str.getbytes("iso-8859-1"));
gzipinputstream gunzip = new gzipinputstream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
// tostring()使用平台默认编码,也可以显式的指定如tostring("gbk")
return out.tostring();
}
}
上一篇: Android常用对话框使用大全
下一篇: Java解决约瑟夫问题代码实例