This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencrypt.cpp
137 lines (102 loc) · 3.3 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
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// 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);
// constrcut aes related object
CryptoPP::AES::Encryption _aes(key, 32);
aesEncryption = _aes;
CryptoPP::ECB_Mode_ExternalCipher::Encryption _ecb(aesEncryption, iv);
ecbEncryption = _ecb;
}
AESEncrypter::AESEncrypter(AESEncrypter &a) {
// copy the key
memcpy(key, a.key, 33);
// make another aes related object
CryptoPP::AES::Encryption _aes(key, 32);
aesEncryption = _aes;
CryptoPP::ECB_Mode_ExternalCipher::Encryption _ecb(aesEncryption, iv);
ecbEncryption = _ecb;
}
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::StreamTransformationFilter
stfEncryptor(ecbEncryption, 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);
CryptoPP::AES::Decryption _aes(key, 32);
aesDecryption = _aes;
CryptoPP::ECB_Mode_ExternalCipher::Decryption _ecb(aesDecryption, iv);
ecbDecryption = _ecb;
}
AESDecrypter::AESDecrypter(AESDecrypter &a) {
memcpy(key, a.key, 33);
CryptoPP::AES::Decryption _aes(key, 32);
aesDecryption = _aes;
CryptoPP::ECB_Mode_ExternalCipher::Decryption _ecb(aesDecryption, iv);
ecbDecryption = _ecb;
}
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::StreamTransformationFilter
stfDecryptor(ecbDecryption, new CryptoPP::StringSink(decrypted));
stfDecryptor.Put(reinterpret_cast<const unsigned char *>( cipher.c_str()),
cipher.length());
stfDecryptor.MessageEnd();
return decrypted;
}