1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
[code]#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//function prototypes
void mapitE(string,char[]);
void mapitD(string,char[]);
void encryptdecrypt(const string,const char[],int,string&);
int main()
{
string ende[]={"encrypt","decrypt"},buffer,key,newbuffer="";
char filename[80],enmap[128],demap[128],again;
int i,maplength;
ofstream out;
ifstream in;
int choice;
do
{
cout<<"Enter 1 to encrypt, 2 to decrypt: ";
cin>>choice;
while(choice<1||choice>2)
{cout<<"invalid choice\n";
cout<<"Enter 1 to encrypt, 2 to decrypt: ";
cin>>choice;
}
cout<<"Enter name of your file to "<<ende[choice-1]<<": ";
cin>>filename;
in.open(filename);
if(in.fail())
{ cout<<"input file did not open please check it\n";
system("pause");
return 1;
}
cout<<"Enter name of your output file: ";
cin>>filename;
out.open(filename);
cout<<"Enter your encryption key (max 128 characters): ";
cin>>key;
if(key.length()>128)
{cout<<"key too long\n";
cout<<"Enter your encryption key (max 128 characters): ";
cin>>key;
}
maplength=key.length();
if(choice==1)
mapitE(key,enmap);
else
mapitD(key,demap);
getline(in,buffer);
while(in)
{
if(choice==1)
encryptdecrypt(buffer,enmap,maplength,newbuffer);
else
encryptdecrypt(buffer,demap,maplength,newbuffer);
out<<newbuffer;
newbuffer.erase(0);
getline(in,buffer);
}
out.close();
in.close();
in.clear();
out.clear();
newbuffer.erase(0);
cout<<"do it again(y/n)? ";
cin>>again;
}while(toupper(again)=='Y');
return 0;
}
void encryptdecrypt(const string buffer,const char map[],int len,string& newbuffer)
{
int i=0;
char t,code;
for(i=0;i<buffer.length();i++)
{ cout<<buffer[i]<<" "<<i<<endl;
if(isalpha(buffer[i]))
{if(islower(buffer[i]))
code='a';
else
code='A';
t=buffer[i]-code;
cout<<code<<" "<<buffer[i]<<" "<<t<<" "<<i<<endl;
t=t+map[i%len];
t=t%26;
t=t+code;
newbuffer.push_back(t);
}
else
newbuffer.push_back(buffer[i]);
}
newbuffer.push_back('\n');
}
void mapitE(string key,char map[]) // Encrypts the file
{
int i;
for(i=0;i<key.length();i++)
map[i]=key[i]-'a';
}
void mapitD(string key,char map[]) // decrypts then encrypted file
{
int i;
for(i=0;i<key.length();i++)
map[i]=26-(key[i]-'a');
}
|