Encryption and Decryption in C

I have to write two funcs, one to encrypt and one to decrypt. I have s

Page 1 of 1

1 Replies - 5600 Views - Last Post: 01 October 2009 - 10:12 AM Rate Topic: -----

#1 spc21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 04-February 09

Encryption and Decryption in C

Posted 30 September 2009 - 04:46 PM

I have this lab here and I'm just not sure where to go with it from here and it's becoming a little nerve wracking because my teacher doesn't actually teach, nor did he have us buy a book so I have nothing to go off of. By the way this is just a lab not homework, so no points or grades are included. I just need some direction here, since I am receiving absolutely nothing from my professor.

here's the code I have so far

Exercise 4. Crytography1
Write a program that encrypts and decrypts a text entered by the user following these
 Asks the user to enter a text with alphanumeric characters (A-Z, a-z, 0-9), no spaces
or any other character is allowed
 Check that only alphanumeric characters have been entered. If not, print an error and
1 Please read below some definitions (encryption, decryption) if you are not familiar at all with
 If the text contains only alphanumeric characters then call a function that encrypts the
text2. Print the encrypted text.
 Call a function that decrypts the text produced in the previous step. Print the
decrypted text and check (visually) that the decrypted text produced by your function
matches the text entered by the user.
The method you are going to use is called the Caesar cipher3 with a shift of 3 characters.
A couple of examples are given below (assumes that your executable is called encdec):
$ ./encdec
Enter the text: abcxyz01789
Encrypted text: defabc34012
Decrypted text: abcxyz01789
$ ./encdec
Enter the text: JOSEPH
Encrypted text: MRVHSK
Decrypted text: JOSEPH
You can use the program in Listing 2 as a starting point (Attached).
Listing 2

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_CHARS 20
const char base_Upper = 'A';
const char base_Lower = 'a';
const char base_Digit = '0';
int mod(int a, int n)
int modulus = a % n;
if (modulus < 0)
modulus = n + modulus;
return modulus;
int validate_entry(char *text_to_validate)
int j;
/* you may replace this with something more efficient
if you like */
for (j=0; j<strlen(text_to_validate); j++)
if ( !isalnum(text_to_validate[j]) )
2 You are not allowed to use global arrays
3 Explained below
return (j+1);
return 0;
write your encryption & decryption functions here
int main()
char entry[MAX_CHARS];
char enc_entry[MAX_CHARS];
char dec_entry[MAX_CHARS];
printf("Enter the text: ");
/* Warning: we are using 'gets' here for simplicity,
but in general you should avoid this function */
int result = validate_entry(entry);
if (!result) /* if (result == 0) */
call your enc/dec functions here
printf("Invalid text entered.");
printf("Illegal character at position: %d\n", (result-1));
return 0;

You need to know this to solve the exercise:
The behavior of the modulus operator (%) in C is undefined for negative results. Use the function mod
provided to you instead of %
int modulus1 = -3 % 26; /* result is implementation-specific */
int modulus2 = mod(-3, 26); /* gives the correct result. See Listing 2 */
These ASCII codes are important:
65 = 'A'
97 = 'a'
48 = '0'
You must take into consideration the above codes before applying a modulus for the Caesar cipher
There is a correspondence between chars and ints in C. For example:
int upperA = 'A'; /* upperA == 65 */
int B = 'A' + 1; /* B == 66 */
printf("%d %c", B, B); /* prints: 66 B */
int modA = 'A' % 64; /* modA == 1 */
The following functions are declared in ctype.h (you can pass them a char instead of an int as
int isalpha(int c); // returns 1 if c is the code of a character A-Z or a-z
int isupper(int c); // returns 1 if c is the code of a letter in uppercase
int islower(int c); // returns 1 if c is the code of a letter in lowercase
int isdigit(int c); // function tests for any decimal-digit character: 0-1
int isalnum(int c); // returns 1 if c is the code of a character A-Z or a-z,0-1
There are 26 letters in the alphabet, but only 10 numbers, so you have to take that detail into consideration.

he gave us additional stuff about Ceasar cryptography, but it hasn't helped at all.

I was thinking that maybe I could do something with the fact that I could translate the ASCII characters into numeric values and then add three and print them out as their corresponding ASCII characters(to encrypt) then reverse that to decrypt if again. I just don't know how to do this.

Is This A Good Question/Topic? 0
  • +

Replies To: Encryption and Decryption in C

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Encryption and Decryption in C

Posted 01 October 2009 - 10:12 AM

Something like this?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1