La copie de code est la suivante:
Importer java.io.ByteArrayInputStream;
Importer java.io.ByteArrayOutputStream;
Importer java.io.file;
import java.io.fileInputStream;
Importer java.io.fileOutputStream;
import java.io.inputStream;
import java.io.outputStream;
Importer Java.Security.SeCurère et;
import javax.crypto.cipher;
Importer javax.crypto.cipherinputStream;
Importer Javax.crypto.SecretKey;
Importer Javax.crypto.SecretKeyFactory;
Importer javax.crypto.spe.deskeyspec;
Importer javax.crypto.spe.ivParameterspec;
classe publique Destool {
String final statique privé PassKey = "afasdf";
String final statique privé deskekey = "asfsdfsdf";
/ **
* @Comments: crypter le fichier
* @param filepath filepath pour crypter
* fichier de nom de fichier @param
* @Param Mode Encryption Mode Encryption: Cipher.Encrypt_Mode Decryption: cipher.decrypt_mode
* @retour
* /
EncoderorDecOder de chaîne statique publique (String FilePath, String FileName, mode int) {
InputStream est = null;
OutputStream out = null;
CIPHERINPUTSTREAM cis = null;
essayer {
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 = nouveau fichier (filepath + file.separator + "Encodeur");
if (! EncoderFile.exists ()) {
Encoderfile.mkdir ();
}
is = new FileInputStream (filepath + file.separator + filename);
out = new FileOutputStream (filepath + file.separator + "Encodeur"
+ File.separator + nom de fichier);
cis = nouveau cipheRinputStream (IS, chiffre);
octet [] tampon = nouveau octet [1024];
int r;
while ((r = cis.read (tampon))> 0) {
out.write (tampon, 0, r);
}
} catch (exception e) {
e.printStackTrace ();
} enfin {
essayer {
if (est! = null) {
is.close ();
}
if (cis! = null) {
cis.close ();
}
if (out! = null) {
out.close ();
}
} catch (exception e1) {
}
}
return filepath + file.separator + "Encodeur" + file.separator
+ nom de fichier;
}
/ ** @ Commentaires: Crypt Strings
* @Param Src Source String
* @Param Mode Encryption Mode Encryption: Cipher.Encrypt_Mode Decryption: cipher.decrypt_mode
* @retour
* /
EncoderorDecOder de chaîne statique publique (String Src, mode int) {
String tag = "";
InputStream est = null;
OutputStream out = null;
CIPHERINPUTSTREAM cis = null;
essayer {
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 ();
octet [] tampon = nouveau octet [1024];
int r;
while ((r = cis.read (tampon))> 0) {
out.write (tampon, 0, r);
}
tag = out.toString ();
} catch (exception e) {
e.printStackTrace ();
} enfin {
essayer {
if (est! = null) {
is.close ();
}
if (cis! = null) {
cis.close ();
}
if (out! = null) {
out.close ();
}
} catch (exception e1) {
}
}
Tag de retour;
}
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));
}
}