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

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

11 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
  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

Leave a Reply

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