A cópia do código é a seguinte:
importar java.io.byteArrayInputStream;
importar java.io.byteArrayOutputStream;
importar java.io.file;
importar java.io.fileInputStream;
importar java.io.fileOutputStream;
importar java.io.inputStream;
importar java.io.OutputStream;
importar java.Security.SecureRandom;
importar javax.crypto.cipher;
importar javax.crypto.cipherinputStream;
importar javax.crypto.secretkey;
importar javax.crypto.secretKeyFactory;
importar javax.crypto.spec.deskeyspec;
importar javax.crypto.spec.ivparameterspec;
classe pública Destool {
String final estática privada passKey = "AFASDF";
String final estática privada Deskey = "ASFSDFSDF";
/**
* @Comments: criptografar o arquivo
* @param filepath filepath para criptografar
* @param fileName File
* Modo de modo @param Criptografia de criptografia: cipher.encrypt_mode descriptografia: cipher.decrypt_mode
* @retornar
*/
public static string coderordecoder (string filepath, string filename, int mode) {
InputStream é = nulo;
OutputStream out = null;
CipherInputStream cis = nulo;
tentar {
SecureRandom SR = new SecureRandom ();
Deskeyspec DKS = new Deskeyspec (Deskey.getBytes ());
SecretKeyFactory KeyFactory = secretKeyFactory.getInstance ("DES");
SecretKey SecureKey = keyFactory.GenerateSecret (DKS);
IVParametersPec IV = novo IVParameterspec (passKey.getBytes ());
Cifra cifra = cipher.getInstance ("DES/CBC/PKCS5Padding");
cipher.init (modo, seguro, iv, sr);
Arquivo coderfile = novo arquivo (filepath + file.separator + "codificador");
if (! coderfile.exists ()) {
coderfile.mkdir ();
}
is = new FileInputStream (filepath + file.separator + nome do arquivo);
out = new FileOutputStream (filepath + file.separator + "codificador"
+ File.separator + nome do arquivo);
cis = novo cipherInputStream (IS, cifra);
byte [] buffer = novo byte [1024];
int r;
while ((r = cis.read (buffer))> 0) {
out.write (buffer, 0, r);
}
} catch (Exceção e) {
E.PrintStackTrace ();
} finalmente {
tentar {
se (é! = null) {
is.close ();
}
if (cis! = null) {
cis.close ();
}
if (out! = null) {
out.Close ();
}
} catch (Exceção E1) {
}
}
Retorne filepath + file.separator + "codificador" + file.separator
+ nome do arquivo;
}
/**@Comentários: Criptografar strings
* @param src string de origem
* Modo de modo @param Criptografia de criptografia: cipher.encrypt_mode descriptografia: cipher.decrypt_mode
* @retornar
*/
public static string coderordecoder (string src, int mode) {
String tag = "";
InputStream é = nulo;
OutputStream out = null;
CipherInputStream cis = nulo;
tentar {
SecureRandom SR = new SecureRandom ();
Deskeyspec DKS = new Deskeyspec (Deskey.getBytes ());
SecretKeyFactory KeyFactory = secretKeyFactory.getInstance ("DES");
SecretKey SecureKey = keyFactory.GenerateSecret (DKS);
IVParametersPec IV = novo IVParameterspec (passKey.getBytes ());
Cifra cifra = cipher.getInstance ("DES/CBC/PKCS5Padding");
cipher.init (modo, seguro, iv, sr);
cis = new cipherInputStream (new ByteArrayInputStream (src.getbytes ()), cifra);
out = novo bytearrayOutputStream ();
byte [] buffer = novo byte [1024];
int r;
while ((r = cis.read (buffer))> 0) {
out.write (buffer, 0, r);
}
tag = out.toString ();
} catch (Exceção e) {
E.PrintStackTrace ();
} finalmente {
tentar {
se (é! = null) {
is.close ();
}
if (cis! = null) {
cis.close ();
}
if (out! = null) {
out.Close ();
}
} catch (Exceção E1) {
}
}
tag de retorno;
}
public static void main (string [] args) {
System.out.println ("aaa");
String t = coderorDecoder ("aaa", cipher.encrypt_mode);
System.out.println (t);
System.out.println (coderorDecoder (t, cipher.decrypt_mode));
}
}