Hi!

I've tried this simple prog to encrypt a file using XOR

But it gives "Segmentation Fault " in 'interactive' mode (pls see the
source code)

it runs perfectly well in DOS (using Turbo C++ 3.0)

Can ny1 help pls ?

--
Nikhil Joshi


/* Program to Encrypt A file */

#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{

  void crypt ();
  char *arg[4];


  if (argc != 4)
 //if no arguments or wrong arguments then take i/p interactively
    {
      printf ("Please enter password\n");
      gets (arg[1]);
      printf ("Please enter the file to be encrypted/decrypted\n");
      gets (arg[2]);
      printf ("Please enter the output filename\n");
      gets (arg[3]);
      crypt (arg);
    }
  else
    crypt (argv);
  printf ("\n Success! \n");
  return 0;

}

void
crypt (char *cr_arg[])
{

  FILE *fi, *fo;
  char *cp;
  int c;

  if ((cp = cr_arg[1]) == NULL)
    {
      printf ("Invalid password\n");
      exit (1);
    }

  if ((fi = fopen (cr_arg[2], "rb")) == NULL)
    {
      perror ("Unable to open input file   ");
      exit (1);
    }

  if ((fo = fopen (cr_arg[3], "wb")) == NULL)
    {
      perror ("Unable to create output file   ");
      exit (1);
    }

  while ((c = getc (fi)) != EOF)
    {
      if (!*cp)

        cp = cr_arg[1];

      c ^= *(cp++); //Uses XOR algorithm

      putc (c, fo);

    }
  fclose (fo);
  fclose (fi);

  return;

}

Reply via email to