Techknow Study

Encryption / Decryption

8:05:00 PM vikas 0 Comments Category :



Encryption/Decryption using Caesar Cipher :-


In cryptography, a Caesar cipher, also known as a Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. The method is named after Julius Caesar, who used it to communicate with his generals.

 

METHOD OF ENCRYPTION-: 

The transformation can be represented by aligning two alphabets; the cipher alphabet is the plain alphabet rotated left or right by some number of positions. For instance, here is a Caesar cipher using a left rotation of three places (the shift parameter, here 3, is used as the key):
Plain:   ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher:  DEFGHIJKLMNOPQRSTUVWXYZABC


To encrypt a message, simply look up each letter of the message in the "plain" line and write down the corresponding letter in the "cipher" line. To decipher, do the reverse.
Plaintext:  the quick brown fox jumps over the lazy dog
Ciphertext: WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ
The encryption can also be represented using modular arithmetic by first transforming the letters into numbers, according to the scheme, A = 0, B = 1,..., Z = 25. Encryption of a letter x by a shift n can be described mathematically as,
Decryption is performed similarly,
The replacement remains the same throughout the message, so the cipher is classed as a type of monoalphabetic substitution, as opposed to polyalphabetic substitution.

 




program:-

#include <iostream.h>
#include <conio.h>
#include <ctype.h>
int SQR(int x)
          {
          int yes=0, i=1;
          while(i<=17 && yes==0)
                   {
                   if(i*i==x)
                             {
                             yes=1;
                             sr=i;
                             }
                   else
                             i++;
                   }
          return yes;
          }

void main()
{
clrscr();
int choice, n;
cout<<"CAESAR CIPHER \n";
cout<<"1. Code your text \n";
cout<<"2. Decode a cipher \n";
cin>>choice;
char A[300], W;
switch (choice)
          {
          case 1:
                   clrscr();
                   do
                             {
                             cout<<"Enter the total number of letters in your message (perfect square) ";
                             cin>>n;
                             } while(SQR(n)==0);
                   cout<<"Enter your message : \n";
                   for(int o=0; o<n; o++)
                             {
                             do
                                      {
                                      cin>>A[o];
                                      } while (isalnum(A[o])==0);
                             }
                   int z=0;
                   clrscr();
                   for(int y=1; y<=2*sr; y+=2)
                             {
                             for(int q=1; q<=sr; q++)
                                      {
                                      gotoxy(y, q);
                                      cout<<(char)toupper(A[z]);
                                      z++;
                                      }
                             cout<<endl;
                             }
                   cout<<"\nOR\nThe coded text is \n";
                   for(int qw=0; qw<sr; qw++)
                             {
                             for(int c=0; c<n; c+=sr)
                             cout<<(char)toupper(A[c+qw]);
                             }
          break;

          case 2:
                   clrscr();
                   do
                             {
                             cout<<"Enter the number of characters in your code ";
                             cin>>n;
                             } while(SQR(n)==0);
                   cout<<"Enter the cipher text as it appears in rows \n";
                   for(int b=0; b<n; b++)
                             {
                             cin>>A[b];
                             cout<<"\nThe decoded text is \n";
                             for(int h=0; h<sr; h++)
                                      {
                                      for(int c=0; c<n; c+=sr)
                                                cout<<A[c+h];
                             }        }
          break;
          }
          getch();
 }


RELATED POSTS

0 comments