-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSA.cpp
More file actions
178 lines (145 loc) · 3.92 KB
/
Copy pathRSA.cpp
File metadata and controls
178 lines (145 loc) · 3.92 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
#include <iostream>
#include <string>
#include <assert.h>
#include "rsa.h"
#include "aes.h"
#include "hex.h"
#include "files.h"
#include "osrng.h"
#ifdef USE_BOOST_FILESYSTEM
# include <boost/filesystem/path.hpp>
# include <boost/filesystem/operations.hpp>
namespace fs = boost::filesystem;
#else
# include <filesystem>
# ifdef FILESYSTEM_EXPERIENTAL
namespace fs = std::experimental::filesystem;
# else
namespace fs = std::filesystem;
# endif
#endif
void encode(
fs::path const& filepath,
CryptoPP::BufferedTransformation const& bt)
{
CryptoPP::FileSink file(filepath.c_str());
bt.CopyTo(file);
file.MessageEnd();
}
void encode_private_key(
fs::path const& filepath,
CryptoPP::RSA::PrivateKey const& key)
{
CryptoPP::ByteQueue queue;
key.DEREncodePrivateKey(queue);
encode(filepath, queue);
}
void encode_public_key(
fs::path const& filepath,
CryptoPP::RSA::PublicKey const& key)
{
CryptoPP::ByteQueue queue;
key.DEREncodePublicKey(queue);
encode(filepath, queue);
}
void decode(
fs::path const& filepath,
CryptoPP::BufferedTransformation& bt)
{
CryptoPP::FileSource file(filepath.c_str(), true);
file.TransferTo(bt);
bt.MessageEnd();
}
void decode_private_key(
fs::path const& filepath,
CryptoPP::RSA::PrivateKey& key)
{
CryptoPP::ByteQueue queue;
decode(filepath, queue);
key.BERDecodePrivateKey(queue, false, queue.MaxRetrievable());
}
void decode_public_key(
fs::path const& filepath,
CryptoPP::RSA::PublicKey& key)
{
CryptoPP::ByteQueue queue;
decode(filepath, queue);
key.BERDecodePublicKey(queue, false, queue.MaxRetrievable());
}
void rsa_sign_file(
fs::path const& filepath,
fs::path const& privateKeyPath,
fs::path const& signaturePath,
CryptoPP::RandomNumberGenerator& rng)
{
CryptoPP::RSA::PrivateKey privateKey;
decode_private_key(privateKeyPath, privateKey);
CryptoPP::RSASSA_PKCS1v15_SHA_Signer signer(privateKey);
CryptoPP::FileSource fileSource(
filepath.c_str(),
true,
new CryptoPP::SignerFilter(
rng,
signer,
new CryptoPP::FileSink(
signaturePath.c_str())));
}
bool rsa_verify_file(
fs::path const& filepath,
fs::path const& publicKeyPath,
fs::path const& signaturePath)
{
CryptoPP::RSA::PublicKey publicKey;
decode_public_key(publicKeyPath.c_str(), publicKey);
CryptoPP::RSASSA_PKCS1v15_SHA_Verifier verifier(publicKey);
CryptoPP::FileSource signatureFile(
signaturePath.c_str(),
true);
if (signatureFile.MaxRetrievable() != verifier.SignatureLength())
return false;
CryptoPP::SecByteBlock signature(verifier.SignatureLength());
signatureFile.Get(signature, signature.size());
auto* verifierFilter = new CryptoPP::SignatureVerificationFilter(verifier);
verifierFilter->Put(signature, verifier.SignatureLength());
CryptoPP::FileSource fileSource(
filepath.c_str(),
true,
verifierFilter);
return verifierFilter->GetLastResult();
}
void generate_keys(
fs::path const& privateKeyPath,
fs::path const& publicKeyPath,
CryptoPP::RandomNumberGenerator& rng)
{
try
{
CryptoPP::RSA::PrivateKey rsaPrivate;
rsaPrivate.GenerateRandomWithKeySize(rng, 3072);
CryptoPP::RSA::PublicKey rsaPublic(rsaPrivate);
encode_private_key(privateKeyPath, rsaPrivate);
encode_public_key(publicKeyPath, rsaPublic);
}
catch (CryptoPP::Exception const& e)
{
std::cerr << e.what() << std::endl;
}
}
int main()
{
CryptoPP::AutoSeededRandomPool rng;
generate_keys(
"rsa-private.key",
"rsa-public.key",
rng);
rsa_sign_file(
"sample.txt",
"rsa-private.key",
"sample.sign",
rng);
auto success = rsa_verify_file(
"sample.txt",
"rsa-public.key",
"sample.sign");
assert(success);
}