Google Answers Logo
View Question
 
Q: simple C program ( Answered 4 out of 5 stars,   3 Comments )
Question  
Subject: simple C program
Category: Computers > Programming
Asked by: phelsva-ga
List Price: $16.00
Posted: 18 Mar 2003 21:08 PST
Expires: 17 Apr 2003 22:08 PDT
Question ID: 178038
Write a program that utilizes C pointers  and C-style strings to
perform text-based encryption/decryption
I work for AP systems a new company in New orleans, I have a big 
project due in a week, but I have no help, I have to write 4 different
programs in 3 diff languages, and make these programs very easy to 
explain to people who are just C begginers.  I have 2 very  big 
projects to do and I'm afraid I don't have time for the smaller ones,
the truth of the matter is, I'm very strong in Java 
but very week in C.Anyway, I need help in writing this program in c, 
I've already began the program, and would be most grateful if someone
can complete it for me, Ap systems may even decide to contact you 
personally for  future assignments and Big, Big bucks. 
 I have  written a code  that demonstrates an encryption  algorithm
using the C-standard  random-number generator. Note the presence of
encrypt /decrypt function (called  “vault”) and the key variable. I 
will like you  to look over  the program and make corrections if 
needed. I   will like you to change the ‘rand function’ into another 
1:1 and onto isomorphism. 
I NEED THIS PROGRAM BY TUESDAY MARCH 25 2003.


The main  help I need is as follows. Please  obtain a copy of the 

"Magna  Carta of  1215"

( from the library or the internet or anywhere)  enter the text
document into an Ascii text file. Then write a C program to read in
the text file, encrypt file using the encryption routine and print out
the encrypted contents.Then read the encrypted file back into your
program, decrypt it, and  print the decrypted contents.



# include <stdio.h>
# include <stdlib.h>
# include <stddef.h>
# include <math.h>
# include <string.h>


# define  SUCCESS   0 
# define FAILURE    -1

int vault ( char *, char , int);

int vault ( char *textstring, char mode, int key);

{
 char *end =NULL;
int i=0, length=0, returncode=SUCCESS;
length =strlen(textstring);

end= textstring + length;

if(mode = = ‘E’)
      printf(“Encrypting string contents.\n”);

else
if(mode = = ‘D’)
 printf (“Decrpting string contents. \n”);

else 
{
printf(“ invalid encryption mode value: use D=decrypt, E=encrypt.\n”);
 printf(“ Function is returning with no action. \n”);
returncode=FAILURE;
}

if (mode= = ‘E’ || mode= = ‘D’)
{  srand(key);

    while (tectstring < end)
{          i= rand( );

     if mode= = ‘E’ )
  
  *textstring = *textstring +  i;
            
 else
  *textstring = *textstring -  i;

  *textstring+ +;
}
}
return  returncode;
}
Answer  
Subject: Re: simple C program
Answered By: hammer-ga on 19 Mar 2003 07:11 PST
Rated:4 out of 5 stars
 
/*
CRYPT - encrypts/decrypts a file using XOR
Outputs results to specified file
Assumes Win32 platform

Usage: crypt infile outfile keystring
Arguments:
infile - file to encrypt/decrypt
outfile - file where result will be written
keystring - encryption key. should be a string of random
    characters with no spaces

Example:
crypt infile.txt outfile.txt jjue7kfkkd88dek30
crypt outfile.txt outfile2.txt jjue7kfkkd88dek30

After running both the above commands, outfile2.txt 
should be identical to infile.txt. Note that both
commands use the same keystring.

*/

#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>

