Java string compression unzip example
- 2020-04-01 03:13:07
- OfStack
The string I tested is the JQuery source code.
Plaintext length :78082
After compression: 26,566
Length of encryption: 54746
Compression: 41647
-----------------------------
Ciphertext length :41647
Decompression: 54746
Decrypted: 26566
Decompression: 78082
-----------------------------
Success is
Des need Jar: sun. Misc. BASE64Decoder. Jar
The Test
public static void main(String[] args) throws Exception {
String cont = "";
String cont2=jm(yjy(cont));
if(cont.equals(cont2)){
System.out.println(" Success is ");
}else{
System.out.println(" Than failure ");
}
}
public static String yjy(String cont) throws Exception {
System.out.println(" Clear the length :" + cont.length());
//First compression
cont = ZipUtil2.compress(cont);
System.out.println(" After the compression: " + cont.length());
//First encryption
cont = DesUtil.encrypt(cont, DesUtil.PWD_KEY);
System.out.println(" Length of encryption: " + cont.length());
//Second compression
cont = ZipUtil2.compress(cont);
System.out.println(" recompression :" + cont.length());
return cont;
}
public static String jm(String cont) throws Exception {
System.out.println("-----------------------------");
System.out.println(" The length of the cipher :" + cont.length());
//The first unzip
cont = ZipUtil2.uncompress(cont);
System.out.println(" Extract: " + cont.length());
//First decryption
cont = DesUtil.decrypt(cont, DesUtil.PWD_KEY);
System.out.println(" After decryption: " + cont.length());
//The second unzip
cont = ZipUtil2.uncompress(cont);
System.out.println(" Extract: " + 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(" Encryption: "+encrypt(data, PWD_KEY));
System.err.println(" Decryption: " +decrypt(encrypt(data, PWD_KEY), PWD_KEY));
}
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;
}
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);
}
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
//Generate a trusted random number source
SecureRandom sr = new SecureRandom();
//Create the DESKeySpec object from the original key data
DESKeySpec dks = new DESKeySpec(key);
//Create a key factory and use it to transform the DESKeySpec into a SecretKey object
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
//A Cipher object actually does the encryption
Cipher cipher = Cipher.getInstance(DES);
//Initializes a Cipher object with a key
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
//Generate a trusted random number source
SecureRandom sr = new SecureRandom();
//Create the DESKeySpec object from the original key data
DESKeySpec dks = new DESKeySpec(key);
//Create a key factory and use it to transform the DESKeySpec into a SecretKey object
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
//A Cipher object actually does the decryption
Cipher cipher = Cipher.getInstance(DES);
//Initializes a Cipher object with a key
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 and unzip a string & NBSP;
public class ZipUtil2 {
//The test method
public static void main(String[] args) throws IOException {
//Test string
String str = "";
System.out.println(" The original length: " + str.length());
System.out.println(" After the compression: " + ZipUtil2.compress(str).length());
System.out
.println(" Extract: " + ZipUtil2.uncompress(ZipUtil2.compress(str)));
}
//The compression
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");
}
//unzip
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 () USES the platform default code and can be explicitly specified as toString(& Quot; GBK& Quot;)
return out.toString();
}
}