Solved

Encrypting in C programming

Posted on 2003-10-29
17
224 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
  • 6
  • 4
  • 3
  • +1
17 Comments
 
LVL 3

Expert Comment

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

Expert Comment

by:Kdo
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
 

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 45

Accepted Solution

by:
Kdo earned 63 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 3

Assisted Solution

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

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

Expert Comment

by:Kdo
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 45

Expert Comment

by:Kdo
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now