Thursday 23 July 2015

Caesar Cipher

A Caesar cipher (shift cipher) is one of the simplest encryption methods. It is a Substitution Cipher that involves replacing each letter of the secret message with a different letter of the alphabet which is a fixed number of positions further in the alphabet.

Example : Apple -------------Shift By 2----------> Crrng 

Now write a C++ program to Encrypt data using shift cypher . 
  • Take care that small letters encrypted to small letters and  capital letters are encrypted to capital letters 
  • character except a...z and A...Z should not encripted
  • keep in mind that shift key may be from 1 to large number. 



Caesar Cipher (C++)


  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int main(){
  5.         int N,i,k;
  6.         string st; //String To Encode
  7.         string rst="";
  8.         cout<<"Enter String :"<<endl;
  9.         cin>>st;
  10.         cout<<"Enter Shift Key :"<<endl;
  11.         cin>>k;
  12.        
  13.         //Start
  14.         i=0;
  15.         k=k%26;
  16.         //If shift is greter than 26 then it repreat itself
  17.         //Just Like angle repeat after 360 degree
  18.         int temp;
  19.         while(i<st.size()){
  20.                 temp=int(st[i]);
  21.                 if(temp>96 && temp<123){
  22.                         if((temp+k)<123){
  23.                                 temp=temp+k;
  24.                         }
  25.                         else{
  26.                                 temp=96+(k-(122-temp));
  27.                         }
  28.                 }
  29.                 else if(temp>64 && temp< 91){
  30.                         if((temp+k)<91){
  31.                                 temp=temp+k;
  32.                         }
  33.                         else{
  34.                                 temp=64+(k-(90-temp));
  35.                         }
  36.                 }
  37.                 i++;
  38.                 rst +=char(temp);
  39.         }
  40.         cout<<rst<<endl;
  41.         return 0;
  42. }
GET More Basic Crypto AlgorithmsGithub

Python 2.7  (Using Modular Approach)

  1. #Shift Cipher
  2. # Additive Cipher
  3. #********************************************************
  4. def shift_cipher(st,key):
  5.     '''
  6.        It return shift cipher encoded string
  7.        It does not Encode any char
  8.        Not in range A..Z or a..z and 0..1        
  9.    '''
  10.     numkey=key%10
  11.     key=key%26
  12.     rst=''
  13.     for ch in st:
  14.         asciiV=ord(ch)
  15.         if asciiV>64 and asciiV<91:
  16.             rst +=chr(((asciiV-64)+key)%26 +64)
  17.            
  18.         elif asciiV>96 and asciiV<123:
  19.             rst +=chr(((asciiV-96)+key)%26 +96)
  20.                
  21.         elif asciiV>47 and asciiV<58:
  22.             rst +=chr(((asciiV-47)+key)%10 +47)    
  23.                
  24.         else:
  25.             rst +=ch
  26.     return rst
  27. if __name__=='__main__':
  28.     st=raw_input('Enter String To encode : ')
  29.     key =int(raw_input('Key ?? :'))
  30.     print str(shift_cipher(st,key))

No comments:

Post a Comment

THANKS FOR UR GREAT COMMENT

Blogger Widgets