สตริงที่ฉันทดสอบคือซอร์สโค้ด jQuery
ความยาวข้อความธรรมดา: 78082
หลังจากการบีบอัด: 26566
ความยาวการเข้ารหัส: 54746
recompression: 41647
-
ความยาวรหัสผ่าน: 41647
การบีบอัด: 54746
หลังจากถอดรหัส: 26566
การประกาศใหม่: 78082
-
การเปรียบเทียบสำเร็จ
DES ต้องการ jar: sun.misc.base64decoder.jar
ทดสอบ
การคัดลอกรหัสมีดังนี้:
โมฆะคงที่สาธารณะหลัก (สตริง [] args) โยนข้อยกเว้น {
สตริง cont = "";
สตริง cont2 = jm (yjy (ต่อ));
if (cont.equals (cont2)) {
System.out.println ("การเปรียบเทียบสำเร็จ");
}อื่น{
System.out.println ("การเปรียบเทียบล้มเหลว");
-
-
สตริงคงที่สาธารณะ yjy (เนื้อหาสตริง) โยนข้อยกเว้น {
System.out.println ("ความยาวข้อความธรรมดา:" + cont.length ());
// การบีบอัดครั้งแรก
cont = ziputil2.compress (ต่อ);
System.out.println ("หลังจากการบีบอัด:" + cont.length ());
// การเข้ารหัสครั้งแรก
cont = desutil.encrypt (ต่อ, desutil.pwd_key);
System.out.println ("ความยาวการเข้ารหัส:" + cont.length ());
// การบีบอัดครั้งที่สอง
cont = ziputil2.compress (ต่อ);
System.out.println ("recompress:" + cont.length ());
ส่งคืนต่อ;
-
สตริงคงที่สาธารณะ JM (เนื้อหาสตริง) พ่นข้อยกเว้น {
System.out.println ("-----------------------------");
System.out.println ("ความยาว ciphertext:" + cont.length ());
// บีบอัดครั้งแรก
cont = ziputil2.uncompress (ต่อ);
System.out.println ("decompress:" + cont.length ());
// decrypt เป็นครั้งแรก
cont = desutil.decrypt (cont, desutil.pwd_key);
System.out.println ("หลังการถอดรหัส:" + cont.length ());
// การบีบอัดครั้งที่สอง
cont = ziputil2.uncompress (ต่อ);
System.out.println ("extract:" + cont.length ());
ส่งคืนต่อ;
-
การตกตะกอน
การคัดลอกรหัสมีดังนี้:
นำเข้า java.io.ioException;
นำเข้า Java.security.Securerandom;
นำเข้า Javax.crypto.cipher;
นำเข้า Javax.crypto.secretkey;
นำเข้า Javax.crypto.secretkeyFactory;
นำเข้า Javax.crypto.spec.deskeyspec;
นำเข้า decoder.base64decoder;
นำเข้า decoder.base64encoder;
ชั้นเรียนสาธารณะ Desutil {
สตริงคงสุดท้ายส่วนตัว DES = "DES";
สตริงคงที่สาธารณะสุดท้าย pwd_key = "mzthpwdjm";
สตริงคงที่สาธารณะสุดท้าย ID_KEY = "MZTHIDJM";
โมฆะคงที่สาธารณะหลัก (สตริง [] args) โยนข้อยกเว้น {
String data = "xkajsdasdk'al; ks'dl; kasl; d";
System.err.println ("เข้ารหัส:"+เข้ารหัส (ข้อมูล, pwd_key));
System.err.println ("DECRYPT:" +DECRYPT (ENCRYPT (ข้อมูล, PWD_KEY), PWD_KEY));
-
-
* คำอธิบายการเข้ารหัสตามค่าคีย์
-
* ข้อมูล @param
* @param key
* การเข้ารหัสคีย์ไบต์อาร์เรย์
* @กลับ
* @throws ยกเว้น
-
การเข้ารหัสสตริงคงที่สาธารณะ (ข้อมูลสตริง, คีย์สตริง) โยนข้อยกเว้น {
ไบต์ [] bt = เข้ารหัส (data.getBytes (), key.getBytes ());
strs strs = ใหม่ base64encoder (). encode (bt);
กลับมา Strs;
-
-
* คำอธิบายถอดรหัสตามค่าคีย์
-
* ข้อมูล @param
* @param key
* การเข้ารหัสคีย์ไบต์อาร์เรย์
* @กลับ
* @throws ioexception
* @throws ยกเว้น
-
Decrypt สตริงคงที่สาธารณะ (ข้อมูลสตริง, คีย์สตริง) พ่น IOException
ข้อยกเว้น {
if (data == null)
คืนค่า null;
Base64Decoder decoder = ใหม่ base64decoder ();
ไบต์ [] buf = decoder.decodeBuffer (ข้อมูล);
ไบต์ [] bt = decrypt (buf, key.getBytes ());
ส่งคืนสตริงใหม่ (BT);
-
-
* คำอธิบายการเข้ารหัสตามค่าคีย์
-
* ข้อมูล @param
* @param key
* การเข้ารหัสคีย์ไบต์อาร์เรย์
* @กลับ
* @throws ยกเว้น
-
ไบต์คงที่ส่วนตัว [] เข้ารหัส (ไบต์ [] ข้อมูลไบต์ [] คีย์) โยนข้อยกเว้น {
// สร้างแหล่งหมายเลขสุ่มที่เชื่อถือได้
Securerandom SR = ใหม่ securerandom ();
// สร้างวัตถุ DeskeySpec จากข้อมูลคีย์ดั้งเดิม
Deskeyspec DKS = ใหม่ Deskeyspec (คีย์);
// สร้างโรงงานสำคัญและใช้เพื่อแปลง 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);
ส่งคืน cipher.dofinal (ข้อมูล);
-
-
* คำอธิบายถอดรหัสตามค่าคีย์
-
* ข้อมูล @param
* @param key
* การเข้ารหัสคีย์ไบต์อาร์เรย์
* @กลับ
* @throws ยกเว้น
-
ไบต์คงที่ส่วนตัว [] decrypt (byte [] data, byte [] คีย์) โยนข้อยกเว้น {
// สร้างแหล่งหมายเลขสุ่มที่เชื่อถือได้
Securerandom SR = ใหม่ securerandom ();
// สร้างวัตถุ DeskeySpec จากข้อมูลคีย์ดั้งเดิม
Deskeyspec DKS = ใหม่ Deskeyspec (คีย์);
// สร้างโรงงานสำคัญและใช้เพื่อแปลง 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);
ส่งคืน cipher.dofinal (ข้อมูล);
-
-
ziputil2
-
การคัดลอกรหัสมีดังนี้:
นำเข้า Java.io.ByTearrayInputStream;
นำเข้า Java.io.ByTeArrayOutputStream;
นำเข้า java.io.ioException;
นำเข้า java.util.zip.gzipinputstream;
นำเข้า java.util.zip.gzipoutputstream;
// บีบอัดและคลายสตริงในโหมดซิป
คลาสสาธารณะ ziputil2 {
// วิธีทดสอบ
โมฆะคงที่สาธารณะหลัก (String [] args) พ่น IOException {
// ทดสอบสตริง
string str = "";
System.out.println ("ความยาวดั้งเดิม:" + str.length ());
System.out.println ("หลังจากการบีบอัด:" + ziputil2.compress (str) .length ());
System.out
.println ("decompress:" + ziputil2.uncompress (ziputil2.compress (str)));
-
// การบีบอัด
การบีบอัดสตริงสาธารณะสาธารณะ (String Str) พ่น IOException {
if (str == null || str.length () == 0) {
return str;
-
ByTeArrayOutputStream out = new ByteArrayOutputStream ();
gzipoutputStream gzip = ใหม่ gzipoutputstream (out);
gzip.write (str.getBytes ());
gzip.close ();
กลับออกมา tostring ("ISO-8859-1");
-
// คลายการบีบอัด
สตริงคงที่สาธารณะไม่บีบอัด (String Str) พ่น IOException {
if (str == null || str.length () == 0) {
return str;
-
ByTeArrayOutputStream out = new ByteArrayOutputStream ();
ByteArrayInputStream ใน = ใหม่ byteArrayInputStream (
str.getBytes ("ISO-8859-1"));
gzipinputstream gunzip = ใหม่ gzipinputstream (in);
ไบต์ [] บัฟเฟอร์ = ไบต์ใหม่ [256];
int n;
ในขณะที่ ((n = gunzip.read (บัฟเฟอร์))> = 0) {
out.write (บัฟเฟอร์, 0, n);
-
// TOSTRING () ใช้การเข้ารหัสเริ่มต้นของแพลตฟอร์มและยังสามารถระบุได้อย่างชัดเจนเช่น TOSTRING (GBK)
กลับออกไป. tostring ();
-
-