Tuesday 1 September 2015

Ceaser Cipher

Before moving to the program let us understand the concept, Caesar encrypted his confidential information using an encryption technique in which he rotated every letter in the string by a fixed number K(Key). (Except alphabets all other characters remains same). 
We are to make a program which does the same i.e. inputs a string and encrypts it as per Caesar cipher technique.


Here's the code:




#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

       int n,key,i,temp;
       char s[100];
       clrscr();
       printf("Enter the no. of characters in string: ");
       scanf("%d",&n);
        printf("Enter the string to be encrypted: ");
 scanf("%s",s);
        printf("Enter the value of key: ");
 scanf("%d",&key);
        key=key%26;
        
        for(i=0;i<n;i++){

        if((s[i]>=65&&s[i]<=90)){
     s[i]+=key;
 if(s[i]>90)
     s[i]-=26;
    }
      else if((s[i]>=97&&s[i]<=122)){
      temp=s[i];
   temp+=key;
 if(temp>122)
     temp-=26;
     s[i]=temp;

    }
 else
     continue;

    }
    printf("Encrypted string is: ");
     for(i=0;i<n;i++){
 printf("%c",s[i]);

    }
    return 0;

}

Output:
C program for caesar cipher

No comments:

Post a Comment