-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcrypt.cpp
More file actions
74 lines (57 loc) · 1.74 KB
/
crypt.cpp
File metadata and controls
74 lines (57 loc) · 1.74 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
#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
#include <cryptopp/base64.h>
#include "crypt.h"
// ---------------------------------------------------------------------
std::string aes_encrypt(const std::string& str_in, const std::string& key)
{
std::string str_out;
CryptoPP::ECB_Mode< CryptoPP::AES >::Encryption e;
e.SetKey((byte*)key.c_str(), key.length());
CryptoPP::StringSource encryptor(str_in, true,
new CryptoPP::StreamTransformationFilter(e,
new CryptoPP::Base64Encoder(
new CryptoPP::StringSink(str_out),
false // do not append a newline
)
)
);
return str_out;
}
// ---------------------------------------------------------------------
std::string aes_decrypt(const std::string& str_in, const std::string& key)
{
std::string str_out;
CryptoPP::ECB_Mode< CryptoPP::AES >::Decryption d;
d.SetKey((byte*)key.c_str(), key.length());
CryptoPP::StringSource decryptor(str_in, true,
new CryptoPP::Base64Decoder(
new CryptoPP::StreamTransformationFilter(d,
new CryptoPP::StringSink(str_out)
)
)
);
return str_out;
}
// ---------------------------------------------------------------------
std::string base64_encode(std::string string)
{
std::string encoded;
CryptoPP::StringSource ss(string, true,
new CryptoPP::Base64Encoder(
new CryptoPP::StringSink(encoded)
)
);
return encoded;
}
// ---------------------------------------------------------------------
std::string base64_decode(std::string encoded)
{
std::string decoded;
CryptoPP::StringSource ss(encoded, true,
new CryptoPP::Base64Decoder(
new CryptoPP::StringSink(decoded)
)
);
return decoded;
}