Foro de elhacker.net

Programación => Java => Mensaje iniciado por: nolasco281 en 1 Febrero 2016, 14:31 pm



Título: Como cifrar y descifrar con 3DES
Publicado por: nolasco281 en 1 Febrero 2016, 14:31 pm
Hola a todos.

Mi consulta es la siquiente estoy tratando de cifrar un archivo con 3DES(TripleDes) he encontrado ejemplos de como se hace esto con cadenas de texto y funcionan, pero me entro la duda que pasa si es un archivo el que deseo cifrar, no he encontrado ejemplos en la web a ecepcion de este:

Ya lo probe y parece hacer la encritacion bien pero al momento de desencritar me devuelve 0 talvez puedan ayudarme, u orientarme. Saludos.

Esta es la parte de cifrar.
Código
  1. public class Encryptor {
  2.    private static String inputFilePath = "D:/1.txt";
  3.    public static void main(String[] args) {
  4.        FileOutputStream fos = null;
  5.        File file = new File(inputFilePath);
  6.        String keyString = "140405PX_0.$88";
  7.        String algorithm = "DESede";
  8.        try {
  9.            FileInputStream fileInputStream = new FileInputStream(file);
  10.            byte[] fileByteArray = new byte[fileInputStream.available()];
  11.            fileInputStream.read(fileByteArray);
  12.            for (byte b : fileByteArray) {
  13.            System.out.println(b);
  14.            }
  15.            SecretKey secretKey = getKey(keyString);
  16.            Cipher cipher = Cipher.getInstance(algorithm);
  17.            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
  18.            ObjectOutputStream objectOutputStream = new ObjectOutputStream
  19.                    (new CipherOutputStream
  20.                            (new FileOutputStream
  21.                                    ("D:/Secret.file"), cipher));
  22.            objectOutputStream.writeObject(fileByteArray);
  23.            objectOutputStream.close();
  24.        } catch (Exception e) {
  25.            e.printStackTrace();
  26.        }
  27.    }
  28.    public static SecretKey getKey(String message) throws Exception {
  29.        String messageToUpperCase = message.toUpperCase();
  30.        byte[] digestOfPassword = messageToUpperCase.getBytes();
  31.        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
  32.        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
  33.        return key;
  34.    }
  35. }
  36.  

Esta es descifrar
Código
  1. public class Decryptor {
  2.    public static void main(String[] args) {
  3.        try {
  4.            File inputFileNAme = new File("d:/Secret.file");
  5.            FileInputStream fileInputStream = new FileInputStream(inputFileNAme);
  6.            FileOutputStream fileOutputStream = new FileOutputStream(outputFilePath);
  7.            SecretKey secretKey = getKey(keyString);
  8.            Cipher cipher = Cipher.getInstance(algorithm);
  9.            cipher.init(Cipher.DECRYPT_MODE, secretKey);
  10.            ObjectInputStream objectInputStream = new ObjectInputStream
  11.                    (new CipherInputStream(fileInputStream, cipher));
  12.            System.out.println(objectInputStream.available());
  13.            while (objectInputStream.available() != 0) {
  14.                fileOutputStream.write((Integer) objectInputStream.readObject());
  15.                System.out.println(objectInputStream.readObject());
  16.            }
  17.            fileOutputStream.flush();
  18.            fileOutputStream.close();
  19.            fileInputStream.close();
  20.            objectInputStream.close();
  21.        } catch (Exception e) {
  22.            e.printStackTrace();
  23.        }
  24.    }
  25.    public static SecretKey getKey(String message) throws Exception {
  26.        String messageToUpperCase = message.toUpperCase();
  27.        byte[] digestOfPassword = messageToUpperCase.getBytes();
  28.        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
  29.        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
  30.        return key;
  31.    }
  32. }
  33.