java字符串緊縮解壓示例。本站提示廣大學習愛好者:(java字符串緊縮解壓示例)文章只能為提供參考,不一定能成為您想要的結果。以下是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();
}
}