Aim: Implement Caesar cipher encryption-decryption.
Pseudocode:
- The source text that needs to be encrypted is given in lower case. But if you need to decrypt the text, it should be given in upper case.
- When it is encrypted, each letter will have its ANSII code increased for three places. When it is decrypted, it will have its code moved toward left.
- The letter ‘x’ will be translated into ‘A’, the letter ‘y’ is transformed into the letter ‘B’, and the ‘z’ will change into ‘C’.
- We are keeping this logic very simple so that we can understand the code. Once you get the hang of it, come-up with more complex logic to encrypt and decrypt.
- The program will handle only English letters and each input text will not be longer that one sentence. At the end of the input sentence, it should have the marker for end ‘.’.
- the longest sentence is 1024 letters long. This is some form of protection, which would prevent the user to input the sentence that would over populate size of the program.
- The numbers in the input will not be changed.
- The blank symbol or any nonletter symbol will not be changed.
Caesar cipher (shift cipher) is a simple substitution cipher based on a replacement of every single character of the open text with a character, which is fixed number of positions further down the alphabet.
In the times of Julius Caesar was used only the shift of 3 characters, but nowadays the term Caesar cipher refers to all variants (shifts) of this cryptosystem.
The encryption can be described with the following formula:
Ci - i-th character of the closed text
Ti - i-th character of the open text
k - shift
m - length of the alphabet
Ti - i-th character of the open text
k - shift
m - length of the alphabet
The process of decryption uses reverted procedure:
C Program:
Encryption:
#include<stdio.h>
int main()
{
char message[100], ch;
int i, key;
printf("Enter a message to encrypt: ");
scanf("%s",message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}
}
printf("Encrypted message: %s", message);
return 0;
}
Decryption:
#include<stdio.h>
int main()
{
char message[100], ch;
int i, key;
printf("Enter a message to decrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch < 'A'){
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}
}
printf("Decrypted message: %s", message);
return 0;
}
Viva Question
1. A ciphertext has been generated with an affine cipher. The most frequent letter of the ciphertext is 'B', and the second most frequent letter of the ciphertext is 'U'. Break this code.
2. In one of his cases, Sherlock Holmes was confronted with the following message.
534 C2 13 127 36 31 4 17 21 41
DOUGLAS 109 293 5 37 BIRLSTONE
26 BIRLSTONE 9 127 171
Although Watson was puzzled, Holmes was able immediately to deduce the type of cipher. Can you?
Comments
Post a Comment