/
PadEncrypt.cpp
124 lines (93 loc) · 2.07 KB
/
PadEncrypt.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<unistd.h>
#define null "\0"
using namespace std;
class PadEncryptor
{
private:
string filename;
string padname;
string secret;
ifstream is;
int fsize;
int filesize(string fn)
{
int a;
is.open(fn.c_str(),ios::binary);
is.seekg(0,ios::end);
a = is.tellg();
is.close();
return a;
}
public:
string text;
void Encrypt()
{
int padsize=filesize(padname);
fsize = text.length();
char* bufferText = new char [fsize];
if(padsize < text.length())
{
cout << "Pad size:"<<padsize<<" Text Size:"<<text.length(); exit(1);
}
if(text==null){
//the next series of commands puts text into text and secret
fsize = filesize(filename);
if(padsize < fsize)
{
cerr << "Pad file too small"<<padsize << fsize << endl;
}
is.open(filename.c_str(),ios::binary);
is.read(bufferText,fsize);
is.close();
text = (string) bufferText;
}
is.open(padname.c_str(),ios::binary);
//read from back
is.seekg(fsize,ios::beg);
is.read(bufferText,fsize);
secret= (string)bufferText;
is.close();
//xor the plain text to cipher
for(int i=0;i< fsize;i++)
{
text[i] ^= secret[i];
}
//remove the excess
truncate(padname.c_str(),padsize-fsize);
cout<<text;
}
void getFilename(string fname)
{
filename = fname;
}
void getPadname(string pname)
{
padname = pname;
}
void getText()
{
cin >> text;
}
};
int main(int argc , char* argv[])
{
PadEncryptor enc;
if(argc<2)
{
std::cout<<"Usage:"<<std::endl<<"\t"<<argv[0]<<"{input file} {pad file}"<<std::endl<<"\tpadencryptor {padfile}(and enter the text.)"<<endl;
}
if(argc == 2)
{
enc.getPadname(argv[1]);
enc.getText();
enc.Encrypt();
}
else if(argc ==3){
enc.getFilename(argv[1]);
enc.getPadname(argv[2]);
enc.Encrypt();}
return 1;
}