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

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

  1. #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
    */

    1. 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…. 😛

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

  3. 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 ?

  4. 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?

    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.

      1. This doesn’t work for capital letters for some reason, do you know why?

        And also how to implement numbers to work more than 10 shifts.

  5. 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;

  6. 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;

    }

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

  8. What if the key is a negative number? For example, if the message is ‘bat’ and key is -2, then this doesn’t convert the code into just the alphabet. It will include special characters.
    How can it be coded to keep within the alphabet in this direction?

  9. there’s a mistake in the decryption part of c++ program ….
    it should be like
    for(i = 0; message[i] != ‘\0’; ++i){
    ch = message[i];

    if(ch >= ‘a’ && ch <= 'z'){
    ch = ch – key;

    if(ch = ‘A’ && ch <= 'Z'){
    ch = ch – key;

    if(ch < 'A'){
    ch = ch + 'Z' – 'A' + 1;
    }

    message[i] = ch;
    }
    }

  10. hi neeraj bro,

    pls guide me, how to get ciphertext by shifting key in below code
    #include
    #include
    #include
    #include

    // getting command-line arguments
    int main(int argc, string argv[])
    {
    int i;
    char plaintext[100], c;
    if (argc == 2)
    {
    //converting command-line argument from string to int
    i = atoi(argv[1]);
    }
    else
    {
    printf(“Usage: ./caesar key\n”);
    }

    {
    //prompting user for plaintext
    string s = get_string(“Plaintext:”);
    for (i = 0; i < strlen(s) ; i++)
    {
    c = (c+i)%26; (<———pls guide me, how to get ciphertext by shifting key in this line)
    s[i] = c;
    // printing out ciphertext
    printf("ciphertext: %i", c);
    }
    printf("\n");
    }
    }

    i am getting output as ciphertet:0

  11. //Simple C++ program to encrypt and decrypt a string

    Bro why it give me wrong result of encryption and decryption, can you find the mistake.

    #include
    using namespace std;

    int main()
    {
    int i, x;
    char str[100];

    cout <> str;

    cout << "\nPlease choose following options:\n";
    cout << "1 = Encrypt the string.\n";
    cout <> x;

    //using switch case statements
    switch (x)
    {
    //first case for encrypting a string
    case 1:
    for (i = 0; (i < 100 && str[i] != '\0'); i++)
    str[i] = (str[i] + 2)%26; //the key for encryption is 3 that is added to ASCII value

    cout << "\nEncrypted string: " << str << endl;
    break;

    //second case for decrypting a string
    case 2:
    for (i = 0; (i < 100 && str[i] != '\0'); i++)
    str[i] = (str[i] – 2)%26; //the key for encryption is 3 that is subtracted to ASCII value

    cout << "\nDecrypted string: " << str << endl;
    break;

    default:
    cout << "\nInvalid Input !!!\n";
    }
    system("pause");
    return 0;

    }

  12. The C code doesn’t work with negative shifts or with large shifts.. for example:

    the following text (minus the quotes)

    “this is a test”

    shifted 263

    Should read “wklv lv d whvw”

    But it does not.. why?

  13. How create a C++ code of cipher?
    example i have: aa=dr; ab=fh; ac=er;… and all any combinations with two letters (676)
    and i write input e.g.: aaacab
    and i get output: drerfh

  14. OMG, guys. You are plain IDIOTS. Your code does not work proper way. CHECK THIS OUT
    #include
    #include
    using namespace std;

    int main()
    {
    int step, len;
    char word[100];
    cin >> word >> step;
    len = strlen(word);
    char *pword = word;
    for (int i = 0; i<len; i++){
    word[i] = (*(pword+i)+(26-step)-65)%26 + 65;
    }
    cout << word;
    }

Leave a Comment

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