C Code 35 _ To Perform Encryption and Decryption using Caesar Cipher


C Code 35 _ To Perform Encryption and Decryption using Caesar Cipher 


Sample Input:


Message to Encrypt  :  abcd

Shifting value : 2

Sample Output:


Encrypted Message  :  cdef

Explanation:


According to the shift value right we need to shift each character.

if we shift ' a ' two times right we get ' c ' , simillarly if we shift ' y ' two times right we get ' a ' and so on.

Simillarly for decryption we need to shift  value left .

if we shift ' d ' one time left we get ' c ' and if we shift ' a ' one time left we get ' z ' and so on.


Program:


#include<stdio.h>
int main()
{
                char str[200],stre[200];
                int n,i=0,m,ch;
                printf("* * * Encryption and Decryption of a Message using Caesar Cipher * * *");
                printf("\n\n1 - Encryption\n2 - Decryption\n\nEnter Your Choice : ");
                scanf("%d",&ch);
                switch(ch)
                {
                                case 1:
                                                printf("\n\nEnter a String to encrypt : ");
                                                fflush(stdin);
                                                gets(str);
                                                printf("\n\nEnter a shift value(number) : ");
                                                scanf("%d",&n);
                                                while(str[i]!='\0')
                                                {
                                                                if(str[i]==32)
                                                                {
                                                                                stre[i]=str[i];
                                                                                i=i+1;
                                                                }
                                                                else
                                                                {
                                                                                if(str[i]>=65&&str[i]<=90)
                                                                                {
                                                                                                m=(int)str[i]-65;
                                                                                                m=(m+n)%26;
                                                                                                stre[i]=m+65;
                                                                                }
                                                                                else if(str[i]>=97&&str[i]<=122)
                                                                                {
                                                                                                m=(int)str[i]-97;
                                                                                                m=(m+n)%26;
                                                                                                stre[i]=m+97;
                                                                                }
                                                                                else
                                                                                {
                                                                                                stre[i]=str[i];
                                                                                }
                                                                                i=i+1;
                                                                }
                                                }
                                                stre[i]='\0';
                                                printf("\n\nEncrypted string : %s",stre);
                                                break;
                               
                                case 2:
                                                printf("\n\nEnter a String to decrypt : ");
                                                fflush(stdin);
                                                gets(stre);
                                                printf("\n\nEnter a shift value(number) : ");
                                                scanf("%d",&n);
                                                while(stre[i]!='\0')
                                                {
                                                                if(stre[i]==32)
                                                                {
                                                                                str[i]=stre[i];
                                                                                i=i+1;
                                                                }
                                                                else
                                                                {
                                                                                if(stre[i]>=65&&stre[i]<=90)
                                                                                {
                                                                                                m=(int)stre[i]-65;
                                                                                                m=(m-n)%26;
                                                                                                if(m<0)
                                                                                                {
                                                                                                                m=m+26;
                                                                                                }
                                                                                                str[i]=m+65;
                                                                                }
                                                                                else if(stre[i]>=97&&stre[i]<=122)
                                                                                {
                                                                                                m=(int)stre[i]-97;
                                                                                                m=(m-n)%26;
                                                                                                if(m<0)
                                                                                                {
                                                                                                                m=m+26;
                                                                                                }
                                                                                                str[i]=m+97;
                                                                                }
                                                                                else
                                                                                {
                                                                                                str[i]=stre[i];
                                                                                }
                                                                                i=i+1;
                                                                }
                                                }
                                                str[i]='\0';
                                                printf("\n\nDecrypted string : %s",str);
                                                break;
                                               
                                default:
                                                printf("\n\nEnter a valid choice 1 or 2 only");
                }
                return 0;
}



Output:
















Thank You ...





Previous Post
Next Post
Related Posts

0 comments:

Popular Posts