La copia del código es la siguiente:
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.secureurandom;
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;
clase pública Destool {
static final static privado passkey = "Afasdf";
String final estática privada deskey = "asfsdfsdf";
/**
* @Comments: cifre el archivo
* @param filepath filepath para encriptar
* @param archivo de nombre de archivo
* @param Modo Cifrado en modo Cifrado: cipher.encrypt_mode descryption: cipher.decrypt_mode
* @devolver
*/
public static String EncoderOrDeCoder (String FilePath, String FileName, Int Mode) {
InputStream es = nulo;
OutputStream out = null;
Cipherinputstream cis = null;
intentar {
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 ());
Cifrado cifrado = cipher.getInstance ("des/cbc/pkcs5padding");
cifrado.init (modo, securekey, iv, sr);
Archivo encoderfile = nuevo archivo (filepath + file.separator + "codificador");
if (! EncoderFile.Exists ()) {
encoderfile.mkdir ();
}
IS = new FileInputStream (FilePath + File.separator + FileName);
Out = nuevo FileOutputStream (FilePath + File.SeParator + "Coder"
+ File.separator + nombre de archivo);
cis = nuevo cipherinputstream (is, cifrado);
byte [] buffer = new Byte [1024];
int r;
while ((r = cis.read (buffer))> 0) {
out.write (buffer, 0, r);
}
} capt (excepción e) {
E.PrintStackTrace ();
} finalmente {
intentar {
if (is! = null) {
is.close ();
}
if (cis! = null) {
cis.close ();
}
if (out! = null) {
out.close ();
}
} capt (excepción e1) {
}
}
Devuelve FilePath + File.separator + "codificador" + file.separator
+ nombre de archivo;
}
/**@Comentarios: cadenas en cifrado
* @param SRC String String
* @param Modo Cifrado en modo Cifrado: cipher.encrypt_mode descryption: cipher.decrypt_mode
* @devolver
*/
public static String coderordEcoder (String src, ints Mode) {
String tag = "";
InputStream es = nulo;
OutputStream out = null;
Cipherinputstream cis = null;
intentar {
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 ());
Cifrado cifrado = cipher.getInstance ("des/cbc/pkcs5padding");
cifrado.init (modo, securekey, iv, sr);
cis = nuevo CipherInputStream (nuevo bytearrayInputStream (src.getBytes ()), cifrado);
out = new byteArRaReOutputStream ();
byte [] buffer = new Byte [1024];
int r;
while ((r = cis.read (buffer))> 0) {
out.write (buffer, 0, r);
}
tag = out.ToString ();
} capt (excepción e) {
E.PrintStackTrace ();
} finalmente {
intentar {
if (is! = null) {
is.close ();
}
if (cis! = null) {
cis.close ();
}
if (out! = null) {
out.close ();
}
} capt (excepción e1) {
}
}
Etiqueta de retorno;
}
public static void main (string [] args) {
System.out.println ("AAA");
Cadena t = encoderordeCoder ("aaa", cipher.encrypt_mode);
System.out.println (t);
System.out.println (coderordeCoder (t, cipher.decrypt_mode));
}
}