Java String Compression decompression sample _java

Source: Internet
Author: User
Tags decrypt uncompress


The string I tested was the jquery source.

PlainText Length: 78082
Compressed after: 26566
Encryption Length: 54746
Re-compressed: 41647
-----------------------------
Cipher Length: 41647
Decompression: 54746
After decryption: 26566
Re-extract: 78082
-----------------------------
Compared to success

Des needs Jar:sun.misc.BASE64Decoder.jar.

Test

Copy Code code as follows:

public static void Main (string[] args) throws Exception {
String cont = "";
String CONT2=JM (YJY (cont));
if (Cont.equals (Cont2)) {
System.out.println ("Compare to Success");
}else{
System.out.println ("Compare to fail");
}
}

public static string Yjy (String cont) throws Exception {
System.out.println ("Clear text length:" + cont.length ());
First time compression
Cont = ZIPUTIL2.COMPRESS (cont);
SYSTEM.OUT.PRINTLN ("Compressed:" + cont.length ());
Encryption for the first time
Cont = DESUTIL.ENCRYPT (cont, desutil.pwd_key);
SYSTEM.OUT.PRINTLN ("Encryption Length:" + cont.length ());
Second compression
Cont = ZIPUTIL2.COMPRESS (cont);
System.out.println ("Re-compress:" + cont.length ());
Return cont;
}

public static string JM (String cont) throws Exception {
System.out.println ("-----------------------------");
System.out.println ("Ciphertext length:" + cont.length ());

Decompression for the first time
Cont = ZIPUTIL2.UNCOMPRESS (cont);
System.out.println ("Decompression:" + cont.length ());

Decryption for the first time
Cont = DESUTIL.DECRYPT (cont, desutil.pwd_key);
System.out.println ("After decryption:" + cont.length ());

Second decompression
Cont = ZIPUTIL2.UNCOMPRESS (cont);
System.out.println ("Re-extract:" + cont.length ());

Return cont;
}

Desutil

Copy Code code as follows:

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));
}

/**
* Description is encrypted based on the key value
*
* @param data
* @param key
* Encryption key byte array
* @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 decryption based on key value
  *
  * @param data
  * @param key
  *&NBSP ;           encryption key byte array
  * @return
  * @throws IOException
  * @throws Exception
  */
 public static string decrypt (string data, string key) thr oWS 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 is encrypted based on the key value
*
* @param data
* @param key
* Encryption key byte array
* @return
* @throws Exception
*/
private static byte[] Encrypt (byte[] data, byte[] key) throws Exception {
Generate a trustworthy random number source
securerandom sr = new SecureRandom ();

Create a Deskeyspec object from raw key data
Deskeyspec DKs = new Deskeyspec (key);

Create a key factory and use it to convert Deskeyspec to Secretkey objects
Secretkeyfactory keyfactory = secretkeyfactory.getinstance (DES);
Secretkey SecureKey = Keyfactory.generatesecret (DKS);

The Cipher object actually completes the encryption operation
Cipher Cipher = cipher.getinstance (DES);

Initializing a Cipher object with a key
Cipher.init (Cipher.encrypt_mode, SecureKey, SR);

return cipher.dofinal (data);
}

/**
* Description is decrypted based on the key value
*
* @param data
* @param key
* Encryption key byte array
* @return
* @throws Exception
*/
private static byte[] Decrypt (byte[] data, byte[] key) throws Exception {
Generate a trustworthy random number source
securerandom sr = new SecureRandom ();

Create a Deskeyspec object from raw key data
Deskeyspec DKs = new Deskeyspec (key);

Create a key factory and use it to convert Deskeyspec to Secretkey objects
Secretkeyfactory keyfactory = secretkeyfactory.getinstance (DES);
Secretkey SecureKey = Keyfactory.generatesecret (DKS);

The Cipher object actually completes the decryption operation
Cipher Cipher = cipher.getinstance (DES);

Initializing a Cipher object with a key
Cipher.init (Cipher.decrypt_mode, SecureKey, SR);

return cipher.dofinal (data);
}
}

ZipUtil2
.

Copy Code code as follows:

Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.util.zip.GZIPInputStream;
Import Java.util.zip.GZIPOutputStream;

Compress and decompress a string in zip mode
public class ZipUtil2 {

Test method
public static void Main (string[] args) throws IOException {

Test string
String str = "";
System.out.println ("Original length:" + str.length ());
SYSTEM.OUT.PRINTLN ("Compressed:" + ziputil2.compress (str). length ());
System.out
. println ("Decompression:" + ziputil2.uncompress (ziputil2.compress (str));
}

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");
}

Decompression
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 encoding, or it can explicitly specify such as ToString ("GBK")
return out.tostring ();
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.