Salinan kode adalah sebagai berikut:
impor java.io.bytearrayInputStream;
impor java.io.bytearrayoutputStream;
impor java.io.file;
impor java.io.fileInputStream;
impor java.io.fileoutputStream;
impor java.io.inputstream;
impor java.io.outputStream;
impor java.security.securerandom;
impor javax.crypto.cipher;
impor javax.crypto.cipherinputstream;
impor javax.crypto.secretkey;
impor javax.crypto.secretkeyfactory;
impor javax.crypto.spec.deskeyspec;
impor javax.crypto.spec.ivparameterspec;
Destool kelas publik {
Private Static Final String Passkey = "afasdf";
string final statis pribadi deskey = "asfsdfsdf";
/**
* @Comments: mengenkripsi file
* @param filepath filepath untuk enkripsi
* file nama file @param
* Enkripsi mode enkripsi mode @param: cipher.encrypt_mode Decryption: cipher.decrypt_mode
* @kembali
*/
Public Static String encoderordecoder (String FilePath, String FileName, Mode int) {
InputStream adalah = null;
OutputStream out = null;
Cipherinputstream cis = null;
mencoba {
Securerandom Sr = new SecureRandom ();
Deskeyspec dks = deskeyspec baru (deskey.getbytes ());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance ("des");
Secretkey SecureKey = KeyFactory.GenerateSecret (DKS);
Ivparameterspec iv = ivparameterspec baru (passkey.getbytes ());
Cipher cipher = cipher.getInstance ("des/cbc/pkcs5padding");
cipher.init (Mode, SecureKey, IV, SR);
File encoderFile = File baru (FilePath + File.Separator + "Encoder");
if (! encoderfile.exists ()) {
encoderfile.mkdir ();
}
IS = FileInputStream baru (FilePath + File.Separator + FileName);
out = FileOutputStream baru (FilePath + File.Separator + "Encoder"
+ File.separator + nama file);
cis = cipherinputStream baru (IS, cipher);
byte [] buffer = byte baru [1024];
int r;
while ((r = cis.read (buffer))> 0) {
out.write (buffer, 0, r);
}
} catch (Exception e) {
e.printstacktrace ();
} Akhirnya {
mencoba {
if (is! = null) {
is.close ();
}
if (cis! = null) {
cis.close ();
}
if (out! = null) {
out.close ();
}
} catch (Exception e1) {
}
}
kembalikan filepath + file.separator + "encoder" + file.separator
+ nama file;
}
/**@Komentar: String Enkripsi
* @param SRC Source String
* Enkripsi mode enkripsi mode @param: cipher.encrypt_mode Decryption: cipher.decrypt_mode
* @kembali
*/
String statis public encoderordecoder (string src, mode int) {
String tag = "";
InputStream adalah = null;
OutputStream out = null;
Cipherinputstream cis = null;
mencoba {
Securerandom Sr = new SecureRandom ();
Deskeyspec dks = deskeyspec baru (deskey.getbytes ());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance ("des");
Secretkey SecureKey = KeyFactory.GenerateSecret (DKS);
Ivparameterspec iv = ivparameterspec baru (passkey.getbytes ());
Cipher cipher = cipher.getInstance ("des/cbc/pkcs5padding");
cipher.init (Mode, SecureKey, IV, SR);
cis = cipherinputStream baru (bytearrayInputStream baru (src.getbytes ()), cipher);
out = new ByteArrayOutputStream ();
byte [] buffer = byte baru [1024];
int r;
while ((r = cis.read (buffer))> 0) {
out.write (buffer, 0, r);
}
tag = out.toString ();
} catch (Exception e) {
e.printstacktrace ();
} Akhirnya {
mencoba {
if (is! = null) {
is.close ();
}
if (cis! = null) {
cis.close ();
}
if (out! = null) {
out.close ();
}
} catch (Exception e1) {
}
}
tag pengembalian;
}
public static void main (string [] args) {
System.out.println ("AAA");
String t = encoderordecoder ("aaa", cipher.encrypt_mode);
System.out.println (t);
System.out.println (encoderordecoder (t, cipher.decrypt_mode));
}
}