Encrypt files in Java

0

I'm trying to encrypt a file and I have the following code in java, but when I run it, I'm sorry, the code is as follows:

public class Prueba {

public static void main(String [] args)
   {

      String comando1 = "-c";
      String comando2 = "-d";

      //COMANDO 1 o COMANDO 2
      if ((comando1.equals(args[0]))||(comando2.equals(args[0]))){    
    //leer clave por teclado
    try{
         InputStreamReader leer_clave = new InputStreamReader(System.in);
         BufferedReader buff_clave = new BufferedReader(leer_clave);
         System.out.print("Escriba una clave: ");
         String clave = buff_clave.readLine();

         //pasar clave a la clase SecretKey
      try{
       SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
       DESKeySpec kspec = new DESKeySpec(clave.getBytes());
       SecretKey ks = skf.generateSecret(kspec);

      //Inicializar el cifrado
         try{
            Cipher cifrado = Cipher.getInstance("DES");

            //Escojo modo cifrado o descifrado segun sea el caso

            if (comando1.equals(args[0])){
               cifrado.init(Cipher.ENCRYPT_MODE, ks);}//MODO CIFRAR
            if (comando2.equals(args[0])){
               cifrado.init(Cipher.DECRYPT_MODE, ks);}//MODO DESCIFRAR


            //Leer fichero

            InputStream archivo = new FileInputStream( args[1] );
            OutputStream fich_out = new FileOutputStream ( args[2] );

            byte[] buffer = new byte[1024];
            byte[] bloque_cifrado;
            String textoCifrado = new String();
            int fin_archivo = -1;
            int leidos;//numero de bytes leidos

            leidos = archivo.read(buffer);

            while( leidos != fin_archivo ) {
               bloque_cifrado = cifrado.update(buffer,0,leidos);
               textoCifrado = textoCifrado + new String(bloque_cifrado,"ISO-8859-1"); 
               leidos = archivo.read(buffer);          
            }

            archivo.close();

            bloque_cifrado = cifrado.doFinal();
            textoCifrado = textoCifrado + new String(bloque_cifrado,"ISO-8859-1");
            //ISO-8859-1 es ISO-Latin-1

            fich_out.write(textoCifrado.getBytes("ISO-8859-1"));//escribir fichero

            }
            //Inicializacion de cifrado
            catch(javax.crypto.NoSuchPaddingException nspe) {} //Instanciacion DES
            catch(javax.crypto.IllegalBlockSizeException ibse) {}//metodo doFinal
            catch(javax.crypto.BadPaddingException bpe) {}//metodo doFinal
         }
         //pasar clave a la clase SecretKey
         catch(java.security.InvalidKeyException ike) {}
         catch(java.security.spec.InvalidKeySpecException ikse) {}
         catch(java.security.NoSuchAlgorithmException nsae) {}
         }
         //leer del teclado la clave como String
         catch(java.io.IOException ioex) {}
      }
   }

   }

The error that comes to me is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0     at tests.Prueba.main (Prueba.java:24)

Haber if you can help me.

    
asked by Daniel Ruiz 21.10.2017 в 03:01
source

1 answer

1

Your error is because you are not sending an argument when running the program

Keep in mind that your application is working with the argument that receives the main method so when you run your application you need to send that argument.

If you execute it from the command line you can send it in the following way

javac Prueba.java
java Prueba -c "esto es un argumento" "esto es otro argumento" esto son 4 argumentos

If you run it from an IDE (eclipse as a reference)

You need to right click > run as > run configurations > arguments and in the text area enter your arguments in the following way

-d "esto es un argumento" "esto es otro argumento" esto son 4 argumentos

In this way the exception will no longer be skipped, on the other hand you have many more errors and you should bear in mind that an object of type String [] is not a file. If you want to occupy files, use

File file = new File("C:/FicheroFoo.txt");

Now it's your turn to investigate how to read a file and how to create a file where you store your encrypted data

    
answered by 21.10.2017 в 06:56