Cipher Programs
Cipher Programs
Cipher Programs
if ( ch == ' ')
continue ;
str += ch ;
}
cout <<" \nThe Plain Text :" << str << endl;
cout <<" \nThe Cipher Text :" << caesar.encryption(str) << endl;
break;
}
case 2:
{
cout << "\nEnter The Cipher Text : \n";
string str = "";
char ch ;
while ( (ch=getche()) != '\r' )
{
if ( ch == ' ')
continue ;
str += ch ;
}
cout << "\nThe Cipher Text : " << str << endl;
cout << "\nThe Plain Text : " << caesar.decryption(str) << endl;
break;
}
case 3:
{
state = false;
cout << "Good Luck !\n";
break;
}
default:
cout << "Error Choice , Try Again !";
break;
}
getch();
system("cls");
______________________________________________________________________________________
/*
* Type
: Monoalphabetic Substitution Cipher
* Affine Cipher
*/
/* to Encrypt With Affine Cipher :
c = m * p + key (mod n)
where m is Multiplirer , p is character in plaintext ,
key is shift number , n is alpha size
Note : to choose the right m , it must the GCD(m,n) = 1 , i.e m and n is relative prime number ,
i.e must use GCD method to check
To Decryption with Affine Cipher :
p = m` * ( c - key) (mod n)
where m` is the inverse of m
Note : to obtain the m` it must use Extended Euclid's Method
*/
#include <iostream>
#include <conio.h>
#include<string>
using namespace std;
class AffineCipher
{
public :
bool checkKey (int m , int n) ; // check to see if GCD(m,n) == 1 , return
true if that , false if not
int GCD ( int m , int n ) ; // return Greatest comman Divisor
int getInverse (int m ,int n) ; // return m` by using Extended Euclid method
string encryption (string plainText , int m , int key , int n ) ;
string decryption (string cipherText , int m , int key , int n ) ;
};
int AffineCipher :: GCD (int x , int y )
{
if ( y == 0 )
return x ;
else
}
return GCD(y,x%y);
// q1 is the qoution
int q = r0 / r1 ;
int r2 = r0 % r1 ;
int s2 , t2 ;
while ( r2 > 0 )
{
s2 = s0 - (s1 * q) ;
s0 = s1 ;
s1 = s2 ;
t2 = t0 - (t1 * q) ;
t0 = t1 ;
t1 = t2 ;
r0 = r1;
r1 = r2 ;
q = r0 / r1 ;
r2 = r0 % r1 ;
}
return s1 ;
}
bool AffineCipher :: checkKey (int m ,int n)
{
if ( GCD(m,n) == 1 )
return true ;
else
return false;
}
string AffineCipher :: encryption (string plainText , int m , int key , int n )
{
string str2 = "" ;
for (int i=0 ; i<plainText.length() ; i++)
{
int x = ( ( ( ((int)plainText[i] - 65) * m) + key ) % n ) + 65 ;
str2 += (char) x ;
}
return str2 ;
// n = 26 , if english
n;
if ( affine.checkKey(m,n) )
{
cout << "GCD(" << m << "," << n
<< ") = 1\tCorrect \n";
affine.encryption(str,m,key,n) << endl;
and N \n" ;
break ;
}
case 2:
{
str += ch ;
int m , n , key;
character
// n = 26 , if english
>> m ;
key ;
n;
if ( affine.checkKey(m,n) )
{
cout << "GCD(" << m << "," << n
and N \n" ;
break ;
}
case 3:
character
m;
int m , n ;
// n = 26 , if english
n;
if (affine.checkKey(m,n) )
cout << "Correct Key\n";
else
int menu ()
{
cout << "\t\t Affine Cipher \n";
cout << "\t\t-----------------\n";
cout << "Encryption ................ [1]\n";
cout << "Decryption ................ [2]\n";
cout << "Test Keys ................ [3]\n";
cout << "Exit Now ................ [4]\n";
cout << "\n \t >> " ;
int choice ;
cin >> choice ;
return choice ;
_________________________________________________________________________________
/*
* Type
: Polyalphabetic Substitution Cipher
* Full Vigenere Cipher
*/
#include <iostream>
#include <conio.h>
#include<string>
using namespace std;
class FullVigenere
{
private :
char vigenereTable[26][26];
public:
string Encryption (string plainText , string keyPhrase ) ;
string Decryption (string cipherText , string keyPhrase ) ;
char getCharFromVigenereTable (int col , int row ) ;
void makeVigenereTable ();
void printVigenereTable ();
};
void FullVigenere :: makeVigenereTable ()
{
char ch = 'A' ;
for (int i=0 ; i<26 ; i++)
{
for (int j=0 ; j<26 ; j++)
{
int x = (((int)ch-65)+j)%26;
vigenereTable[i][j] = (char)x+65 ;
}
ch++;
}
}
void FullVigenere :: printVigenereTable ()
{
for (int i=0 ; i<26 ; i++)
{
for (int j=0 ; j<26 ; j++)
cout << vigenereTable[i][j] << " " ;
cout << "\n" ;
}
}
char FullVigenere :: getCharFromVigenereTable (int col , int row )
{
return vigenereTable[row][col] ;
}
string FullVigenere :: Encryption (string plainText , string keyPhrase )
{
string cipherText = "";
}
string FullVigenere ::Decryption (string cipherText , string keyPhrase )
{
string plainText = "";
int kplength = keyPhrase.length();
for (int i=0 ; i<cipherText.length() ; i++)
{
int x = ( ((int) cipherText[i]) - 65 ) ;
int y = ( ((int) keyPhrase[i%kplength]) - 65 );
char ch = cipherText[i] ;
int j ;
for ( j=0 ; j<26 ; j++)
{
if ( getCharFromVigenereTable(y,j) == ch )
break;
}
plainText += getCharFromVigenereTable(0,j) ;
}
return plainText ;
}
int menu () ;
int main()
{
FullVigenere fv ;
fv.makeVigenereTable() ;
string plainText = "" ;
string keyPhrase = "" ;
string cipherText = "" ;
char ch ;
bool state = true ;
while (state)
{
switch ( menu() )
{
case 1:
{
plainText = "" ;
keyPhrase = "" ;
cipherText = "" ;
cout << "Enter The Plain Text : " ;
while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;
plainText += ch ;
}
}
getch();
system("cls");
}
cout << "\n\n";
return 0;
}
int menu ()
/*
* Auto-Key Vigenere Cipher
* Type
: Polyalphabetic Substitution Cipher
*/
#include <iostream>
#include <conio.h>
#include<string>
using namespace std;
class AutoKeyVigenere
{
public:
string Encryption (string plainText , string keyPhrase ) ;
string Decryption (string cipherText , string keyPhrase ) ;
};
string AutoKeyVigenere :: Encryption (string plainText , string keyPhrase )
{
string cipherText = "";
for (int i=0 ; i<plainText.length() ; i++)
{
int x = ( ((int) plainText[i]) - 65 ) + ( ((int) keyPhrase[i]) - 65 );
x = x % 26 ;
cipherText += (char) x+65 ;
}
return cipherText ;
}
return plainText ;
}
int menu () ;
int main()
{
string plainText = "" ;
string keyPhrase = "" ;
string cipherText = "" ;
AutoKeyVigenere akv ;
char ch ;
bool state = true ;
while (state)
{
switch ( menu() )
{
case 1:
{
plainText = "" ;
keyPhrase = "" ;
cipherText = "" ;
cout << "Enter The Plain Text : " ;
while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;
plainText += ch ;
}
cout << "\n\nEnter The Key Phrase : " ;
while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;
keyPhrase += ch ;
case 2:
{
plainText = "" ;
keyPhrase = "" ;
cipherText = "" ;
cout << "Enter The Cipher Text : " ;
while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;
cipherText += ch ;
case 3:
{
state = false;
cout << "Good Luck !\n";
break;
}
default:
cout << "Error Choice , Try Again !";
break;
}
getch();
system("cls");
}
cout << "\n\n";
return 0;
int menu ()
{
cout << "\t\t Auto-Key Vigenere Cipher \n";
cout << "\t\t---------------------------------\n";
/* PlayFair Cipher
*/
#include <iostream>
#include<string>
#include <conio.h>
using namespace std ;
class PlayFair
{
private :
public :
char array[5][5] ;
string plainText ;
string cipherText ;
PlayFair()
{
for (int i=0 ; i<5 ; i++)
for (int j=0 ; j<5 ; j++)
array[i][j] = 0 ;
}
void initializingArray (string key) ;
void printArray () ;
bool foundChar() ;
in array
string plainTextProcessing (string pt); // remove space and repeative char
from plain text
void getCipherText();
// print ciphertext ,
void encryption() ;
void decryption() ;
};
void PlayFair :: setPlainText ( string pt)
{
string str = plainTextProcessing(pt) ;
plainText = str ;
}
void PlayFair :: getPlainText ()
{
for (int i=0 ; i<plainText.length() ; i++)
{
if ( i % 2 == 0 && i != 0)
space at begnning
cout << " " ;
return false;
}
// fill the remainder of array with other char , if char is repeat ignore it
for (k=0 ; k<26 ; k++ , ch++)
{
if ( j == 5)
{
i++;
j = 0;
}
if ( (ch == 'J'|| ch == 'I' ) && foundChar() )
continue ;
if ( foundChar(ch) )
continue ;
array[i][j++] = ch ;
str2 += pt[i] ;
}
if ( str2.length() % 2 != 0 )
str2 += 'X' ;
return str2 ;
}
string PlayFair :: cipherTextProcessing (string ct)
{
string str2 = "" ;
for (int i=0 ; i<ct.length() ; i++)
{
if ( ct[i] == ' ' )
continue ;
str2 += ct[i] ;
}
return str2 ;
}
void PlayFair :: printArray ()
{
for (int i=0 ; i<5 ; i++)
{
for (int j=0 ; j<5 ; j++)
{
if ( array[i][j] == 'J') { cout << array[i][j] << "\\I" << " " ; continue ; }
if ( array[i][j] == 'I') { cout << array[i][j] << "\\J" << " " ; continue ; }
cout << array[i][j] << " " ;
}
cout << "\n";
}
}
void PlayFair :: encryption()
{
cipherText = "" ;
int ch1Col , ch1Raw ; // spcified [i][j] for char1
int ch2Col , ch2Raw ; // spcified [i][j] for char2
char ch1 , ch2 ;
for (int k=0; k<plainText.length()-1 ; k+=2)
{
ch1 = plainText[k] ;
// take first char in block
ch2 = plainText[k+1] ; // take second char in block
for (int i=0 ; i<5 ; i++)
{
for (int j=0 ; j<5 ; j++)
{
if (array[i][j] == ch1 )
{
ch1Col = j ;
ch1Raw = i ;
}
if ( array[i][j] == ch2 )
{
ch2Col = j ;
ch2Raw = i ;
}
}
PlayFair pf ;
string pt = "" ;
string key = "" ;
cout << "\nEnter The KeyPhrase : " ;
char ch ;
while ( (ch=getche()) != '\r' )
{
if ( ch == ' ')
continue ;
key += ch ;
if ( ch == ' ')
continue ;
}
cout << "\nEnter The Plain Text : ";
while ( (ch=getche()) != '\r' )
{
pt += ch ;
}
cout << "\n\nThe PlainText = " ;
pf.setPlainText(pt);
pf.getPlainText();
cout << "\n";
// print keyPhrase in 5*5 array
cout << "\nThe KeyPhrase = \n" ;
pf.initializingArray(key);
pf.printArray();
pf.encryption() ;
cout << "\n\n";
cout << "The CipherText = " ;
pf.getCipherText() ;
break;
case 2:
{
PlayFair pf ;
string ct = "" ;
string key = "" ;
cout << "\nEnter The KeyPhrase : " ;
char ch ;
if ( ch == ' ')
continue ;
}
cout << "\nEnter The Cipher Text : ";
while ( (ch=getche()) != '\r' )
{
ct += ch ;
}
cout << "\n\nThe cipherText = " ;
pf.setCipherText(ct) ;
pf.getCipherText();
cout << "\n";
// print keyPhrase in 5*5 array
cout << "\nThe KeyPhrase = \n" ;
pf.initializingArray(key);
pf.printArray();
pf.decryption() ;
cout << "\n\n";
cout << "\nThe Plain Text = " ;
pf.getPlainText() ;
break ;
}
case 3:
{
state = false;
cout << "Good Luck !\n";
break;
}
default:
cout << "Error Choice , Try Again !";
break;
}
getch();
system("cls");
}
return 0;
}
int menu ()
{
cout << "\t\t PlayFair Cipher \n";
/*
* ROT13 Cipher
* Type
: Monoalphabetic Substitution Cipher
*/
#include <iostream>
#include <conio.h>
#include<string>
using namespace std ;
string ROT13 (string str) ;
int menu () ;
int main()
{
bool state = true ;
while (state)
{
switch ( menu() )
{
case 1:
{
cout << "\nEnter The Plain Text : \n";
char ch ;
string str = "" ;
while ( (ch=getche()) != '\r' )
{
if ( ch == ' ')
continue ;
else
str += ch ;
}
cout << "\n\nPlainText = " << str << endl;
cout << "\nCipherText = " << ROT13(str) << endl;
break;
}
case 2:
{
cout << "\nEnter The Cipher Text : \n";
char ch ;
string str = "" ;
while ( (ch=getche()) != '\r' )
{
if ( ch == ' ')
continue ;
else
str += ch ;
int menu ()
{
cout << "\t\t ROT13 Cipher \n";
cout << "\t\t-----------------\n";
cout << "Encryption ................ [1]\n";
cout << "Decryption ................ [2]\n";
cout << "Exit Now ................ [3]\n";
cout << "\n \t >> " ;
int choice ;
cin >> choice ;
return choice ;
return str2 ;
/*
* Type
: Polyalphabetic Substitution Cipher
* Running Key Vigenere Cipher
*/
#include <iostream>
#include <conio.h>
#include<string>
using namespace std;
class RunningKeyVigenere
{
public:
string Encryption (string plainText , string keyPhrase ) ;
string Decryption (string cipherText , string keyPhrase ) ;
};
string RunningKeyVigenere :: Encryption (string plainText , string keyPhrase )
{
string cipherText = "";
for (int i=0 ; i<plainText.length() ; i++)
{
int x = ( ((int) plainText[i]) - 65 ) + ( ((int) keyPhrase[i]) - 65 );
x = x % 26 ;
cipherText += (char) x+65 ;
}
return cipherText ;
return plainText ;
int menu () ;
int main()
{
string plainText = "" ;
string keyPhrase = "" ;
string cipherText = "" ;
RunningKeyVigenere rkv ;
char ch ;
bool state = true ;
while (state)
{
switch ( menu() )
{
case 1:
{
plainText = "" ;
keyPhrase = "" ;
cipherText = "" ;
cout << "Enter The Plain Text : " ;
while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;
plainText += ch ;
keyPhrase += ch ;
}
case 3:
{
state = false;
cout << "Good Luck !\n";
break;
}
default:
cout << "Error Choice , Try Again !";
break;
}
getch();
system("cls");
/*
* Type
: Polyalphabetic Substitution Cipher
* Simple Shift Vigenere Cipher
*/
#include <iostream>
#include <conio.h>
#include<string>
using namespace std;
class SimpleVigenere
{
public:
string Encryption (string plainText , string keyPhrase ) ;
string Decryption (string cipherText , string keyPhrase ) ;
};
string SimpleVigenere :: Encryption (string plainText , string keyPhrase )
{
string cipherText = "";
int kplength = keyPhrase.length();
for (int i=0 ; i<plainText.length() ; i++)
{
int x = ( ((int) plainText[i]) - 65 ) + ( ((int) keyPhrase[i%kplength]) - 65 );
x = x % 26 ;
cipherText += (char) x+65 ;
}
return cipherText ;
}
return plainText ;
int menu () ;
int main()
{
string plainText = "" ;
string keyPhrase = "" ;
string cipherText = "" ;
SimpleVigenere sv ;
char ch ;
bool state = true ;
while (state)
{
switch ( menu() )
{
case 1:
{
plainText = "" ;
keyPhrase = "" ;
cipherText = "" ;
cout << "Enter The Plain Text : " ;
while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;
plainText += ch ;
case 2:
{
plainText = "" ;
keyPhrase = "" ;
cipherText = "" ;
cout << "Enter The Cipher Text : " ;
while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;
cipherText += ch ;
case 3:
{
state = false;
cout << "Good Luck !\n";
break;
}
default:
cout << "Error Choice , Try Again !";
break;
}
getch();
system("cls");
}
cout << "\n\n";
system("pause");
return 0;
}
int menu ()
{
cout << "\t\t Simple Shift Vigenere Cipher \n";
cout << "\t\t---------------------------------\n";