//****************************CPP FILE*************************************//
#include<iostream>
#include<string>
#include<stdlib>
#include"fstream.h"
using namespace std;
#include"fstream.h"
using namespace std;
char ciphers[26];
char filename[26];
ifstream file1;
char type;
int flag=0,ind=0;
int flag1=0;
int y=0;
char keyword[11];
char keywordweights[11];
char ch=65;
char ed=0;
char plaincipher[26][2];
char c,c1;
char temp[2];
int l=0,flag3=0;
char filename2[]="encryptedfile.txt\0";
char filename3[]="decryptedfile.txt\0";
int len=0,wt=1,count=0,numofrows=0,col=0;
float mod=0;
ofstream file3;
char **doubletrans='\0';
void decrypt()
{
cout<<"Enter Name Of the File You Want to Decrypt:";
cin>>filename;
cout<<"Name Of The File To Be Decrypted:"<<filename<<"\n";
ofstream file2("decryptedfile.txt");
file1.open(filename);
while (file1.good()) // loop while extraction from file is possible
{ l=0;
c = file1.get(); // get character from file
count++;
}
file1.close();
count--;
numofrows=count/len;
doubletrans=new char*[numofrows+1];
for( int i=0 ; i < numofrows+1 ; i++ )
doubletrans[i] = new char[col];
for(i=0;i<len;i++)
doubletrans[0][i]=keywordweights[i];
ind=1;
int rr=1;
int ff=1;
i=0;
int flag4=0,tt;
file1.open(filename);
while (file1.good() && tt<count) // loop while extraction from file is possible
{ l=0;
tt++;
c = file1.get(); // get character from file
itoa(ind,temp,10);
if(rr==numofrows+1 )
{
ff=1;
i=0;
}
while(i<len && ff==1)
{rr=1;
if(doubletrans[0][i]!=temp[0])
{
i++;
flag4=1;
}
else
{ ind++;
ff=0;
break;
}
}
if(rr<numofrows+1 )
{doubletrans[rr][i]=c;
rr++;
}
}
file3.open("keymatrix.txt");
for(i=0;i<numofrows+1;i++)
{
for(int j=0;j<len;j++)
{ cout<<doubletrans[i][j];
file3<<doubletrans[i][j];
}
cout<<'\n';
file3<<'\n';
}
file1.close(); // close file
ofstream file11(filename3);
cout<<"decrypted text:";
for(i=1;i<numofrows+1;i++)
{
for(int j=0;j<len;j++)
{if(doubletrans[i][j]!='X')
{
cout<<doubletrans[i][j];
file11<<doubletrans[i][j];
}
}
}
cout<<'\n';
cout<<"Key Matrix File is 'keymatrix.txt' and Cipher Table File is 'ciphertable.txt'\n";
cout<<"Your File Has Been Dencrypted and Dencrypted Data Is Saved To File :"<<filename3<<"\n";
}
bool findchar(char charr)
{
int len=strlen(ciphers);
int t=0;
while(t<len)
{
if(ciphers[t]==charr)
{
return true;
break;
}
else t++;
}
return false;
}
int main()
{
ofstream file3;
ofstream file4;
ch=65;
temp[1]='\0';
for(int i=0;i<26;i++)
{
ciphers[i]='\0';
filename[i]='\0';
}
for( i=0;i<11;i++)
{
keyword[i]='\0';
keywordweights[i]='\0';
}
cout << "Enter Keyword(max characters 10 are allowed and all in upper case)";
cin.getline(keyword,10);
len=strlen(keyword);
for(i=0;i<26;i++)
{
for(int h=0;h<strlen(keyword);h++)
{
if(keyword[h]==ch)
{ flag3=1;
itoa(wt,temp,10);
keywordweights[h]=temp[0];
temp[0]='\0';
wt++;
}
}
ch++;
}
cout<<"Keyword Entered:"<<keyword<<"\n";
ciphers[0]=keyword[0];
y++;
ch=65;
for( i=1;i<len;i++)
{ if(false==findchar(keyword[i]))
{ciphers[y]=keyword[i];
y++;
}
}
for(i=0;i<26;i++)
{
if(false==findchar(ch))
{ ciphers[y]=ch;
y++;
}
ch++;
}
ch=97;
for(i=0;i<26;i++)
{
plaincipher[i][0]=ch;
plaincipher[i][1]=ciphers[i];
ch++;
}
file4.open("ciphertable.txt");
cout<<"Cipher Table:\n";
for(i=0;i<26;i++)
{
cout<<plaincipher[i][0];
file4<<plaincipher[i][0];
}
cout<<"\n";
for(i=0;i<26;i++)
{
cout<<plaincipher[i][1];
file4<<plaincipher[i][1];
}
cout<<"\n";
while(flag==0)
{
cout<<"Enter 'S' for Monoalphabetic Substitution Cipher and 'T' For Double Transposition Cipher:\n";
cin>>type;
if (type=='S')
flag=1;
else if(type=='T')
flag=2;
if(flag==1)
{
cout<<"You Selected Monoalphabetic Substitution Cipher!\n";
break;
}
else if(flag==2)
{
cout<<"You Selected Double Transposition Cipher!\n";
break;
}
else
cout<<"Wrong Input\n";
}
if(flag==1)
{
while(flag1==0)
{
cout<<"Enter 'E' for Encryption And 'D' for Decryption:";
cin>>ed;
if(ed=='E')
flag1=1;
else if(ed=='D')
flag1=2;
if (flag1==1 || flag1==2)
break;
else
cout<<"Wrong Input\n";
}
if(flag1==1)
{
cout<<"Enter Name Of the File You Want to Encrypt:";
cin>>filename;
cout<<"Name Of The File To Be Encrypted:"<<filename<<"\n";
ofstream file2;
file2.open("encryptedfile.txt");
file1.open(filename);
while (file1.good()) // loop while extraction from file is possible
{ l=0;
c = file1.get(); // get character from file
while(plaincipher[l][0]!=c)
{ l++;
}
if(l<26)
file2<<plaincipher[l][1];
// if (file1.good())
// cout << c;
}
file1.close(); // close file
cout<<" Cipher Table File is 'ciphertable.txt'\n";
cout<<"Your File Has Been Encrypted and Encrypted Data Is Saved To File :"<<filename2<<"\n";
}
if(flag1==2)
{
cout<<"Enter Name Of the File You Want to Dencrypt:";
cin>>filename;
cout<<"Name Of The File To Be Dencrypted:"<<filename<<"\n";
ofstream file2("dencryptedfile.txt");
file1.open(filename);
while (file1.good()) // loop while extraction from file is possible
{ l=0;
c = file1.get(); // get character from file
while(plaincipher[l][1]!=c)
{ l++;
}
if(l<26)
file2<<plaincipher[l][0];
// if (file1.good())
// cout << c;
}
file1.close(); // close file
cout<<" Cipher Table File is 'ciphertable.txt'\n";
cout<<"Your File Has Been Dencrypted and Dencrypted Data Is Saved To File :"<<filename3<<"\n";
}
}
else if(flag==2)
{
cout<<"Weights Assiged To Keyword:";
for(int h=0;h<strlen(keyword);h++)
cout<<keywordweights[h];
cout<<"\n";
while(flag1==0)
{
cout<<"Enter 'E' for Encryption And 'D' for Decryption:";
cin>>ed;
if(ed=='E')
flag1=1;
else if(ed=='D')
flag1=2;
if (flag1==1 || flag1==2)
break;
else
cout<<"Wrong Input\n";
}
if(flag1==1)
{
cout<<"Enter Name Of the File You Want to Encrypt:";
cin>>filename;
cout<<"Name Of The File To Be Encrypted:"<<filename<<"\n";
ofstream file2("encryptedfile.txt");
file1.open(filename);
while (file1.good()) // loop while extraction from file is possible
{ l=0;
c = file1.get(); // get character from file
count++;
}
file1.close();
count--;
mod=count%len;
while(mod!=0)
{
count++;
mod=count%len;
}
numofrows=count/len;
doubletrans=new char*[numofrows+1];
for( int i=0 ; i < numofrows+1 ; i++ )
doubletrans[i] = new char[col];
for(i=0;i<len;i++)
doubletrans[0][i]=keywordweights[i];
file1.open(filename);
int rr=1;
while (file1.good()) // loop while extraction from file is possible
{ l=0;
c = file1.get(); // get character from file
if(rr<numofrows+1 && col<len)
{doubletrans[rr][col]=c;
col++;
if(col==len)
{
rr++;
col=0;
}
}
}
col--;
while(col!=len)
{
doubletrans[rr][col]='X';
col++;
}
cout<<'\n';
file3.open("keymatrix.txt");
for(i=0;i<numofrows+1;i++)
{
for(int j=0;j<len;j++)
{ cout<<doubletrans[i][j];
file3<<doubletrans[i][j];
}
cout<<'\n';
file3<<'\n';
}
//cout<<"rows:"<<numofrows<<'\n';
file1.close(); // close file
ind=1;
for(int j=0;j<len;j++)
{itoa(ind,temp,10);
for(int i=0;i<len;)
{
if(doubletrans[0][i]!=temp[0])
i++;
else
{ ind++;
break;
}
}
for(int u=1;u<numofrows+1;u++)
{cout<<doubletrans[u][i];
file2<<doubletrans[u][i];
}
cout<<" ";
}
cout<<"Key Matrix File is 'keymatrix.txt' and Cipher Table File is 'ciphertable.txt'\n";
cout<<"Your File Has Been Encrypted and Encrypted Data Is Saved To File :"<<filename2<<"\n";
}
if(flag1==2)
{
decrypt();
}
}
return 0;
}
//****************************CPP FILE*************************************//www.google.com