Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program name encrypt - encrypt or decrypt a file using DES encryption Usage encr

ID: 3888479 • Letter: P

Question

Program name

encrypt - encrypt or decrypt a file using DES encryption

Usage

encrypt <key> <input file> <output file> <mode>

key: an 8 character string used as the DES key to encrypt/decrypt

input file: the file to encrypt/decrypt

output file: the result of the operation

mode: specifies whether to encrypt or decrypt- if mode=0, then encrypt the input file, if mode=1 then decrypt the input file

CAN YOU PLEASE HELP CREATE A PROGRAM THAT READS A FILE AND ENCRYPT AND DECRYPTS AND WRITES TO A NEW FILE USING THE READ() AND WRITE() FUNCTIONS WITH THE HELP OF THE LINUX ECB_CRYPT() FUNCTION. THE PROGRAM SHOULD BE COMPILED USING THE ./ENCRYPT(KEY, INPUT FILE, OUTPUT FILE, MODE OF 0 OR 1 FOR ENCRYPTION)

C PROGRAM

Explanation / Answer

#include<fcntl.h>
#include<sys/types.h>
#include<stdio.h>
#include<rpc/des_crypt.h>
#include<stdlib.h>

char key[9];        /* to store 8-characters string */
char data[1024];    /* to store the encrypted/decrypted data - this data must be fetched from an input file */
unsigned datalen;   /* length of the encrypted/decrypted data */
short int mode;     /* 0 - encryption, 1 - decryption */

int fd;             /* file descriptor - which will be used to read an input file either encrypted or decrypted file */
int ofd;            /* ofd - output file desciptor - to store either decrypted data or encrypted data */

int error_flag=0;
int n;              /* To store no. of bytes fetched from the input file */

void usage()
{
printf("Usage : ");
printf("encrypt <key> <inp_file> <out_file> <0|1> ");
printf(" ");
printf("For example to encrypt a file : ");
printf("encrypt helloxyz stu.dat stu_encrypt 0 ");
printf("For example to decrypt a file : ");
printf("encrypt helloxyz stu_encrypt stu_decrypt 1 ");
}

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

   if(argc!=5)
    {
       printf("Invalid no. of Arugments ... ");
       usage();
    }

   if( strlen(argv[1])!=8) /* key length is not matching */
    {
       printf("Key should contain only 8 characters");
       error_flag=1;
    }

   if ( ( fd=open(argv[2],O_RDONLY) ) == -1 )
    {
       printf("Error opening input file... ");
       error_flag=2;
    }

   if( (ofd=open(argv[3],O_WRONLY | O_CREAT | O_TRUNC, 0644))==-1)
    {

      printf("Error opening output file ... ");
      error_flag=3;
    }

   if ( strlen(argv[4])!=1 )
    {
      printf("mode value should be either 0 (Encrypt) or 1 (Decrypt) ");
      error_flag=4;
    }
   else
    {
      mode=atoi(argv[4]);
      if( !(mode==0||mode==1) )
       {
       {
          printf("mode value should be either 0 (Encrypt) or 1 (Decrypt) ");
          error_flag=5;
       }
    }

   if(error_flag!=0)
    {
      close(fd); /* closing input file descriptor */
      close(ofd); /* closing output file descriptor */
      exit(error_flag);
    }

    /*fetch data from input file */

    n=read( fd,(void *)data,1024);
    data[n]='';

    switch(mode)
     {
        case 0:
                 ecb_crypt(key,data,n,0);
                 break;
        case 1:
                 ecb_crypt(key,data,n,1);
                 break;
     }

     write(ofd,(void*)data, n);
     close(ofd);
     close(fd);
} /* End of main */

Save the above program as encrypt.c using vi editor or gedit

compile the above program as

cc encrypt.c -o encypt

the compiler shows warning messaage, please ignore it.

now execute your program as

./encrypt helloxyz stu.dat stu_encrypt 0