• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

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.
0
joedabomb
Asked:
joedabomb
  • 6
  • 4
  • 3
  • +1
2 Solutions
 
guynumber5764Commented:
Where are you stuck?
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
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;
}
0
Increase Security & Decrease Risk with NSPM Tools

Analyst firm, Enterprise Management Associates (EMA) reveals significant benefits to enterprises when using Network Security Policy Management (NSPM) solutions, while organizations without, experienced issues including non standard security policies and failed cloud migrations

 
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;
}
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
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.
0
 
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
0
 
guynumber5764Commented:
Good Work.

Use gets() instead of scanf() to read the line to encrypt.
0
 
Kent OlsenData Warehouse Architect / DBACommented:


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

0
 
guynumber5764Commented:
(grumble) "non-white-space"
0
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
guynumber5764Commented:
It keeps us paid :).
E.
0
 
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now