Cipher Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

/*

* Type: Monoalphabetic Substitution Cipher


* Caesar Cipher
*/
#include <iostream>
#include <conio.h>
#include<stdlib.h>
#include<string>
using namespace std;
class CaesarCipher
{
private:
static const int KEY = 3 ;
// the key for caesar cipher
public:
string encryption (string str)
{
string str2 = "" ;
for (int i=0 ; i<str.length() ; i++)
{
int x = ( ( ((int)str[i] - 65) + KEY ) % 26 ) + 65 ;
str2 += (char) x ;
}
return str2 ;
}
string decryption (string str)
{
string str2 = "" ;
for (int i=0 ; i<str.length() ; i++)
{
int x = ( ( ((int)str[i] - 65) - KEY ) % 26 ) ;
x = (x<0) ? (26- abs(x)) : x ;
str2 += (char)x+65 ;
}
return str2 ;
}
};
int menu () ;
int main()
{
CaesarCipher caesar ;
bool state = true ;
while (state)
{
switch ( menu() )
{
case 1:
{
cout << "\nEnter The Plain Text : \n";
string str ;
char ch ;
while ( (ch=getche()) != '\r' )
{

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");

cout << "\n\n";


return 0;
}
int menu ()
{
cout << "\t\t Caesar 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 ;
}

______________________________________________________________________________________
/*
* 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);

int AffineCipher :: getInverse (int m , int n)


{
int s0 = 1 , s1 = 0 ;
int t0 = 0 , t1 = 1 ;
// save this number in varibale
int r0 = m ;
int r1 = n ;

// 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 ;

string AffineCipher :: decryption (string cipherText , int m , int key , int n )


{
string str2 = "" ;
int inverse_M = getInverse(m,n) ;
for (int i=0 ; i<cipherText.length() ; i++)
{

int x = ( ( inverse_M * ( ((int)cipherText[i] - 65) - key ) ) % n ) ;


str2 += (char)x+65 ;
}
return str2 ;
}
int menu () ;
int main()
{
AffineCipher affine ;
bool state = true ;
while (state)
{
switch ( menu() )
{
case 1:
{
cout << "\nEnter The Plain Text : \n";
string str ;
char ch ;
while ( (ch=getche()) != '\r' )
{
if ( ch == ' ')
continue ;
str += ch ;
}
int m , n , key;
character
>> m ;
key ;

// n = 26 , if english

cout << "\nEnter The Multiplier M : " ; cin


cout << "Enter The Key (Shift) : " ; cin >>
cout << "Enter The Alpha Size N : " ; cin >>

n;
if ( affine.checkKey(m,n) )
{
cout << "GCD(" << m << "," << n
<< ") = 1\tCorrect \n";
affine.encryption(str,m,key,n) << endl;

cout << "The Cipher Text is : " <<


}
else

<< ") != 1 \tIncorrect \n",

cout << "GCD(" << m << "," << n


cout << "You Must Choose Another M

and N \n" ;
break ;

}
case 2:
{

cout << "\nEnter The Cipher Text : \n";


string str ;
char ch ;
while ( (ch=getche()) != '\r' )
{
if ( ch == ' ')
continue ;

str += ch ;

int m , n , key;

character

// n = 26 , if english

cout << "\nEnter The Multiplier M : " ; cin

>> m ;

cout << "Enter The Key (Shift) : " ; cin >>

key ;

cout << "Enter The Alpha Size N : " ; cin >>

n;

if ( affine.checkKey(m,n) )
{
cout << "GCD(" << m << "," << n

<< ") = 1\tCorrect \n";

cout << "The Plain Text is : " <<

affine.decryption(str,m,key,n) << endl;


}
else

cout << "GCD(" << m << "," << n

<< ") != 1 \tIncorrect \n",

cout << "You Must Choose Another M

and N \n" ;
break ;

}
case 3:
character
m;

int m , n ;

// n = 26 , if english

cout << "Enter The Multiplier M : " ; cin >>


cout << "Enter The Alpha Size N : " ; cin >>

n;
if (affine.checkKey(m,n) )
cout << "Correct Key\n";
else

cout << "Incorrect Key\n";


break ;
case 4:
{
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 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 = "";

int kplength = keyPhrase.length();


for (int i=0 ; i<plainText.length() ; i++)
{
int x = ( ((int) plainText[i]) - 65 ) ;
int y = ( ((int) keyPhrase[i%kplength]) - 65 );
cipherText += getCharFromVigenereTable(x,y) ;
}
return 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 ;
}

cout << "\n\nEnter The Key Phrase : " ;


while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;
keyPhrase += ch ;
}
cout << "\n\n";
cout << "\n\nThe Cipher Text is : " << fv.Encryption(plainText,keyPhrase) ;
break;
}
case 2:
{
plainText = "" ;
keyPhrase = "" ;
cipherText = "" ;
cout << "Enter The Cipher Text : " ;
while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;
cipherText += ch ;
}
cout << "\n\nEnter The Key Phrase : " ;
while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;
keyPhrase += ch ;
}
cout << "\n\nThe Plain Text is : " << fv.Decryption(cipherText,keyPhrase) ;
break;
}
case 3:
{
system("cls");
fv.printVigenereTable();
cout << "\n" ;
break;
}
case 4:
{
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 Full Vigenere Cipher \n";


cout << "\t\t---------------------------------\n";
cout << "Encryption
................. [1]\n";
cout << "Decryption
................. [2]\n";
cout << "Vigenere Table ................. [3]\n";
cout << "Exit Now
................. [4]\n";
cout << "\n \t >> " ;
int choice ;
cin >> choice ;
cout << "\n";
return choice ;}

/*
* 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 ;

string AutoKeyVigenere ::Decryption (string cipherText , string keyPhrase )


{
string plainText = "";
for (int i=0 ; i<cipherText.length() ; i++)
{
int x = ( ((int) cipherText[i]) - 65 ) - ( ((int) keyPhrase[i]) - 65 );
x = x % 26 ;
x = (x<0) ? (26- abs(x)) : x ;
plainText += (char) x+65 ;
keyPhrase += (char) x+65 ;

}
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 ;

int kplength = keyPhrase.length();


int ptlength = plainText.length();
if ( kplength < ptlength)
{
int x = ptlength - kplength ;

for( int i=0 ; i<x ; i++)


keyPhrase += plainText[i] ;

cout << "\n\n";


cout << "\n\nThe Cipher Text is : " << akv.Encryption(plainText,keyPhrase) ;
break;

case 2:
{
plainText = "" ;
keyPhrase = "" ;
cipherText = "" ;
cout << "Enter The Cipher Text : " ;
while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;

cipherText += ch ;

cout << "\n\nEnter The Key Phrase : " ;


while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;
keyPhrase += ch ;
}

cout << "\n\nThe Plain Text is


break;

: " << akv.Decryption(cipherText,keyPhrase) ;

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";

cout << "Encryption ................ [1]\n";


cout << "Decryption ................ [2]\n";
cout << "Exit Now ................ [3]\n";
cout << "\n \t >> " ;
int choice ;
cin >> choice ;
cout << "\n";
return choice ;
}

/* 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() ;

char I,J in array

bool foundChar(char ch);

// fill 5*5 array


// print 5*5 array
// found if the
// found if the char ch

in array
string plainTextProcessing (string pt); // remove space and repeative char
from plain text

string cipherTextProcessing(string ct); // remove space in cipher text


void setPlainText(string pt);
// take plain text from user
void getPlainText();
// print plaintext ,

each two char in block


void setCipherText(string st);

// take cipher from user

each two char in block

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 << " " ;

// print space between two char , and no

cout << plainText[i] ;


}
cout << "\n\n";
}
void PlayFair :: setCipherText( string ct)
{
string str = cipherTextProcessing(ct) ;
cipherText = str ;
}
void PlayFair :: getCipherText ()
{
for (int i=0 ; i<cipherText.length() ; i++)
{
if ( i % 2 == 0 && i != 0)
space at begnning
cout << " " ;

// print space between two char , and no

cout << cipherText[i] ;


}
cout << "\n\n";
}
bool PlayFair :: foundChar ()
{
for (int i=0 ; i<5 ; i++)
for (int j=0 ; j<5 ; j++)
if ( array[i][j] == 'I' || array[i][j] == 'J')
return true ;

return false;

bool PlayFair :: foundChar (char ch)


{
for (int i=0 ; i<5 ; i++)

for (int j=0 ; j<5 ; j++)


if ( array[i][j] == ch )
return true ;
return false;
}
void PlayFair :: initializingArray (string str)
{
int i =0 ;
int
j =0 ;
int k = 0;
char ch = 'A' ;
// fill array with key , didn't take repeat char
for (k=0 ; k<str.length() ; k++)
{
if ( j == 5 )
{
i++ ;
j = 0;
}
if ( (str[k] == 'J'|| str[k] == 'I' ) && foundChar() )
continue ;
if ( foundChar(str[k]) )
continue ;
array[i][j++] = str[k] ;

}
// 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 ;

string PlayFair ::plainTextProcessing(string pt)


{
string str2 = "" ;
for (int i=0 ; i<pt.length() ; i++)
{
if ( pt[i] == ' ' )
continue ;

if ( (pt[i] == pt[i-1]) && (i%2 != 0) )


str2 += 'X' ;

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 ;
}
}

// if character in same raw


if ( ch1Raw == ch2Raw )
{
cipherText += array[ch1Raw][(ch1Col+1)%5] ;
cipherText += array[ch2Raw][(ch2Col+1)%5] ;
}
// if character in same column
else if ( ch1Col == ch2Col )
{
cipherText += array[(ch1Raw+1)%5][ch1Col] ;
cipherText += array[(ch2Raw+1)%5][ch2Col] ;
}
// if character in different raw and col
else
{
cipherText += array[ch2Raw][ch1Col] ;
cipherText += array[ch1Raw][ch2Col] ;
}
}
}
void PlayFair :: decryption()
{
plainText = "" ;
int ch1Col , ch1Raw ; // spcified [i][j] for char1

int ch2Col , ch2Raw ; // spcified [i][j] for char2


char ch1 , ch2 ;
for (int k=0; k<cipherText.length()-1 ; k+=2)
{
ch1 = cipherText[k] ; // take first char in block
ch2 = cipherText[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 ;
}
}

// if character in same raw


if ( ch1Raw == ch2Raw )
{
plainText += array[ch1Raw][(ch1Col-1)%5] ;
plainText += array[ch2Raw][(ch2Col-1)%5] ;
}
// if character in same column
else if ( ch1Col == ch2Col )
{
plainText += array[(ch1Raw-1)%5][ch1Col] ;
plainText += array[(ch2Raw-1)%5][ch2Col] ;
}
// if character in different raw and col
else
{
plainText += array[ch2Raw][ch1Col] ;
plainText += array[ch1Raw][ch2Col] ;
}
}
}
int menu () ;
int main ()
{

bool state = true ;


while (state)
{
switch ( menu() )
{
case 1:
{

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 ;

while ( (ch=getche()) != '\r' )


{
if ( ch == ' ')
continue ;
key += 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";

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 ;
}

/*
* 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 ;

cout << "\n\nCipherText = " << str << endl;


cout << "\nPlainText = " << ROT13(str) << endl;
break ;
}
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 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 ;

string ROT13 (string str)


{
string str2 = "";
int KEY;
for (int i=0 ; i< str.length() ; i++)
{
int x = ( ( ((int)str[i] - 65) + KEY ) % 26 ) + 65 ;
str2 += (char) x ;
}

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 ;

string RunningKeyVigenere ::Decryption (string cipherText , string keyPhrase )


{
string plainText = "";
for (int i=0 ; i<cipherText.length() ; i++)
{
int x = ( ((int) cipherText[i]) - 65 ) - ( ((int) keyPhrase[i]) - 65 );
x = x % 26 ;
x = (x<0) ? (26- abs(x)) : x ;

plainText += (char) x+65 ;

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 ;

cout << "\n\nEnter The Key Phrase : " ;


while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;
keyPhrase += ch ;
}
int kplength = keyPhrase.length();
int ptlength = plainText.length();
if ( kplength < ptlength )
{
cout << "\n\nKeyPhrase Must me Greater or same length as plain Text !\n";
break ;
}
cout << "\n\n";

cout << "\n\nThe Cipher Text is : " << rkv.Encryption(plainText,keyPhrase) ;


break;
}
case 2:
{
plainText = "" ;
keyPhrase = "" ;
cipherText = "" ;
cout << "Enter The Cipher Text : " ;
while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;
cipherText += ch ;
}
cout << "\n\nEnter The Key Phrase : " ;
while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;

keyPhrase += ch ;

int kplength = keyPhrase.length();


int ctlength = cipherText.length();
if ( kplength < ctlength )
{
cout << "\n\nKeyPhrase Must me Greater or same length as
cipherText !\n\n";
break ;
}
cout << "\n\nThe Plain Text is
break;

: " << rkv.Decryption(cipherText,keyPhrase) ;

}
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 Running Key Vigenere 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 ;
cout << "\n";
return choice ;
}

/*
* 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 ;
}

string SimpleVigenere ::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) keyPhrase[i%kplength]) - 65 );
x = x % 26 ;
x = (x<0) ? (26- abs(x)) : x ;
plainText += (char) x+65 ;
}

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 ;

cout << "\n\nEnter The Key Phrase : " ;


while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;
keyPhrase += ch ;
}

cout << "\n\n";


cout << "\n\nThe Cipher Text is : " << sv.Encryption(plainText,keyPhrase) ;
break;

case 2:
{
plainText = "" ;
keyPhrase = "" ;
cipherText = "" ;
cout << "Enter The Cipher Text : " ;
while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;

cipherText += ch ;

cout << "\n\nEnter The Key Phrase : " ;


while ( (ch=getche()) != '\r')
{
if ( ch == ' ')
continue ;
keyPhrase += ch ;
}

cout << "\n\nThe Plain Text is : " << sv.Decryption(cipherText,keyPhrase) ;


break;

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";

cout << "Encryption ................ [1]\n";


cout << "Decryption ................ [2]\n";
cout << "Exit Now ................ [3]\n";
cout << "\n \t >> " ;
int choice ;
cin >> choice ;
cout << "\n";
return choice ;
}

You might also like