The string I tested was JQuery source code.
Plain text length: 78082
After compression: 26566
Encryption length: 54746
Recompression: 41647
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Password length: 41647
Decompression: 54746
After decryption: 26566
Re-decompression: 78082
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Comparison successfully
Des requires Jar: sun.misc.BASE64Decoder.jar
Test
The code copy is as follows:
public static void main(String[] args) throws Exception {
String cont = "";
String cont2=jm(yjy(cont));
if(cont.equals(cont2)){
System.out.println("Comparison successful");
}else{
System.out.println("Comparison failed");
}
}
public static String yjy(String content) throws Exception {
System.out.println("plaintext length:" + cont.length());
// First compression
cont = ZipUtil2.compress(cont);
System.out.println("After compression:" + cont.length());
// First time encryption
cont = DesUtil.encrypt(cont, DesUtil.PWD_KEY);
System.out.println("Encryption length:" + cont.length());
// Second compression
cont = ZipUtil2.compress(cont);
System.out.println("Recompress:" + cont.length());
return cont;
}
public static String jm(String content) throws Exception {
System.out.println("-----------------------------");
System.out.println("ciphertext length:" + cont.length());
// Decompress the first time
cont = ZipUtil2.uncompress(cont);
System.out.println("Decompress:" + cont.length());
// Decrypt for the first time
cont = DesUtil.decrypt(cont, DesUtil.PWD_KEY);
System.out.println("After decryption:" + cont.length());
// The second decompression
cont = ZipUtil2.uncompress(cont);
System.out.println("Re-extract:" + cont.length());
return cont;
}
DesUtil
The code copy is 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("Encrypt:"+encrypt(data, PWD_KEY));
System.err.println("Decrypt:" +decrypt(encrypt(data, PWD_KEY), PWD_KEY));
}
/**
* Description Encryption based on 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 Decrypts according to key value
*
* @param data
* @param key
* Encryption key byte array
* @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 Encryption based on 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 trusted random number source
SecureRandom sr = new SecureRandom();
// Create a DESKeySpec object from the original key data
DESKeySpec dks = new DESKeySpec(key);
// Create a key factory and use it to convert DESKeySpec to a SecretKey object
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// The Cipher object actually completes the encryption operation
Cipher cipher = Cipher.getInstance(DES);
// Initialize the Cipher object with a key
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
/**
* Description Decrypts according to 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 trusted random number source
SecureRandom sr = new SecureRandom();
// Create a DESKeySpec object from the original key data
DESKeySpec dks = new DESKeySpec(key);
// Create a key factory and use it to convert DESKeySpec to a SecretKey object
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// The Cipher object actually completes the decryption operation
Cipher cipher = Cipher.getInstance(DES);
// Initialize the Cipher object with a key
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
}
ZipUtil2
.
The code copy is 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("After compression:" + ZipUtil2.compress(str).length());
System.out
.println("Decompress:" + 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");
}
// Decompress
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, and can also be explicitly specified such as toString(GBK)
return out.toString();
}
}