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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
#include <iostream>
#include <string>
//#include "crypto++/HASHCrypt.hpp"
#include <crypto++/aes.h>
#include <crypto++/stdcpp.h>
#include <crypto++/hex.h>
using CryptoPP::HexEncoder;
using CryptoPP::HexDecoder;
#include <crypto++/cryptlib.h>
using CryptoPP::BufferedTransformation;
using CryptoPP::AuthenticatedSymmetricCipher;
#include <crypto++/secblock.h>
using CryptoPP::SecByteBlock;
#include <crypto++/modes.h>
using CryptoPP::CFB_Mode;
#include <crypto++/filters.h>
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::AuthenticatedEncryptionFilter;
using CryptoPP::AuthenticatedDecryptionFilter;
#include <crypto++/osrng.h>
using CryptoPP::AutoSeededRandomPool;
#include <crypto++/aes.h>
using CryptoPP::AES;
#include <crypto++/md5.h>
#include <crypto++/gcm.h>
using CryptoPP::GCM;
using CryptoPP::GCM_TablesOption;
void getKI(std::string chaine,byte* key, byte* iv, int size){
CryptoPP::MD5 hash;
byte digest[ CryptoPP::MD5::DIGESTSIZE ];
std::string message = chaine;
hash.CalculateDigest( digest, (byte*) message.c_str(), message.length() );
CryptoPP::HexEncoder encoder;
std::string output;
encoder.Attach( new CryptoPP::StringSink( output ) );
encoder.Put( digest, sizeof(digest) );
encoder.MessageEnd();
for(int i=0; i<size;i++){
key[i]=output[i];
iv[i]=0x1;
}
// stub for how you really get it, e.g. reading it from a file, off of a network socket encrypted with an asymmetric cipher, or whatever
//read_key(aes_key, sizeof(aes_key));
// stub for how you really get it, e.g. filling it with random bytes or reading it from the other side of the socket since both sides have to use the same IV as well as the same key
//read_initialization_vector(iv);
// the final argument is specific to CFB mode, and specifies the refeeding size in bytes. This invocation corresponds to Java's Cipher.getInstance("AES/CFB8/NoPadding")
CryptoPP::CFB_Mode<CryptoPP::Rijndael>::Encryption* enc = new CFB_Mode<AES>::Encryption(key, size, iv, 1);
// the final argument is specific to CFB mode, and specifies the refeeding size in bytes. This invocation corresponds to Java's Cipher.getInstance("AES/CFB8/NoPadding")
CryptoPP::CFB_Mode<CryptoPP::Rijndael>::Decryption* dec = new CFB_Mode<AES>::Decryption(key, size, iv, 1);
}
void aff(std::string chaine);
//#include "crypto++/sha3.h"
int main(){
/* /home/loic/Documents/c/forgetIt/crypto++/HASHCrypt.cpp|21aff("------------------\n");
HASHCrypt monhash=HASHCrypt("loic");
aff(monhash.getMD5_128());
aff("\n");
aff("--------------\n");*/
std::string MessageS="Bonjours les amis, je vais être crypter !!!!";
char* Message=(char*)MessageS.c_str();
std::cout << Message << std::endl;
AutoSeededRandomPool rnd;
// Generate a random key
//SecByteBlock key(0x00, AES::DEFAULT_KEYLENGTH);
//rnd.GenerateBlock( key, key.size() );
// Generate a random IV
//byte iv[AES::BLOCKSIZE];
//rnd.GenerateBlock(iv, AES::BLOCKSIZE);
std::string cle;
aff("Entrez une clé de cryptage : ");
std::cin >> cle;
byte key[32];
byte iv[32];
getKI(cle,key,iv, sizeof(key));
int messageLen = (int)strlen(Message) + 1;
//////////////////////////////////////////////////////////////////////////
// Encrypt
CFB_Mode<AES>::Encryption cfbEncryption(key, sizeof(key), iv);
cfbEncryption.ProcessData((byte*)Message, (byte*)Message, messageLen);
while(not(cle=="exit")){
byte key2[32];
byte iv2[32];
MessageS="Bonjours les amis, je vais être crypter !!!!";
CFB_Mode<AES>::Encryption cfbEncryption(key, sizeof(key), iv);
cfbEncryption.ProcessData((byte*)Message, (byte*)Message, messageLen);
aff("Entrez une clé de décryptage : ");
std::cin >> cle;
getKI(cle,key2,iv2, sizeof(key));
//////////////////////////////////////////////////////////////////////////
// Decrypt
std::cout << std::endl << "Decryptage : " << std::endl;
CFB_Mode<AES>::Decryption cfbDecryption(key2,sizeof(key2), iv2);
cfbDecryption.ProcessData((byte*)Message, (byte*)Message, messageLen);
std::cout <<std::endl << Message << std::endl << std::endl;
}
return 0;
}
void aff(std::string chaine){
std::cout << chaine;
}
|