It was not long time ago that I wrote a simple C/C++ program that uses XOR encryption/decryption ( the detailed post in this topic was interesting). However, I am not so good at programming, I'm just at intermediate or may be even at beginner level. Nowe, someone quoted form seeing the following code that I don't know nothing about escaping special characters, can any one point out
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<cstdio>
using namespace std;
void encryptdecrypt(string& data,string& key)
{
int keypos = 0;
for(int i = 0;i<data.length();i++)
{
data[i] = data[i] ^ key[keypos];
keypos++;
if(keypos >= key.length())
keypos = 0;
}
}
int main(int argc,char** argv)
{
string key = "";
char c;
string data = "";
if(argc == 1)
{
cout << "Usage: progname filename";
return 0;
}
cout << "Enter a password : ";
while((c = getchar()) != '\n' )
{
key += c;
}
FILE *fp;
fp = fopen(argv[1],"rb");
if(fp == NULL)
{
cout << "Sorry, File could not be opened.";
return 0;
}
while(1)
{
c = fgetc(fp);
if(feof(fp))
break;
else
data+=c;
}
encryptdecrypt(data,key);
fclose(fp);
FILE *fout;
fout = fopen(argv[1],"wb");
if(fout == NULL)
{
cout << "Sorry, File could not be opened.";
return 0;
}
for(int i = 0;i<data.length();i++)
fputc(data[i],fout);
fclose(fout);
getchar();
return 0;
}
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<cstdio>
using namespace std;
void encryptdecrypt(string& data,string& key)
{
int keypos = 0;
for(int i = 0;i<data.length();i++)
{
data[i] = data[i] ^ key[keypos];
keypos++;
if(keypos >= key.length())
keypos = 0;
}
}
int main(int argc,char** argv)
{
string key = "";
char c;
string data = "";
if(argc == 1)
{
cout << "Usage: progname filename";
return 0;
}
cout << "Enter a password : ";
while((c = getchar()) != '\n' )
{
key += c;
}
FILE *fp;
fp = fopen(argv[1],"rb");
if(fp == NULL)
{
cout << "Sorry, File could not be opened.";
return 0;
}
while(1)
{
c = fgetc(fp);
if(feof(fp))
break;
else
data+=c;
}
encryptdecrypt(data,key);
fclose(fp);
FILE *fout;
fout = fopen(argv[1],"wb");
if(fout == NULL)
{
cout << "Sorry, File could not be opened.";
return 0;
}
for(int i = 0;i<data.length();i++)
fputc(data[i],fout);
fclose(fout);
getchar();
return 0;
}
To copy to clipboard, switch view to plain text mode
Also, how can I improve this small program ...
Thanks.
Bookmarks