Key Mismatch -
Matching Key -
Code -
...... Encryption Decryption in C (logic told in another post) ........
//___ Program for Encryption and Decryption in C ___
/* Program will read the message and an encryption key (which should be of only 3 digits) and based on that key
data will be encrypted and for decryption the program will again read a number and if it is same as that of the number being inputted the first time
original message can be obtained */
/* Encrypted message in not printed but you can edit the code to watch the encrypted message */
//____ Including Header Files ___
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
//__ Included for fflush(stdin) ___
char original_message[100];
int temp_variable = 0;
int encryption_message_key_scan();
int decryption_message_key_scan();
void message_encryption(char *original_message, int temp_key);
//___ Function to encrypt the message ___
void message_decryption(char *original_message, int temp_key);
//___ Function to decrypt the message ___
int main()
//___ Main function from where the program execution starts ___
{
int temp = 0, counter = 0;
printf("\n\n\t\t__________ Encryption/Decryption in C _________\n\n");
printf("\n\n 1. Encryption will take place\n\n 2. Decryption will take place");
printf("\n\n\t Press Enter to Continue.....");
printf("\n\n________________________________________________________________________________");
while(getch() != 13)
{
}
fflush(stdin);
printf("\n\n Encryption - ");
temp = encryption_message_key_scan();
message_encryption(original_message, temp++);
printf("\n\n________________________________________________________________________________");
printf("\n\n Decryption - ");
temp = decryption_message_key_scan();
message_decryption(original_message, temp);
printf("\n\n Your Message - ");
puts(original_message);
getch();
}
/* Function to scan the message and the encryption key based on which message will be encrypted */
int encryption_message_key_scan()
{
int encryption_key = 0;
printf("\n\n Enter the Message - ");
gets(original_message);
fflush(stdin);
printf("\n\n Enter the Number - "); //___ Prompting the user to add the key ___
scanf("%d",&encryption_key);
//__ Reading the three digit encryption key __
return encryption_key;
}
/* Function to scan the decryption key and the function return type is of integer i.e. it returns the decryption
key where the function is being called */
int decryption_message_key_scan()
{
int decryption_key = 0;
fflush(stdin);
printf("\n\n Enter the Number - "); //___ Prompting the user to add the key ___
scanf("%d",&decryption_key);
//__ Reading the three digit encryption key __
return decryption_key;
}
void message_encryption(char *original_message, int temp_key)
{
int encryption_key = 0, remainder = 0, first_digit = 0, last_digit = 0, string_length = 0, counter = 0;
char temp_character;
/* Counter in the while loop represents the place value
(0 place represents the ones/2 represents 100 place value) */
while(temp_key != 0)
{
remainder = temp_key % 10;
temp_key = temp_key / 10;
if(counter == 0 )
{
last_digit = remainder;
}
else if ( counter == 2)
{
first_digit = remainder;
}
counter++;
}
if (counter == 3)
//___ if the number is of 3 digits then ___
{
string_length = 0;
//___ String_length is used to count the number of characters in message ___
while(original_message[string_length] != '\0')
//___ Counting the number of characters in the message__
{
string_length++;
}
if (string_length%2 != 0)
//__ According the algorithm if message length is odd, make it even ___
{
original_message[string_length] = 32;
original_message[string_length + 1] = '\0';
string_length++;
}
//___ Replacing the characters at the initial positions _____
temp_character = original_message[first_digit - 1];
original_message[first_digit - 1] = original_message[last_digit - 1];
original_message[last_digit - 1] = temp_character;
while((string_length >= first_digit * 2) && (string_length >= last_digit * 2))
/* The loop will continue till any of the condition is false i.e. if any of the digit is becomes greater
than message length loop will stop */
{
first_digit = first_digit * 2;
last_digit = last_digit * 2;
temp_character = original_message[first_digit - 1];
original_message[first_digit - 1] = original_message[last_digit - 1];
original_message[last_digit - 1] = temp_character;
}
counter = 0;
while(counter!= string_length)
//__ Replacing the spaces will'@' __
{
if (original_message[counter] == ' ')
{
original_message[counter] = '@';
}
counter = counter + 1;
}
}
else
{
printf("\n\n\t\t __ 3 Digit Only __");
}
}
void message_decryption(char *original_message, int temp_key)
{
char temp_character;
int decryption_key = 0, string_length = 0, remainder = 0, counter = 0, previous_first = 0, previous_last = 0,last_digit = 0, first_digit = 0, original_first = 0, original_last = 0;
while(temp_key != 0)
{
remainder = temp_key % 10;
temp_key = temp_key / 10;
if (counter == 0 )
{
last_digit = remainder;
}
else if ( counter == 2)
{
first_digit = remainder;
}
counter++;
}
if (counter == 3)
{
string_length = 0;
while(original_message[string_length] != '\0')
//___ Counting the number of characters in the message__
{
string_length++;
}
original_first = first_digit;
original_last = last_digit;
counter = 0;
while(original_message[counter] != '\0')
//__ Replacing '@' with space __
{
if (original_message[counter] == '@')
{
original_message[counter] = 32;
}
counter++;
}
while((first_digit <= string_length) && (last_digit <= string_length))
{
previous_first = first_digit;
previous_last = last_digit;
first_digit = first_digit * 2;
last_digit = last_digit * 2;
}
while((previous_first != original_first) && (previous_last != original_last))
{
temp_character = original_message[previous_first - 1];
original_message[previous_first - 1] = original_message[previous_last - 1];
original_message[previous_last - 1] = temp_character;
previous_first = previous_first/2;
previous_last = previous_last/2;
}
temp_character = original_message[previous_first - 1];
original_message[original_first - 1] = original_message[original_last - 1];
original_message[original_last - 1] = temp_character;
}
else
{
printf("\n\n\t\t __ 3 Digit only __");
}
}
Follow Us on -
Assignment Hub