forked from LDLDL/FileUploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.cpp
124 lines (88 loc) · 2.97 KB
/
encrypt.cpp
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
//
// Created by TYTY on 2019-06-03 003.
//
#include "encrypt.h"
using namespace encrypt;
// Encrypter implementation
AESEncrypter::AESEncrypter(const string &_key) {
if (_key.length() < 7) {
LOG(WARNING) << "You are using a short key which is weak to attack.";
}
// scrypt algorithm
CryptoPP::Scrypt keygen;
keygen.DeriveKey(key, 32, (byte *) _key.c_str(), _key.size(), salt, 32);
aesEncryption.SetKeyWithIV(key, CryptoPP::AES::MAX_KEYLENGTH, iv, CryptoPP::AES::BLOCKSIZE);
}
AESEncrypter::AESEncrypter(AESEncrypter &a) {
// copy the key
memcpy(key, a.key, 33);
aesEncryption.SetKeyWithIV(key, CryptoPP::AES::MAX_KEYLENGTH, iv, CryptoPP::AES::BLOCKSIZE);
}
void AESEncrypter::showkey() {
// dump as hex
CryptoPP::HexEncoder encoder;
std::string output;
encoder.Attach(new CryptoPP::StringSink(output));
encoder.Put(key, sizeof(key));
encoder.MessageEnd();
LOG(DEBUG) << "Now using key: " << output;
}
void AESEncrypter::showiv() {
// dump as hex
CryptoPP::HexEncoder encoder;
std::string output;
encoder.Attach(new CryptoPP::StringSink(output));
encoder.Put(iv, sizeof(iv));
encoder.MessageEnd();
LOG(DEBUG) << "Now using iv: " << output;
}
string AESEncrypter::encrypt(const string &plain) {
string encrypted;
CryptoPP::AuthenticatedEncryptionFilter stfEncryptor(
aesEncryption, new CryptoPP::StringSink(encrypted));
// not that in encryption we need to give plain.length() + 1
// while in decryption we not
stfEncryptor.Put(reinterpret_cast<const unsigned char *>( plain.c_str()),
plain.length() + 1);
stfEncryptor.MessageEnd();
return encrypted;
}
// Decrypter implementation
// Implementation is very likely to Encrypter
AESDecrypter::AESDecrypter(const string &_key) {
// scrypt
CryptoPP::Scrypt keygen;
keygen.DeriveKey(key, 32, (byte *) _key.c_str(), _key.size(), salt, 32);
aesDecryption.SetKeyWithIV(key, CryptoPP::AES::MAX_KEYLENGTH, iv, CryptoPP::AES::BLOCKSIZE);
}
AESDecrypter::AESDecrypter(AESDecrypter &a) {
memcpy(key, a.key, 33);
aesDecryption.SetKeyWithIV(key, CryptoPP::AES::MAX_KEYLENGTH, iv, CryptoPP::AES::BLOCKSIZE);
}
void AESDecrypter::showkey() {
// dump as hex
CryptoPP::HexEncoder encoder;
std::string output;
encoder.Attach(new CryptoPP::StringSink(output));
encoder.Put(key, sizeof(key));
encoder.MessageEnd();
LOG(DEBUG) << "Now using key: " << output;
}
void AESDecrypter::showiv() {
// dump as hex
CryptoPP::HexEncoder encoder;
std::string output;
encoder.Attach(new CryptoPP::StringSink(output));
encoder.Put(iv, sizeof(iv));
encoder.MessageEnd();
LOG(DEBUG) << "Now using iv: " << output;
}
string AESDecrypter::decrypt(const string &cipher) {
string decrypted;
CryptoPP::AuthenticatedEncryptionFilter stfDecryptor(
aesDecryption, new CryptoPP::StringSink(decrypted));
stfDecryptor.Put(reinterpret_cast<const unsigned char *>( cipher.c_str()),
cipher.length());
stfDecryptor.MessageEnd();
return decrypted;
}