-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa.cpp
More file actions
79 lines (62 loc) · 2.55 KB
/
Copy pathrsa.cpp
File metadata and controls
79 lines (62 loc) · 2.55 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
#include "cryptopp870/rsa.h"
#include "cryptopp870/osrng.h"
#include "cryptopp870/base64.h"
#include "cryptopp870/files.h"
#include "cryptopp870/hex.h"
#include <iostream>
#include <string>
CryptoPP::AutoSeededRandomPool rng;
void generate_keys(){
CryptoPP::RSA::PrivateKey privKey;
CryptoPP::RSA::PublicKey pubKey;
privKey.GenerateRandomWithKeySize(rng, 8192);
pubKey = CryptoPP::RSA::PublicKey(privKey);
std::cout << "Generation ended." << std::endl;
std::ofstream privFile("rsa-private.key", std::ios::binary);
CryptoPP::FileSink privSink(privFile);
privKey.DEREncode(privSink);
privFile.close();
std::ofstream pubFile("rsa-public.key", std::ios::binary);
CryptoPP::FileSink pubSink(pubFile);
pubKey.DEREncode(pubSink);
pubFile.close();
}
std::string rsa_encrypt(std::string to_encrypt){
CryptoPP::RSA::PublicKey pubKey;
std::ifstream pubFile("rsa-public.key", std::ios::binary);
CryptoPP::FileSource pubSource(pubFile, true);
pubKey.BERDecode(pubSource);
pubFile.close();
CryptoPP::RSAES_OAEP_SHA_Encryptor encryptor(pubKey);
std::string ciphered;
CryptoPP::StringSource(to_encrypt, true, new CryptoPP::PK_EncryptorFilter(rng, encryptor, new CryptoPP::StringSink(ciphered)));
std::string encoded_ciphered;
CryptoPP::StringSource(ciphered, true, new CryptoPP::Base64Encoder(new CryptoPP::StringSink(encoded_ciphered)));
std::cout << "Encoded ciphered : " << encoded_ciphered << std::endl;
return encoded_ciphered;
}
std::string rsa_decrypt(std::string to_decrypt){
CryptoPP::RSA::PrivateKey privKey;
std::ifstream privFile("rsa-private.key", std::ios::binary);
CryptoPP::FileSource privSource(privFile, true);
privKey.BERDecode(privSource);
privFile.close();
std::string decoded_ciphered;
CryptoPP::StringSource(to_decrypt, true, new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded_ciphered)));
std::string recovered;
CryptoPP::RSAES_OAEP_SHA_Decryptor decryptor(privKey);
CryptoPP::StringSource(decoded_ciphered, true, new CryptoPP::PK_DecryptorFilter(rng, decryptor, new CryptoPP::StringSink(recovered)));
return recovered;
}
int main(int argc, char* argv[]) {
if (argc > 1) {
std::string first_arg(argv[1]);
std::cout << "Text to encrypt : " << first_arg << std::endl;
} else {
std::cout << "Nothing to encrypt." << std::endl;
}
generate_keys();
std::string encrypted = rsa_encrypt(argv[1]);
std::cout << "Decoded cipher : " << rsa_decrypt(encrypted) << std::endl;
return 0;
}