void main(int argc, void **argv)
{
void  vault(char *, char *, int);
char  *ptr;
int  fd;
int  fd2;
int  f_len;

  if(argc > 1)
  {
    if(argc > 2)
    {
      if(argc > 3)
      {
        if(access(argv[1], 0) == 0)
        {
          fd = open(argv[1], O_RDONLY | O_BINARY);
          if(fd > -1)
          {
            f_len = filelength(fd);  
            if(f_len > 0)
            {
              ptr = (char *)malloc(f_len + 1);
              if(ptr != NULL)
              {
                if(read(fd, ptr, f_len) == f_len)
                {
                  *(ptr + f_len) = '\0';
                  vault(ptr, argv[3], f_len);
                  fd2 = open(argv[2], O_WRONLY | O_CREAT | O_BINARY,
_S_IREAD | _S_IWRITE);
                  if(fd2 > -1)
                  {
                    write(fd2, ptr, f_len);
                    close(fd2);
                  }
                  else
                  {
                    fprintf(stderr, "Error:  Output file failed to
open.\n");
                  }
                }
                else
                {
                  fprintf(stderr, "Error:  Input file failed to
read.\n");
                }
                free(ptr);
              }
              else
              {
                fprintf(stderr, "Error:  Failed to malloc ptr.\n");
              }
            }
            else
            {
              fprintf(stderr, "Error:  Input file has no length.\n");
            }
            close(fd);
          }
          else
          {
            fprintf(stderr, "Error:  Input file failed to open.\n");
          }
        }
        else
        {
          fprintf(stderr, "Error:  Input file does not exist.\n");
        }
      }
      else
      {
        fprintf(stderr, "Error:  Key string must be specified.\n");
      }
    }
    else
    {
      fprintf(stderr, "Error: Output filename must be specified.\n");
    }
  }
  else
  {
    fprintf(stderr, "Error: Filename to encrypt/decrypt must be
specified.\n");
  }
}

void  vault(char *val, char *key, int val_len)
{
int  r;
char  *cp_val;
char  *cp_key;
char  result;

  r = 0;
  if((val != NULL) && (key != NULL))
  {
    cp_val = val;
    cp_key = key;
    while(r < val_len)
    {
      result = *cp_val ^ *cp_key;
      *cp_val = result;
      cp_val++;
      cp_key++;
      if(*cp_key == '\0') cp_key = key;
      r++;
    }
  }
}

/*
I was able to successfully encrypt and decrypt the Magna Carta using
this program.

Good luck with your project.

- Hammer
*/

Request for Answer Clarification by phelsva-ga on 24 Mar 2003 21:29 PST
thanks hammer, I need some futher clarification.

I need the API source code(header file, function library, and main
driver), you've already provided that.

But I also need a printout of the encrypted  file and a printout of
the decrypted file. You can cut and paste these, but I need to see
them

Request for Answer Clarification by phelsva-ga on 24 Mar 2003 21:30 PST
I need these by wed  march 26th.

Clarification of Answer by hammer-ga on 25 Mar 2003 04:40 PST
The crypted file is not going to cut and paste happily. I have placed
a zip file for you to download which contains the original file
(magna.txt), the encrypted file (magna_crypt.txt) and the decrypted
file (magna_decrypt.txt). The key string used was 12345.

Download File:
http://www.hammerdata.com/Google/crypt.zip

- Hammer
phelsva-ga rated this answer:4 out of 5 stars
Good job

Comments  
Subject: Re: simple C program
From: karlon-ga on 19 Mar 2003 00:08 PST
 
Take a look at http://www.cox-internet.com/ksetal/crypt.c and tell me
what you think. 

example of use:
linux$ gcc -o crypt crypt.c
linux$ ./crypt 
Enter input filename: magnacart.txt         
Enter output filename: magnacart.cry
Enter passphrase: GoogleAnswers
Enter E for encrypt, D for decrypt: E
Encrypting string contents.
Encrypting string contents.
Encrypting string contents.
Encrypting string contents.
Encrypting string contents.
Encrypting string contents.
Encrypting string contents.
Encrypting string contents.

linux$ ./crypt 
Enter input filename: magnacart.cry
Enter output filename: magnacart.clear
Enter passphrase: GoogleAnswers
Enter E for encrypt, D for decrypt: D
Decrpting string contents. 
Decrpting string contents. 
Decrpting string contents. 
Decrpting string contents. 
Decrpting string contents. 
Decrpting string contents. 
Decrpting string contents. 
Decrpting string contents. 

# now, make sure they decrypted text is the same as the original

linux$ sum magnacart.txt magnacart.clear 
31287     2 magnacart.txt
31287     2 magnacart.clear
Subject: Re: simple C program
From: phelsva-ga on 28 Mar 2003 07:05 PST
 
were you able to succesfully encrypt and decrypt the magna carta
Subject: Re: simple C program
From: bmcompany-ga on 07 Feb 2004 12:58 PST
 
Im really sorry if i'm sticking my nose in but why, when you offered
such a low $ amount for such a specialised job, did you only rate 4/5
when (as far as i can see) it did exactly what you asked for?

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy