by Dinesh Thakur Category: Control Structures

C program to encrypt text using one of the simplest ciphers known as the "Caesar cipher." In this encryption scheme, we shift all characters by a given offset. For example, if we use an offset of 4, every occurrence of 'A' will be replaced by 'E', every occurrence of 'B' will be replaced by 'F', and so forth. The encrypted text can be decrypted by using the reverse process if know the offset used for the encryption.

#include <stdio.h>

#include <ctype.h>
#define MAX 200
int main()
{
     char instr[MAX], crypt[MAX], decrypt[MAX], aa;
     int shift = 6, k;/* this is the Caesar encryption key */
     clrscr();
     printf("\n Enter your string\n");
     gets(instr);
     printf( "\noriginal string is [%s]",instr);
     k=0;
     while(instr[k] !='\0')
      {
               aa = toupper(instr[k]);
             if(aa != ' ') aa += shift;
             {
                if(aa > 'z') aa -=26;
                {
                   crypt[k] = aa;
                   k++;
                }
             }
      }
                crypt[k]='\0';
                printf("\nEncrypted string is [%s]",crypt);
                k=0;
            while(crypt[k] !='\0')
            {
                aa = crypt[k];
              if(aa != ' ') aa -=shift;
              {
                 if(aa < 'A' && aa !=' ') aa +=26;
                 {
                          decrypt[k] = aa;
                          k++;
                 }
              }
            }
               decrypt[k]='\0';
               printf("\n Decrypted string is [%s]",decrypt);
               return 0;
}

string encryption using Caesar cipher

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content





Popular Article