• C

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.
joedabombAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

guynumber5764Commented:
Where are you stuck?
Kent OlsenDBACommented:

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
joedabombAuthor Commented:
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;
}
SolarWinds® VoIP and Network Quality Manager(VNQM)

WAN and VoIP monitoring tools that can help with troubleshooting via an intuitive web interface. Review quality of service data, including jitter, latency, packet loss, and MOS. Troubleshoot call performance and correlate call issues with WAN performance for Cisco and Avaya calls

joedabombAuthor Commented:
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;
}
Kent OlsenDBACommented:

You seem to have most of it.  What's missing is the actual substitution of each character.  Try replacing your switch() statement with this:

for (idx = 0; message[idx]; idx++)
  switch (message[idx])
  {
    case 'a':
      putchar (a);
      break;
    case 'A':
      putchar (A);
      break;
....  /*  insert similar code for the other vowels  */
    case 'U':
      putchar (U);
      break;
    default:
      putchar (message[idx]);
  }


Kent

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
guynumber5764Commented:
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.
joedabombAuthor Commented:
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
guynumber5764Commented:
Good Work.

Use gets() instead of scanf() to read the line to encrypt.
Kent OlsenDBACommented:


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
guynumber5764Commented:
Kdo...  from the scanf() man page...
s      Matches a  sequence  of  non&#8208;white&#8208;space  characters....
                                                     ^^^^^^^^^^^^^^^

guynumber5764Commented:
(grumble) "non-white-space"
Kent OlsenDBACommented:

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
guynumber5764Commented:
It keeps us paid :).
E.
void_mainCommented:
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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.