Skip to main content

Caesar cipher

Aim:  Implement Caesar cipher encryption-decryption.

Pseudocode:
  1. 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.
  2. 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.
  3. The letter ‘x’ will be translated into ‘A’, the letter ‘y’ is transformed into the letter ‘B’, and the ‘z’ will change into ‘C’.
  4. 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.
  5. 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 ‘.’.
  6.  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.
  7. The numbers in the input will not be changed.
  8. 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:

C_{i} = (T_{i} + k) \\pmod{m}
Ci - i-th character of the closed text
Ti - i-th character of the open text
k - shift
m - length of the alphabet
The process of decryption uses reverted procedure:

T_{i} = (C_{i} - k) \\pmod{m}

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

Popular posts from this blog

Hill Cipher

                                            Hill cipher encryption-decryption. PSEUDOCODE Encryption To encrypt a message using the Hill Cipher we must first turn our keyword into a key matrix (a 2 x 2 matrix for working with digraphs, a 3 x 3 matrix for working with trigraphs, etc). We also turn the plaintext into digraphs (or trigraphs) and each of these into a column vector. We then perform matrix multiplication modulo the length of the alphabet (i.e. 26) on each vector. These vectors are then converted back into letters to produce the ciphertext. Step 1:  We shall encrypt the plaintext message "short example" using the keyword hill and a 2 x 2 matrix. The first step is to turn the keyword into a matrix. If the keyword was longer than the 4 letters needed, we would only take the first 4 letters, and if it was shorter, we would fill it up with the alphabet...

Playfair cipher

                                                          Playfair cipher encryption-decryption Pseudocode: 1.       First, a plaintext message is split into pairs of two letters (digraphs). If there is an odd number of letters, a Z is added to the last letter. Let us say we want to encrypt the message "Programming". It will be written as - Programming 2.       The rules of encryption are - 3.       If both the letters are in the same column, take the letter below each one (going back to the top if at the bottom) 4.       If both letters are in the same row, take the letter to the right of each one (going back to the left if at the farthest right) 5.       If neither of the preceding two rules is true, fo...

Mono alphabetic cipher

                                               Mono alphabetic cipher encryption-decryption A mono-alphabetic cipher is a type of simple substitution cipher. In this cipher technique, each letter of the plaintext is replaced by another letter in the cipher-text. An example of a mono-alphabetic cipher key follows: Plain Text   >>>   a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z Cipher Text >>>  z  w  x  y  o  p  r  q  a  h  c  b  e  s  u  t  v  f  g  j  l  k  m  n  d  i This key means that any ‘a’ in the plaintext will be replaced by a ...