Caesar Cipher Encryption & Decryption program in C

C program using functions – fgetc(), fgets(), fputs(), fputc() - Blurnooz.com

Caesar Cipher Encryption & Decryption Program Download File Available below Code.

Encryption Program:-

 #include<stdio.h>
 #include<conio.h>
 int main()
 {
        char c[100], ch;
        int i, key;
        clrscr();
        printf("Enter the message: ");
        gets(c);
        printf("Enter the key: ");
        scanf("%d",&key);
        for(i=0;c[i]!= '\0';i++)
        {
             ch = c[i];
             if(ch >= 'a' && ch <= 'z')
             {
                   ch = ch + key;
                   if(ch > 'z')
                   {
                         ch = ch - 'z'+ 'a' - 1;
                   }
                   c[i] = ch;
             }
             else
             if(ch >= 'A' && ch <= 'Z')
             {
                   ch = ch + key;
                   if(ch > 'Z')
                   {
                         ch = ch - 'Z' + 'A' - 1;
                   }
                   c[i] = ch;
              }
              printf("\nThe Encrypted message is: %s", c);
       }
       getch();
       return 0;
 }

OUTPUT:

Caesar Cipher Encryption with output

 

Decryption Program:-

 #include<stdio.h>
 #include<conio.h>
 int main()
 {
        char c[100], ch;
        int i, key;
        clrscr();
        printf("Enter the message: ");
        gets(c);
        printf("Enter the key: ");
        scanf("%d",&key);
        for(i=0;c[i]!= '\0';i++)
        {
             ch = c[i];
             if(ch >= 'a' && ch <= 'z')
             {
                  ch = ch - key;
                  if(ch > 'z')
                  {
                       ch = ch + 'z'- 'a' + 1;
                  }
                  c[i] = ch;
             }
             else 
             if(ch >= 'A' && ch <= 'Z')
             {
                  ch = ch - key;
                  if(ch > 'Z')
                  {
                       ch = ch + 'Z' - 'A' + 1;
                  }
                  c[i] = ch;
              }
              printf("\nThe Encrypted message is: %s", c);
        }
        getch();
        return 0;
 }

OUTPUT:-

Caesar Cipher Decryption with output

Attachments

Leave a Reply

Please enter your comment!
Please enter your name here

*