Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Encrypting in C programming

Posted on 2003-10-29
17
Medium Priority
?
233 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:joedabomb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +1
17 Comments
 
LVL 3

Expert Comment

by:guynumber5764
ID: 9648396
Where are you stuck?
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 9650379

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
0
 

Author Comment

by:joedabomb
ID: 9651656
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;
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:joedabomb
ID: 9651658
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;
}
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 252 total points
ID: 9652046

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
0
 
LVL 3

Expert Comment

by:guynumber5764
ID: 9653238
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.
0
 

Author Comment

by:joedabomb
ID: 9696735
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
0
 
LVL 3

Assisted Solution

by:guynumber5764
guynumber5764 earned 248 total points
ID: 9696865
Good Work.

Use gets() instead of scanf() to read the line to encrypt.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 9696885


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
0
 
LVL 3

Expert Comment

by:guynumber5764
ID: 9696928
Kdo...  from the scanf() man page...
s      Matches a  sequence  of  non&#8208;white&#8208;space  characters....
                                                     ^^^^^^^^^^^^^^^

0
 
LVL 3

Expert Comment

by:guynumber5764
ID: 9696939
(grumble) "non-white-space"
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 9697345

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
0
 
LVL 3

Expert Comment

by:guynumber5764
ID: 9697467
It keeps us paid :).
E.
0
 
LVL 4

Expert Comment

by:void_main
ID: 9830281
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!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question