simple C program
-
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:
/* 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 */
phelsva-ga at Google Answers Visit the source
Related Q & A:
- How Can I use .net dll in C program?Best solution by Stack Overflow
- How to run an executable from within a c program?Best solution by Stack Overflow
- How to create a simple counter program?Best solution by Stack Overflow
- Codes for a simple visual basic program.Best solution by vbtutor.net
- Can anyone help me with this C++ program?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.