Foro de elhacker.net

Programación => Java => Mensaje iniciado por: Luffy97 en 13 Octubre 2016, 12:18 pm



Título: Ejercicio Java
Publicado por: Luffy97 en 13 Octubre 2016, 12:18 pm
Buenas, tengo que realizar un ejercicio en java. El ejercicio consiste en cifrar un documento binario con el mètodo XOR. Yo tengo echo el codigo pero solo me funciona en extensión .txt aquí lo adjunto.
Código
  1. package xor;
  2.  
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.util.Scanner;
  6.  
  7. public class XOR {
  8.  
  9.    public static void main(String[] args) {
  10.        try
  11.        {
  12.            System.out.println("Introdueix el nom de l'arxiu: ");
  13.            String nomArxiu = new Scanner(System.in).nextLine();
  14.            System.out.println("Introdueix el nom de l'arxiu encriptat: ");
  15.            String nomArxiu2 = new Scanner(System.in).nextLine();
  16.            System.out.println("Introdueix la contrasenya de encriptació: ");
  17.            String password = new Scanner(System.in).nextLine();
  18.            int tamaño = password.length();
  19.            int cont = 0;
  20.            if (!nomArxiu.equals(nomArxiu2))
  21.            {
  22.                FileReader fr = new FileReader("arxius\\" + nomArxiu);
  23.                FileWriter fw = new FileWriter("arxius\\" + nomArxiu2);
  24.                int caracter = fr.read();
  25.                while(caracter != -1)
  26.                {
  27.                    int encrip = caracter ^ password.charAt(cont); //cifra la letra
  28.                    fw.write(encrip); //escribe la letra en el fichero
  29.                    caracter = fr.read(); //lee el siguiente caracter
  30.                 if (cont  < tamaño - 1)
  31.                 {
  32.                     cont++;
  33.                 }
  34.                 else
  35.                 {
  36.                     cont = 0;
  37.                 }
  38.                }
  39.                fr.close();
  40.                fw.close();
  41.            }
  42.        }
  43.        catch (Exception e)
  44.        {
  45.            System.out.println("e.toString");
  46.        }
  47.    }
  48.  
  49. }
  50.  

Gracias. Un saludo.


Título: Re: Ejercicio Java
Publicado por: oldaccount en 14 Octubre 2016, 17:03 pm
Hola Luffy97.

Esta es mi solución al problema:

Código
  1. import java.nio.file.Files;
  2. import java.nio.file.Path;
  3. import java.nio.file.Paths;
  4.  
  5. public class XOR {
  6. public static void main(String[] args) throws Exception {
  7. Path source, dest;
  8. byte[] key;
  9. byte[] buffer;
  10.  
  11. if(args.length != 3) {
  12. System.out.println("Usage: XOR SOURCE DEST KEY");
  13. System.out.println("Encrypt SOURCE into DEST using XOR algorithm with KEY");
  14. System.exit(0);
  15. }
  16.  
  17. source = Paths.get(args[0]);
  18. dest = Paths.get(args[1]);
  19. key = args[2].getBytes();
  20. buffer = Files.readAllBytes(source);
  21.  
  22. for(int i = 0; i < buffer.length; i++) {
  23. buffer[i] =  (byte) (buffer[i] ^ key[i % key.length]);
  24. }
  25.  
  26. Files.write(dest, buffer);
  27. }
  28. }

Saludos.