การคัดลอกรหัสมีดังนี้:
นำเข้า Java.io.ByTearrayInputStream;
นำเข้า Java.io.ByTeArrayOutputStream;
นำเข้า Java.io.File;
นำเข้า Java.io.FileInputStream;
นำเข้า Java.io.FileOutputStream;
นำเข้า Java.io.InputStream;
นำเข้า Java.io.OutputStream;
นำเข้า Java.security.Securerandom;
นำเข้า Javax.crypto.cipher;
นำเข้า Javax.crypto.CipherInputStream;
นำเข้า Javax.crypto.secretkey;
นำเข้า Javax.crypto.secretkeyFactory;
นำเข้า Javax.crypto.spec.deskeyspec;
นำเข้า Javax.crypto.spec.ivparameterspec;
ชั้นเรียนสาธารณะ destool {
สตริงสุดท้ายคงที่ส่วนตัว passkey = "afasdf";
สตริงสุดท้ายคงที่ส่วนตัว deskey = "asfsdfsdf";
-
* @Comments: เข้ารหัสไฟล์
* @param filepath filepath เพื่อเข้ารหัส
* ไฟล์ชื่อไฟล์ @param
* @param โหมดเข้ารหัสโหมดการเข้ารหัส: cipher.encrypt_mode decryption: cipher.decrypt_mode
* @กลับ
-
String String Public String encoderordecoder (String filepath, ชื่อไฟล์สตริง, โหมด int) {
InputStream คือ = null;
outputstream out = null;
CipherInputStream CIS = NULL;
พยายาม {
Securerandom SR = ใหม่ securerandom ();
Deskeyspec dks = ใหม่ Deskeyspec (deskey.getBytes ());
secretKeyFactory keyFactory = secretKeyFactory.getInstance ("des");
SecretKey SecureKey = KeyFactory.GenerAtesecret (DKS);
ivParameterspec IV = ใหม่ ivparameterspec (passkey.getBytes ());
CIPHER CIPHER = CIPHER.GETINSTANCE ("DES/CBC/PKCS5PADDING");
cipher.init (โหมด, SecureKey, IV, SR);
ไฟล์ encoderFile = ไฟล์ใหม่ (filePath + file.Saperator + "encoder");
if (! encoderfile.exists ()) {
encoderfile.mkdir ();
-
IS = ใหม่ FileInputStream (filePath + file.Sparator + ชื่อไฟล์);
out = new fileOutputStream (filePath + file.Saperator + "encoder"
+ file.Sparator + ชื่อไฟล์);
CIS = ใหม่ CipherInputStream (IS, CIPHER);
ไบต์ [] บัฟเฟอร์ = ไบต์ใหม่ [1024];
int r;
ในขณะที่ ((r = cis.read (บัฟเฟอร์))> 0) {
out.write (บัฟเฟอร์, 0, r);
-
} catch (Exception e) {
E.PrintStackTrace ();
} ในที่สุด {
พยายาม {
ถ้า (คือ! = null) {
is.close ();
-
ถ้า (cis! = null) {
cis.close ();
-
ถ้า (ออก! = null) {
out.close ();
-
} catch (Exception e1) {
-
-
ส่งคืน filepath + file.Saperator + "encoder" + file.Saperator
+ ชื่อไฟล์;
-
/**@ความคิดเห็น: Encrypt Strings
* @Param Src Source String
* @param โหมดเข้ารหัสโหมดการเข้ารหัส: cipher.encrypt_mode decryption: cipher.decrypt_mode
* @กลับ
-
String String Public String encoderordecoder (String Src, โหมด int) {
String tag = "";
InputStream คือ = null;
outputstream out = null;
CipherInputStream CIS = NULL;
พยายาม {
Securerandom SR = ใหม่ securerandom ();
Deskeyspec dks = ใหม่ Deskeyspec (deskey.getBytes ());
secretKeyFactory keyFactory = secretKeyFactory.getInstance ("des");
SecretKey SecureKey = KeyFactory.GenerAtesecret (DKS);
ivParameterspec IV = ใหม่ ivparameterspec (passkey.getBytes ());
CIPHER CIPHER = CIPHER.GETINSTANCE ("DES/CBC/PKCS5PADDING");
cipher.init (โหมด, SecureKey, IV, SR);
CIS = ใหม่ CipherInputStream (New ByteArrayInputStream (src.getBytes ()), cipher);
out = ใหม่ byteArrayOutputStream ();
ไบต์ [] บัฟเฟอร์ = ไบต์ใหม่ [1024];
int r;
ในขณะที่ ((r = cis.read (บัฟเฟอร์))> 0) {
out.write (บัฟเฟอร์, 0, r);
-
tag = out.toString ();
} catch (Exception e) {
E.PrintStackTrace ();
} ในที่สุด {
พยายาม {
ถ้า (คือ! = null) {
is.close ();
-
ถ้า (cis! = null) {
cis.close ();
-
ถ้า (ออก! = null) {
out.close ();
-
} catch (Exception e1) {
-
-
แท็ก return;
-
โมฆะคงที่สาธารณะหลัก (สตริง [] args) {
System.out.println ("AAA");
String t = encoderordecoder ("AAA", cipher.encrypt_mode);
System.out.println (t);
System.out.println (encoderordecoder (t, cipher.decrypt_mode));
-
-