Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.security.SecureRandom;
Import Javax.crypto.Cipher;
Import Javax.crypto.CipherInputStream;
Import Javax.crypto.SecretKey;
Import Javax.crypto.SecretKeyFactory;
Import Javax.crypto.spec.DESKeySpec;
Import Javax.crypto.spec.IvParameterSpec;
public class Destool {
private static final String passkey = "AFASDF";
private static final String Deskey = "ASFSDFSDF";
/**
* @Comments: Encrypt files
* @param filePath the file path to encrypt
* @param filename File
* @param mode encryption mode encryption: Cipher.encrypt_mode decryption: Cipher.decrypt_mode
* @return
*/
public static string Encoderordecoder (String FilePath, string fileName, int mode) {
InputStream is = null;
OutputStream out = null;
CipherInputStream cis = NULL;
try {
securerandom sr = new SecureRandom ();
Deskeyspec DKs = new 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 (mode, SecureKey, IV, SR);
File Encoderfile = new file (FilePath + file.separator + "encoder");
if (!encoderfile.exists ()) {
Encoderfile.mkdir ();
}
is = new FileInputStream (FilePath + file.separator + fileName);
out = new FileOutputStream (FilePath + file.separator + "encoder"
+ File.separator + fileName);
CIS = new CipherInputStream (IS, cipher);
byte[] buffer = new byte[1024];
int R;
while ((r = cis.read (buffer)) > 0) {
Out.write (buffer, 0, R);
}
} catch (Exception e) {
e.printstacktrace ();
 &NBSP} finally {
try {
if (is!= null) {
is.close ();
}
if (cis!= null) {
cis.close ();
   &NBSP}
if (out!= null) {
out.close ();
}
} catch (Exception E1) {
}
}
Return FilePath + file.separator + "encoder" + file.separator
+ FileName;
}
/** @Comments: Encrypt strings
* @param src Source string
* @param mode encryption mode encryption: Cipher.encrypt_mode decryption: Cipher.decrypt_mode
* @return
*/
public static string Encoderordecoder (String src, int mode) {
String tag= "";
InputStream is = null;
OutputStream out = null;
CipherInputStream cis = NULL;
try {
securerandom sr = new SecureRandom ();
Deskeyspec DKs = new 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 (mode, SecureKey, IV, SR);
CIS = new CipherInputStream (New Bytearrayinputstream (Src.getbytes ()), cipher);
Out=new Bytearrayoutputstream ();
byte[] buffer = new byte[1024];
int R;
while ((r = cis.read (buffer)) > 0) {
Out.write (buffer, 0, R);
}
Tag=out.tostring ();
catch (Exception e) {
E.printstacktrace ();
finally {
try {
if (is!= null) {
Is.close ();
}
if (CIS!= null) {
Cis.close ();
}
if (out!= null) {
Out.close ();
}
catch (Exception E1) {
}
}
return tag;
}
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));
}
}