نسخة الكود كما يلي:
استيراد 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.secretkeke ؛
استيراد javax.crypto.secretkeyfactory ؛
استيراد javax.crypto.spec.deskeyspec ؛
استيراد javax.crypto.spec.ivparameterspec ؛
الطبقة العامة DESTOOL {
static passkey static static static static = "afasdf" ؛
Static Final Final String Deskey = "ASFSDFSDF" ؛
/**
* @comments: تشفير الملف
* param filepath filepath لتشفير
* ملف اسم ملف Param
* param وضع تشفير الوضع التشفير: cipher.encrypt_mode decryption: cipher.decrypt_mode
* @يعود
*/
EncoderorDecoder Static Static Static (سلسلة FilePath ، اسم ملف السلسلة ، وضع int) {
inputStream هو = فارغ ؛
OutputStream Out = null ؛
CipherInputStream Cis = NULL ؛
يحاول {
SecurerAndom sr = new SecurerAndom () ؛
deskeyspec dks = جديد deskeyspec (deskey.getBytes ()) ؛
secretKeyFactory keyfactory = secretKeyFactory.getInstance ("des") ؛
SecretKey SecureKey = keyfactory.generateSecret (DKS) ؛
Ivparameterspec IV = New ivparameterspec (passkey.getBytes ()) ؛
cipher cipher = cipher.getInstance ("des/cbc/pkcs5padding") ؛
cipher.init (الوضع ، الآمن ، الرابع ، sr) ؛
ملف encoderFile = ملف جديد (FilePath + file.separator + "Encoder") ؛
if (! encoderfile.exists ()) {
encoderfile.mkdir () ؛
}
هو = جديد fileInputStream (filePath + file.separator + اسم الملف) ؛
Out = fileOutputStream جديد (FilePath + file.separator + "Encoder"
+ file.separator + اسم الملف) ؛
CIS = CipherInputStream جديد (IS ، الشفرات) ؛
Byte [] Buffer = New Byte [1024] ؛
int r ؛
بينما ((r = cis.read (buffer))> 0) {
out.write (Buffer ، 0 ، r) ؛
}
} catch (استثناء e) {
E.PrintStackTrace () ؛
} أخيراً {
يحاول {
إذا (هو! = فارغ) {
is.close () ؛
}
if (cis! = null) {
cis.close () ؛
}
if (out! = null) {
out.close () ؛
}
} catch (استثناء e1) {
}
}
إرجاع filepath + file.separator + "encoder" + file.separator
+ اسم الملف ؛
}
/**@التعليقات: تشفير السلاسل
* @Param SRC Source String
* param وضع تشفير الوضع التشفير: cipher.encrypt_mode decryption: cipher.decrypt_mode
* @يعود
*/
encoderordecoder Static Static Static (السلسلة SRC ، وضع int) {
علامة سلسلة = "" ؛
inputStream هو = فارغ ؛
OutputStream Out = null ؛
CipherInputStream Cis = NULL ؛
يحاول {
SecurerAndom sr = new Securerandom () ؛
deskeyspec dks = جديد deskeyspec (deskey.getBytes ()) ؛
secretKeyFactory keyfactory = secretKeyFactory.getInstance ("des") ؛
SecretKey SecureKey = keyfactory.generateSecret (DKS) ؛
Ivparameterspec IV = New ivparameterspec (passkey.getBytes ()) ؛
cipher cipher = cipher.getInstance ("des/cbc/pkcs5padding") ؛
cipher.init (الوضع ، الآمن ، الرابع ، sr) ؛
CIS = جديد CipherInputStream (BytearRayInputStream (SRC.GetBytes ()) ، الشفرات) ؛
Out = new ByteArrayOutputStream () ؛
Byte [] Buffer = New Byte [1024] ؛
int r ؛
بينما ((r = cis.read (buffer))> 0) {
out.write (Buffer ، 0 ، r) ؛
}
tag = out.toString () ؛
} catch (استثناء e) {
E.PrintStackTrace () ؛
} أخيراً {
يحاول {
إذا (هو! = فارغ) {
is.close () ؛
}
if (cis! = null) {
cis.close () ؛
}
if (out! = null) {
out.close () ؛
}
} catch (استثناء e1) {
}
}
عودة العلامة ؛
}
الفراغ الثابت العام الرئيسي (سلسلة [] args) {
System.out.println ("AAA") ؛
String t = Encoderordecoder ("AAA" ، cipher.encrypt_mode) ؛
system.out.println (t) ؛
System.out.println (Encoderordecoder (T ، cipher.decrypt_mode)) ؛
}
}