-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcryptopp-key-encode.cpp
More file actions
178 lines (129 loc) 路 4.64 KB
/
Copy pathcryptopp-key-encode.cpp
File metadata and controls
178 lines (129 loc) 路 4.64 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
// Linux help: http://www.cryptopp.com/wiki/Linux
// Debug:
// g++ -g3 -ggdb -O0 -Wall -Wextra -Wno-unused -Wno-type-limits -I. -I/usr/include/cryptopp cryptopp-key-encode.cpp -o cryptopp-key-encode.exe -lcryptopp
// Release:
// g++ -O2 -Wall -Wextra -Wno-unused -Wno-type-limits -I. -I/usr/include/cryptopp cryptopp-key-encode.cpp -o cryptopp-key-encode.exe -lcryptopp && strip --strip-all cryptopp-key-encode.exe
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#include <string>
using std::string;
#include <stdexcept>
using std::runtime_error;
#include <cryptopp/queue.h>
using CryptoPP::ByteQueue;
#include <cryptopp/files.h>
using CryptoPP::FileSource;
using CryptoPP::FileSink;
#include "cryptopp/dsa.h"
using CryptoPP::DSA;
#include "cryptopp/rsa.h"
using CryptoPP::RSA;
#include <cryptopp/cryptlib.h>
using CryptoPP::PrivateKey;
using CryptoPP::PublicKey;
using CryptoPP::BufferedTransformation;
#include "cryptopp/osrng.h"
using CryptoPP::AutoSeededRandomPool;
void EncodePrivateKey(const string& filename, const RSA::PrivateKey& key);
void EncodePublicKey(const string& filename, const RSA::PublicKey& key);
void Encode(const string& filename, const BufferedTransformation& bt);
void DecodePrivateKey(const string& filename, RSA::PrivateKey& key);
void DecodePublicKey(const string& filename, RSA::PublicKey& key);
void Decode(const string& filename, BufferedTransformation& bt);
int main(int argc, char** argv)
{
std::ios_base::sync_with_stdio(false);
// http://www.cryptopp.com/docs/ref/class_auto_seeded_random_pool.html
AutoSeededRandomPool rnd;
try
{
// http://www.cryptopp.com/docs/ref/rsa_8h.html
RSA::PrivateKey rsaPrivate;
rsaPrivate.GenerateRandomWithKeySize(rnd, 3072);
RSA::PublicKey rsaPublic(rsaPrivate);
EncodePrivateKey("rsa-private.key", rsaPrivate);
EncodePublicKey("rsa-public.key", rsaPublic);
cout << "Successfully generated and saved RSA keys" << endl;
////////////////////////////////////////////////////////////////////////////////////
RSA::PrivateKey k1;
DecodePrivateKey("rsa-private.key", k1);
RSA::PublicKey k2;
DecodePublicKey("rsa-public.key", k2);
cout << "Successfully loaded RSA keys" << endl;
////////////////////////////////////////////////////////////////////////////////////
if(!k1.Validate(rnd, 3))
throw runtime_error("Rsa private key validation failed");
if(!k2.Validate(rnd, 3))
throw runtime_error("Rsa public key validation failed");
cout << "Successfully validated RSA keys" << endl;
////////////////////////////////////////////////////////////////////////////////////
if(rsaPrivate.GetModulus() != k1.GetModulus() ||
rsaPrivate.GetPublicExponent() != k1.GetPublicExponent() ||
rsaPrivate.GetPrivateExponent() != k1.GetPrivateExponent())
{
throw runtime_error("private key data did not round trip");
}
if(rsaPublic.GetModulus() != k2.GetModulus() ||
rsaPublic.GetPublicExponent() != k2.GetPublicExponent())
{
throw runtime_error("public key data did not round trip");
}
cout << "Successfully round-tripped RSA keys" << endl;
////////////////////////////////////////////////////////////////////////////////////
}
catch(CryptoPP::Exception& e)
{
cerr << e.what() << endl;
return -2;
}
catch(std::exception& e)
{
cerr << e.what() << endl;
return -1;
}
return 0;
}
void EncodePrivateKey(const string& filename, const RSA::PrivateKey& key)
{
// http://www.cryptopp.com/docs/ref/class_byte_queue.html
ByteQueue queue;
key.DEREncodePrivateKey(queue);
Encode(filename, queue);
}
void EncodePublicKey(const string& filename, const RSA::PublicKey& key)
{
// http://www.cryptopp.com/docs/ref/class_byte_queue.html
ByteQueue queue;
key.DEREncodePublicKey(queue);
Encode(filename, queue);
}
void Encode(const string& filename, const BufferedTransformation& bt)
{
// http://www.cryptopp.com/docs/ref/class_file_sink.html
FileSink file(filename.c_str());
bt.CopyTo(file);
file.MessageEnd();
}
void DecodePrivateKey(const string& filename, RSA::PrivateKey& key)
{
// http://www.cryptopp.com/docs/ref/class_byte_queue.html
ByteQueue queue;
Decode(filename, queue);
key.BERDecodePrivateKey(queue, false /*optParams*/, queue.MaxRetrievable());
}
void DecodePublicKey(const string& filename, RSA::PublicKey& key)
{
// http://www.cryptopp.com/docs/ref/class_byte_queue.html
ByteQueue queue;
Decode(filename, queue);
key.BERDecodePublicKey(queue, false /*optParams*/, queue.MaxRetrievable());
}
void Decode(const string& filename, BufferedTransformation& bt)
{
// http://www.cryptopp.com/docs/ref/class_file_source.html
FileSource file(filename.c_str(), true /*pumpAll*/);
file.TransferTo(bt);
bt.MessageEnd();
}