-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSA_OAEP.cpp
More file actions
190 lines (167 loc) 路 4.93 KB
/
Copy pathRSA_OAEP.cpp
File metadata and controls
190 lines (167 loc) 路 4.93 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Sample.cpp
#include "cryptopp/rsa.h"
using CryptoPP::RSA;
using CryptoPP::InvertibleRSAFunction;
using CryptoPP::RSAES_OAEP_SHA_Encryptor;
using CryptoPP::RSAES_OAEP_SHA_Decryptor;
#include "cryptopp/sha.h"
using CryptoPP::SHA1;
#include "cryptopp/filters.h" //string input, output
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::BufferedTransformation;
using CryptoPP::PK_EncryptorFilter;
using CryptoPP::PK_DecryptorFilter;
#include "cryptopp/files.h" // File input, output
using CryptoPP::FileSink;
using CryptoPP::FileSource;
#include "cryptopp/osrng.h" // random generator
using CryptoPP::AutoSeededRandomPool;
#include "cryptopp/SecBlock.h" // byte in CryptoPP
using CryptoPP::SecByteBlock;
// using CryptoPP::byte;
#include "cryptopp/cryptlib.h"
using CryptoPP::PrivateKey;
using CryptoPP::PublicKey;
using CryptoPP::BufferedTransformation;
using CryptoPP::Exception;
using CryptoPP::DecodingResult;
#include "cryptopp/hex.h"
using CryptoPP::HexEncoder;
using CryptoPP::HexDecoder;
#include <string>
using std::wstring;
using std::string;
#include <exception>
using std::exception;
#include <iostream>
using std::wcin;
using std::wcout;
using std::cerr;
using std::endl;
#include <assert.h>
/* Set _setmode()*/
#ifdef _WIN32
#include <io.h>
#elif __linux__
#include <inttypes.h>
#include <unistd.h>
#define __int64 int64_t
#define _close close
#define _read read
#define _lseek64 lseek64
#define _O_RDONLY O_RDONLY
#define _open open
#define _lseeki64 lseek64
#define _lseek lseek
#define stricmp strcasecmp
#endif
#include <fcntl.h>
/* Convert string*/
#include <locale>
using std::wstring_convert;
#include <codecvt>
using std::codecvt_utf8;
wstring string_to_wstring (const std::string& str);
string wstring_to_string (const std::wstring& str);
/*Reading key from file*/
#include <cryptopp/queue.h>
using CryptoPP::ByteQueue;
#include <cryptopp/files.h>
using CryptoPP::FileSource;
using CryptoPP::FileSink;
void Load(const string& filename, BufferedTransformation& bt);
void LoadPrivateKey(const string& filename, PrivateKey& key);
void LoadPublicKey(const string& filename, PublicKey& key);
int main(int argc, char* argv[])
{
try
{
/*Set mode support Vietnamese*/
#ifdef __linux__
setlocale(LC_ALL,"");
#elif _WIN32
_setmode(_fileno(stdin), _O_U16TEXT);
_setmode(_fileno(stdout), _O_U16TEXT);
#else
#endif
// Generate keys
string encoded;
AutoSeededRandomPool rng;
InvertibleRSAFunction parameters;
parameters.GenerateRandomWithKeySize(rng, 3072);
RSA::PrivateKey privateKey;
RSA::PublicKey publicKey;
LoadPrivateKey("rsa-private.key", privateKey);
LoadPublicKey("rsa-public.key", publicKey);
/* Pretty print private key */
wstring wplain;
string plain, cipher, recovered;
wcout <<"Input plaintext: ";
getline(wcin,wplain);
plain=wstring_to_string(wplain);
wcout << "plaintext: " << wplain << endl;
////////////////////////////////////////////////
// Encryption
RSAES_OAEP_SHA_Encryptor e(publicKey);
StringSource( plain, true,
new PK_EncryptorFilter( rng, e,
new StringSink( cipher )
) // PK_EncryptorFilter
); // StringSource
/* Pretty Print cipher text */
encoded.clear();
StringSource(cipher, true,
new HexEncoder(
new StringSink(encoded)
) // HexEncoder
); // StringSource
wcout << "cipher text: "<<string_to_wstring(encoded)<< endl;
////////////////////////////////////////////////
// Decryption
RSAES_OAEP_SHA_Decryptor d( privateKey );
StringSource( cipher, true,
new PK_DecryptorFilter( rng, d,
new StringSink( recovered )
) // PK_EncryptorFilter
); // StringSource
wcout << "recovered text:" << string_to_wstring(recovered) << endl;
assert( plain == recovered );
}
catch( CryptoPP::Exception& e )
{
cerr << "Caught Exception..." << endl;
cerr << e.what() << endl;
}
return 0;
}
/* convert string to wstring */
wstring string_to_wstring (const std::string& str)
{
wstring_convert<codecvt_utf8<wchar_t>> towstring;
return towstring.from_bytes(str);
}
/* convert wstring to string */
string wstring_to_string (const std::wstring& str)
{
wstring_convert<codecvt_utf8<wchar_t>> tostring;
return tostring.to_bytes(str);
}
void LoadPrivateKey(const string& filename, PrivateKey& key)
{
ByteQueue queue;
Load(filename, queue);
key.Load(queue);
}
void LoadPublicKey(const string& filename, PublicKey& key)
{
ByteQueue queue;
Load(filename, queue);
key.Load(queue);
}
void Load(const string& filename, BufferedTransformation& bt)
{
FileSource file(filename.c_str(), true /*pumpAll*/);
file.TransferTo(bt);
bt.MessageEnd();
}