A cipher is a set of rules for converting between plain text
and cipher-text. These rules often use a secret key.
A Virtual Cipher Wheel : http://invpy.com/cipherwheel
How to Encrypt with the
Cipher Wheel
First, write out your message
in English on paper. For this example we will encrypt the message, “The secret
password is Rosebud.” Next, spin the inner wheel around until its letters match
up with letters in the outer wheel. Notice in the outer wheel there is a dot
next to the letter A. Look at the number in the inner wheel next to the dot in
the outer wheel. This number is known the encryption key.
The encryption key is the
secret to encrypting or decrypting the message
So the steps to encrypt a
letter are:
1. Decide on a key from 1 to
25. Keep this key secret!
2. Find the plain-text letter’s
number.
3. Add the key to the plain-text
letter’s number.
4. If this number is larger
than 26, subtract 26.
5. Find the letter for the
number you’ve calculated. This is the cipher-text letter.
6. Repeat steps 2 to 5 for
every letter in the plain-text message.
To decrypt, subtract the key instead of adding it. For the cipher-text letter B, the number is 1. Subtract 1 – 13 to get -12. Like our
“subtract 26” rule for encrypting, when we are decrypting and the result is
less than 0, we have an “add 26” rule. -12 + 26 is 14. So the cipher-text letter
B decrypts back to letter O.
Code:
#include<iostream>
#include<cstring>
//#include<ctype>
char * encript(int k,char *text);
char * decript(int k,char *text);
using namespace std;
#include<cstring>
//#include<ctype>
char * encript(int k,char *text);
char * decript(int k,char *text);
using namespace std;
int main()
{
char text[50];
int key,x;
cout<<"Enter the text:";
cin>>text;
cout<<"Enter encryption key:";
cin>>key;
cout<<"Press 1 for Encryption\nPress 2 for Decryption\n";
cin>>x;
if(x==1)
cout<<"Encripted text:"<<encript(key,text)<<endl;
else
cout<<"Decripted text:"<<decript(key,text)<<endl;
return(0);
}
char * encript(int key,char *text)
{
int ti;
for(int i=0;i<strlen(text);i++)
{
if(isalpha(text[i])&&(isupper(text[i])))
{
ti=(int)text[i];
ti+=key;
if(ti>90)
ti-=26;
text[i]=(char)ti;
}
if(isalpha(text[i])&&(islower(text[i])))
{
ti=(int)text[i];
ti+=key;
if(ti>122)
ti-=26;
text[i]=(char)ti;
}
}
return(text);
}
char * decript(int key,char *text)
{
int ti;
for(int i=0;i<strlen(text);i++)
{
if(isalpha(text[i])&&(isupper(text[i])))
{
ti=(int)text[i];
ti-=key;
if(ti<65)
ti+=26;
text[i]=(char)ti;
}
if(isalpha(text[i])&&(islower(text[i])))
{
ti=(int)text[i];
ti-=key;
if(ti<97)
ti+=26;
text[i]=(char)ti;
}
}
return(text);
}