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++.

33 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
    1. Jones, H

      The purpose for having a key is to determine the number of letters to move over to encrypt

      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
  11. Jones, H

    Hey, does anyone know how to put the script she used into python, I am having trouble with it.

    Reply
  12. Jayaganesh

    If the key value is more than 5 the out put is wrong for z and of the key is more than 6 the out put for y is also wrong.can any one explain?

    Reply
    1. salar asker zada

      Hej!
      It is bug in the program. Simple char can holp up to 127 values. z is equal to 122 and if you want to shift z up to 6 spaces so its become 128. Just change char declaration to unsinged char ch;
      It would work.

      Reply
  13. salar asker zada

    Would it work if I choose key 6 or above and i have character z in my string? I don’t think so.
    char has range 0-127, it would be better if you declare it like unsigned char ch;

    Reply
  14. thodoris katsoulakos

    i im new programmer i dont know very much about c but i thought a way for that but it doesn’t work clearly if you have time could y tell me why not working?

    #include

    int main(){
    char mystring(int k){
    char a[52];
    int m;
    m=0;
    a[m]=”a”;
    a[m++]=”b”;
    a[m++]=”c”;
    a[m++]=”d”;
    a[m++]=”e”;
    a[m++]=”f”;
    a[m++]=”g”;
    a[m++]=”h”;
    a[m++]=”i”;
    a[m++]=”j”;
    a[m++]=”k”;
    a[m++]=”l”;
    a[m++]=”m”;
    a[m++]=”n”;
    a[m++]=”o”;
    a[m++]=”p”;
    a[m++]=”q”;
    a[m++]=”r”;
    a[m++]=”s”;
    a[m++]=”t”;
    a[m++]=”u”;
    a[m++]=”v”;
    a[m++]=”w”;
    a[m++]=”x”;
    a[m++]=”y”;
    a[m++]=”z”;
    int f;
    f=26;
    int l;
    l=0;
    a[f++]=toupper(a[l]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    a[f++]=toupper(a[l++]);
    int d;
    d=0;

    while(d<=52){
    if(a[d]=k){ k=1;
    }
    d++;
    }
    return k;
    }

    char source[50],target[50];
    printf("give letter/if you want to stop give 2:");
    char on[50];

    int p;
    p=0;

    while((on[p]=getchar())!="2"){
    p++;}
    int h;
    h=0;
    int metr;

    while(h<=p){metr=0; if(mystring(on[h])!=1){target[h]="á"; source[h]=on[h]; h++;
    } else if(on[h]="x"){ source[h]="x"; target[h]="a"; h++; metr++;}else
    if(on[h]="X"){ source[h]="X"; target[h]="A"; h++; metr++;}else
    if(on[h]="y"){ source[h]="y"; target[h]="b"; h++; metr++;}else
    if(on[h]="Y"){ source[h]="Y"; target[h]="B"; h++; metr++;}else
    if(on[h]="z"){ source[h]="z"; target[h]="c"; h++; metr++;}else
    if(on[h]="Z"){ source[h]="Z"; target[h]="C"; h++; metr++;}else

    if(metr=0){ source[h]=on[h]; target[h]=on[h]+3; h++;}

    }

    int sp;

    for(sp=0;sp<p;sp++){ if(sp=0){printf("word:");} printf("%c",source[sp]);
    }
    for(sp=0;sp<p;sp++){ if(sp=0){printf("Caesar's hidden word:");} printf("%c",target[sp]);
    }

    return 0;

    }

    Reply
  15. Krishna

    I have a problem that requires use of Caesar cipher to encrypt and decrypt message in C++, but with a keyword for example “bat”. Also, i need to avoid duplication of letters and use upper case. Can you do this problem with the use of a keyword!. Help appreciated.

    Reply
  16. Xee

    Have error…

    If you encrypt capital letter.. Decryption is wrong!

    Reply
  17. Nishant kumar

    bro your code is not efficient.
    check this
    Ciphering
    26

    Reply

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.