a continuacion pongo parte de las funciones que utilizo para encriptar:
/**
* Use the specified TripleDES key to encrypt bytes from the input stream
* and write them to the output stream. This method uses CipherOutputStream
* to perform the encryption and write bytes at the same time.
*/
public static void encrypt(SecretKey key, InputStream in, OutputStream out)
throws NoSuchAlgorithmException, InvalidKeyException,
NoSuchPaddingException, IOException {
// Create and initialize the encryption engine
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
// Create a special output stream to do the work for us
CipherOutputStream cos = new CipherOutputStream(out, cipher);
// Read from the input and write to the encrypting output stream
byte[] buffer = new byte[2048];
int bytesRead=0,cont=0;
while (( bytesRead = in.read(buffer)) != -1) {
cos.write(buffer, 0, bytesRead);
cont = cont + bytesRead;
}
System.out.println("Se han encriptado: "+cont+" bytes"+" bR: "+bytesRead);
cos.close();
// For extra security, don't leave any plaintext hanging around memory.
java.util.Arrays.fill(buffer, (byte) 0);
}
/**
* Use the specified TripleDES key to decrypt bytes ready from the input
* stream and write them to the output stream. This method uses uses Cipher
* directly to show how it can be done without CipherInputStream and
* CipherOutputStream.
*/
public static void decrypt(SecretKey key, InputStream in, OutputStream out)
throws NoSuchAlgorithmException, InvalidKeyException, IOException,
IllegalBlockSizeException, NoSuchPaddingException,
BadPaddingException {
// Create and initialize the decryption engine
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.DECRYPT_MODE, key);
// Read bytes, decrypt, and write them out.
byte[] buffer = new byte[2048];
int bytesRead,cont=0;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(cipher.update(buffer, 0, bytesRead));
cont = cont + bytesRead;
}
System.out.println("Se han desencriptado: "+cont+" bytes"+" bR: "+bytesRead);
// Write out the final bunch of decrypted bytes
out.write(cipher.doFinal());
out.flush();
}
y a continuacion pongo el codigo en donde resive mi aplicasion los datos en el servidor que es donde me esta ocasionando el conflicto:
public int RecibirArchivo(int resp){
try{
// Se generan las llaves para desencriptar la informacion
File keyfile = new File("C:/persona.txt");
SecretKey key = SR.generateKey();
SR.writeKey(key, keyfile);
NombreArchivo=le.LeerString(socket);
//Obtengo la longitud del archivo
long lon = le.LeerLong(socket);
//Obtengo el id de persona
id_persona=le.LeerString(socket);
//Se define la ruta donde va hacer espaldado el archivo
ruta = re.obtenRutaRespaldoArchivo(Integer.parseInt(id_pe rsona));
//Se crean dos flujos uno para leer la informacion atravez del socket
//el otro para escribir el archivo con la ruta ya especificada.
sIn =socket.getInputStream();
son = new FileOutputStream(ruta + NombreArchivo);
//Se comienza a desencriptar la informacion mandando los dos flujos donde
//se resive la informacio y donde va hacer almacenada
key = SR.readKey(keyfile);
SR.decrypt(key,sIn,son);
sIn.close(); //Aqui es donde me crea el problema al cerrar el socket y ya el cliente no se puede comunicar con el servidor.
//Si el parametro es igual a cero registra la transaccion como respaldo
//si no no se toma en cuenta el registro del respaldo
if (resp == 0){
//Se registra la transaccion del cliente que ha mandado la informacion
System.out.println("Registrando transaccion...");
bd.conexion();
bd.EjecutarSQL("INSERT INTO adm_movimientos(idm,id_persona,status,ruta,fecha)V ALUES('"+NombreArchivo+"',"+id_persona+",'S','"+ru ta+"', NOW() )");
bd.desconexion();
}
return 1;
}
catch(Exception e){
return 0;
}
}
en si es el codigo al cual no he podido encontrar solucion agradeceria su ayuda de ante mano
