joedabomb
asked on
Encrypting in C programming
I need to write a program in c that will substitute non-alphanumeric and non-punctuation encryption characters for vowels.
It needs to prompt the user to type in a sequence of 10 encryption characters to be used, a different one for each upper and lower case vowel (aeiouAEIOU). Assignment to a vowel is by the order they are typed. Display a user prompt to type a one-line message, ending with the Return or Enter key.
As the message is typed and encrypted, write the encrypted message to a data file. Then, read the encrypted data file and display it on the next line. Then, read the encrypted data file and write the decrypted message to a different (from the previous) decrypted data file.
Finally, read the decrypted data file and display it on a new line just below the encrypted line.
It needs to prompt the user to type in a sequence of 10 encryption characters to be used, a different one for each upper and lower case vowel (aeiouAEIOU). Assignment to a vowel is by the order they are typed. Display a user prompt to type a one-line message, ending with the Return or Enter key.
As the message is typed and encrypted, write the encrypted message to a data file. Then, read the encrypted data file and display it on the next line. Then, read the encrypted data file and write the decrypted message to a different (from the previous) decrypted data file.
Finally, read the decrypted data file and display it on a new line just below the encrypted line.
Where are you stuck?
Hi joedabomb,
This is obviously a homework assignment and the rules around here prohibit us from doing your (or anybody else's) homework. We can, however, give you lots of guidance and advice.
If you'll tell us where you're stuck, or post what you've written to date, we can help you "take the next step".
Kent
ASKER
Ok guys here is what I have so far. I'm not sure if I'm doing this right or what my next step is. Please help.
Thanx alot.
#include "stdafx.h"
#include <stdio.h>
int main()
{
printf("Enter text: \n");
{
char ch;
char a,e,i,o,u,A,E,I,O,U;
char message[40];
printf ("Type 10 encryption characters to be used (any nonalphanumeric or non-punctuation characters): ");
// scanf ("%c", &ch);
scanf ("%c%c%c%c%c%c%c%c%c%c", &a,&e,&i,&o,&u,&A,&E,&I,&O ,&U);
printf ("\nType a one-line message, ending with a Return or Enter key: ");
scanf ("%s", &message);
switch(ch){
case 97:
printf ("%c",A);
break;
case 101:
printf ("%c",E);
break;
case 105:
printf ("%c",I);
break;
case 111:
printf ("%c",O);
break;
case 117:
printf ("%c",U);
break;
case 65:
printf ("%c",a);
break;
case 69:
printf ("%c",e);
break;
case 73:
printf ("%c",i);
break;
case 79:
printf ("%c",o);
break;
case 85:
printf ("%c",u);
break;
}
printf ("Encrypted Message:\n %s", message);
}
return 0;
}
Thanx alot.
#include "stdafx.h"
#include <stdio.h>
int main()
{
printf("Enter text: \n");
{
char ch;
char a,e,i,o,u,A,E,I,O,U;
char message[40];
printf ("Type 10 encryption characters to be used (any nonalphanumeric or non-punctuation characters): ");
// scanf ("%c", &ch);
scanf ("%c%c%c%c%c%c%c%c%c%c", &a,&e,&i,&o,&u,&A,&E,&I,&O
printf ("\nType a one-line message, ending with a Return or Enter key: ");
scanf ("%s", &message);
switch(ch){
case 97:
printf ("%c",A);
break;
case 101:
printf ("%c",E);
break;
case 105:
printf ("%c",I);
break;
case 111:
printf ("%c",O);
break;
case 117:
printf ("%c",U);
break;
case 65:
printf ("%c",a);
break;
case 69:
printf ("%c",e);
break;
case 73:
printf ("%c",i);
break;
case 79:
printf ("%c",o);
break;
case 85:
printf ("%c",u);
break;
}
printf ("Encrypted Message:\n %s", message);
}
return 0;
}
ASKER
Ok guys here is what I have so far. I'm not sure if I'm doing this right or what my next step is. Please help.
Thanx alot.
#include "stdafx.h"
#include <stdio.h>
int main()
{
printf("Enter text: \n");
{
char ch;
char a,e,i,o,u,A,E,I,O,U;
char message[40];
printf ("Type 10 encryption characters to be used (any nonalphanumeric or non-punctuation characters): ");
// scanf ("%c", &ch);
scanf ("%c%c%c%c%c%c%c%c%c%c", &a,&e,&i,&o,&u,&A,&E,&I,&O ,&U);
printf ("\nType a one-line message, ending with a Return or Enter key: ");
scanf ("%s", &message);
switch(ch){
case 97:
printf ("%c",A);
break;
case 101:
printf ("%c",E);
break;
case 105:
printf ("%c",I);
break;
case 111:
printf ("%c",O);
break;
case 117:
printf ("%c",U);
break;
case 65:
printf ("%c",a);
break;
case 69:
printf ("%c",e);
break;
case 73:
printf ("%c",i);
break;
case 79:
printf ("%c",o);
break;
case 85:
printf ("%c",u);
break;
}
printf ("Encrypted Message:\n %s", message);
}
return 0;
}
Thanx alot.
#include "stdafx.h"
#include <stdio.h>
int main()
{
printf("Enter text: \n");
{
char ch;
char a,e,i,o,u,A,E,I,O,U;
char message[40];
printf ("Type 10 encryption characters to be used (any nonalphanumeric or non-punctuation characters): ");
// scanf ("%c", &ch);
scanf ("%c%c%c%c%c%c%c%c%c%c", &a,&e,&i,&o,&u,&A,&E,&I,&O
printf ("\nType a one-line message, ending with a Return or Enter key: ");
scanf ("%s", &message);
switch(ch){
case 97:
printf ("%c",A);
break;
case 101:
printf ("%c",E);
break;
case 105:
printf ("%c",I);
break;
case 111:
printf ("%c",O);
break;
case 117:
printf ("%c",U);
break;
case 65:
printf ("%c",a);
break;
case 69:
printf ("%c",e);
break;
case 73:
printf ("%c",i);
break;
case 79:
printf ("%c",o);
break;
case 85:
printf ("%c",u);
break;
}
printf ("Encrypted Message:\n %s", message);
}
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just keep plugging away. You are doing fine so far...
You are collecting both inputs,
You are printing the 1st part of your output.
You've got a general idea how your encoder is going to look.
Obviously the next thing (as Kent suggests) is to make the encoder actually encode then start on the decoder.
I would ignore the "write to file" part for now..
tip: A "switch" statement will not work for the decoder. Care to guess why?
E.
You are collecting both inputs,
You are printing the 1st part of your output.
You've got a general idea how your encoder is going to look.
Obviously the next thing (as Kent suggests) is to make the encoder actually encode then start on the decoder.
I would ignore the "write to file" part for now..
tip: A "switch" statement will not work for the decoder. Care to guess why?
E.
ASKER
Hey guys
I have the Encoder pretty much done but how do I make the string read white spaces and the stop when it gets to newline?
thanx
joedabomb
I have the Encoder pretty much done but how do I make the string read white spaces and the stop when it gets to newline?
thanx
joedabomb
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
scanf() and fscanf() should just read a line at a time so every time you call one of these functions just encode the returned value. scanf() and fscanf() will NOT return control back to the program until newline is pressed. Is this the effect you're looking for?
What is it you want to do with the whitespaces?
Kent
Kdo... from the scanf() man page...
s Matches a sequence of non‐white‐spac e characters....
^^^^^^^^^^^^^^^
s Matches a sequence of non‐white‐spac
^^^^^^^^^^^^^^^
(grumble) "non-white-space"
Sorry. Brain cramp.
[soapbox]
I wish that I could put my hands on the current generation of C instructors that are teaching that scanf() should be used for general input. We see a LOT of it here on the boards. Scanf() and fscanf() have specific purposes and are NOT general purpose I/O routines. gets(), fgets(), getc(), getch(), getchar(), etc are the family of APIs that should be used for most input.
period.
[/soapbox]
sigh....
Kent
It keeps us paid :).
E.
E.
Do this:
// Pseudo code
void encrypt(char *in, char *pass, char *out)
{
int i;
int lop = strlen(pass);
for (i = 0; i < strlen(in); i++)
out[i] = in[i] ^ pass[i % lop];
}
char *originalText = malloc(21); // the same with char *pass and char *out
scanf("%20s", originalText);
fflush(stdin);
scanf("%20s", pass);
fflush(stdin);
encrypt(originalText, pass, out);
printf(out);
// to decrypt call encrypt again!
// Pseudo code
void encrypt(char *in, char *pass, char *out)
{
int i;
int lop = strlen(pass);
for (i = 0; i < strlen(in); i++)
out[i] = in[i] ^ pass[i % lop];
}
char *originalText = malloc(21); // the same with char *pass and char *out
scanf("%20s", originalText);
fflush(stdin);
scanf("%20s", pass);
fflush(stdin);
encrypt(originalText, pass, out);
printf(out);
// to decrypt call encrypt again!