Caesar Cipher in C and C++ [Encryption & Decryption]

Get program for caesar cipher in C and C++ for encryption and decryption.

What is Caesar Cipher?

It is one of the simplest encryption technique in which each character in plain text is replaced by a character some fixed number of positions down to it.

For example, if key is 3 then we have to replace character by another character that is 3 position down to it. Like A will be replaced by D, C will be replaced by F and so on.

For decryption just follow the reverse of encryption process.

Caesar Cipher in C and C++ [Encryption & Decryption]

Below I have shared program to implement caesar cipher in C and C++.

Also Read: Vigenere Cipher in C and C++

Program for Caesar Cipher in C

Encryption

Output

Enter a message to encrypt: axzd
Enter key: 4
Encrypted message: ebdh

Decryption

Output

Enter a message to decrypt: ebdh
Enter key: 4
Decrypted message: axzd

Program for Caesar Cipher in C++

Encryption

Output

Enter a message to encrypt: asd zf
Enter key: 3
Encrypted message: dvg ci

Decryption

Output

Enter a message to decrypt: az GjK
Enter key: 2
Decrypted message: yx EhI

Comment below if you have doubts or found anything incorrect in above program for caesar cipher in C and C++.

18 thoughts on “Caesar Cipher in C and C++ [Encryption & Decryption]

  1. Jonathan

    What are ‘z’ and ‘a’ for exactly? Care to explain a bit on the conditional statements there?

    Reply
    1. Admin Post author

      We are checking that given character in message should be in between ‘a’ and ‘z’ or ‘A’ and ‘Z’.

      Reply
  2. Priyanka

    #include
    #include
    #include
    void main()
    {
    int k,j,i,h,u=0;
    char plain[30],p[30],c[30],alpha[30]=”abcdefghijklmnopqrstuvwxyz”;
    clrscr();
    printf(“**********Encryption**********”);
    printf(“\nEnter the plain text = “);
    gets(plain);
    printf(“Enter the key = “);
    scanf(“%d”,&k);
    for(i=0;i<strlen(plain);i++)
    {
    if(plain[i]==' ') {
    i++;u++; }
    for(j=0;j<strlen(alpha);j++)
    {
    if(alpha[j]==plain[i])
    {
    h=(j+k)%26;
    c[i-u]=alpha[h];
    break;
    }
    }
    }
    c[i-u]='\0';
    printf("\nCipher Text = ");
    puts(c);
    printf("**********Decryption**********");
    for(i=0;i<strlen(c);i++)
    {
    for(j=0;j<strlen(alpha);j++)
    {
    if(alpha[j]==c[i])
    {
    h=(j-k)%26;
    p[i]=alpha[h];
    break;
    }
    }
    }
    p[i]='\0';
    printf("\nPlain Text = ");
    puts(p);
    getch();
    }

    /*
    **********Encryption**********
    Enter the plain text = hello hii
    Enter the key = 3

    Cipher Text = khoorkll
    **********Decryption**********
    Plain Text = hellohii
    */

    Reply
  3. KRISHNA

    Sir what is the meaning of this condition i don’t undetstand
    if(ch > ‘z’){
    ch = ch – ‘z’ + ‘a’ – 1;
    }

    Reply
    1. Levi Lawliet

      It is to subtract the ASCII value of the character in the message[i] (i is index) and to shift it, in order to keep message in alphabets only and so that it doesn’t go in the special characters. In other words take this example :
      if your key is 2 and your word is zebra the *first letter* shifts by the ASCII value of z(ASCII value-122) to 124 and then when the outcome comes it will print something like this |gdtc and the vertical bar is an absurd in this case, so by placing the character like this ‘z’ (which is actually 122 in computer terms) is, admin writes this algorithm (very clever) [ch-‘z’+’a’ -1 ] in terms of computer what it does is [124-122+97-1=98] and 98 is the ASCII value of b and hence the shift still remains in the character format….. I hope this makes it clear
      PS: the whole point of doing this was to keep things simple the other way it could have been written:
      ch-26… i think correct me if I am wrong people…. 😛

      Reply
  4. maniraj

    Is there anyone in whatsapp group for programming plz add me 9788342473
    Thank you friends, sisters and brothers

    Reply
  5. Tiny Rick

    I get an error with the gets(message); below the enter the encryption message, why could that be?

    Reply
  6. Vineeth

    Hi,
    considering i dont know the key to decrypt and if i run the c ++ decryption code under a ‘for’ loop for 25 times the code doesn’t work.How do i do this ?

    Reply
  7. priya

    if(ch < 'a'){
    ch = ch + 'z' – 'a' + 1;
    I cannot understand ths.. plz explain ths with an example

    Reply
  8. Tany

    Bro actualy i didnt get the final output after running the program…can please guide me

    Reply
  9. Tani

    bro can u please explain me this peice of code
    if(ch > ‘z’)
    {
    ch = ch – ‘z’ + ‘a’ – 1;

    Reply
  10. Ernest Jude Maape

    why if I entered a 2 or 3 digit key the encryption has a letter with a symbol on it? like š?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *