-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-decode.cpp
More file actions
69 lines (61 loc) · 1.91 KB
/
Copy pathtest-decode.cpp
File metadata and controls
69 lines (61 loc) · 1.91 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
#include <iostream>
#include <cryptopp/base64.h>
#include <cryptopp/rsa.h>
#include <cryptopp/sha.h>
#include <cryptopp/filters.h>
#include <cryptopp/hex.h>
#include <cryptopp/files.h>
#include <cryptopp/osrng.h>
#include <cryptopp/pem.h>
#include <string>
#include <fstream>
#include <sstream>
#include <bitset>
int main() {
// read in encrypted message
std::string decoded;
std::ifstream file("output_encrypted.txt");
std::stringstream buffer;
buffer << file.rdbuf();
std::string encoded = buffer.str();
// base64 decode
CryptoPP::Base64Decoder decoder;
decoder.Put( (CryptoPP::byte*)encoded.data(), encoded.size() );
decoder.MessageEnd();
CryptoPP::word64 size = decoder.MaxRetrievable();
if(size && size <= SIZE_MAX)
{
decoded.resize(size);
decoder.Get((CryptoPP::byte*)&decoded[0], decoded.size());
}
//std::cout << decoded << std::endl;
// load private key
CryptoPP::FileSource fs("quiz_pri.pem", true);
CryptoPP::RSA::PrivateKey key;
PEM_Load(fs, key);
// Decryption
CryptoPP::RSAES<CryptoPP::OAEP<CryptoPP::SHA256> >::Decryptor d(key);
CryptoPP::AutoSeededRandomPool rng;
/*
CryptoPP::SHA256 hash;
CryptoPP::byte digest[CryptoPP::SHA256::DIGESTSIZE];
hash.CalculateDigest(digest, (CryptoPP::byte*) decoded.c_str(), decoded.length() );
*/
// Create recovered text space
/*
size_t dpl = d.MaxPlaintextLength( decoded.size() );
CryptoPP::SecByteBlock recovered( 256 );
CryptoPP::DecodingResult result = d.Decrypt(rng, digest, 256, recovered );
recovered.resize( result.messageLength );
std::string str(reinterpret_cast<const char*>(&recovered[0]), recovered.size());
std::cout << str << std::endl;
*/
std::string recovered;
CryptoPP::StringSource ss2(decoded, true,
new CryptoPP::PK_DecryptorFilter(rng, d,
new CryptoPP::StringSink(recovered)
) // PK_DecryptorFilter
); // StringSource
std::cout << recovered << std::endl;
return 0;
}