-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest.cpp
More file actions
116 lines (94 loc) · 4.02 KB
/
test.cpp
File metadata and controls
116 lines (94 loc) · 4.02 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
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
//* This file is for testing the Crypto++ library and check we are able to use it to do want we want to
#include <string>
#include <iostream>
#include "cryptlib.h"
#include "osrng.h"
#include "sha.h"
#include "aes.h"
#include "hex.h"
#include "integer.h"
#include "pwdbased.h"
#include "modes.h"
// Procedure to do:
// take the user password
// key = sha1(sha1(password))
// stored_password = DES(message=key, key, iv)
// store the stored_password AND the IV
// When checking if the password provided is the right one:
// take the password -> sha1 -> sha1
// try to decrypt the stored_password with this key and the salt
// if stored_password == sha1(sha1(given_password)) => OK, sinon KO
// Then unencrypt things in memory...
const std::string HEXSALT = "E5579A8EC1FB90310103718E4DF80495";
const std::string HEXIV = "B56E5AC5D1F2E4873E7F408C51F4ED52";
int main(int argc, char * argv[])
{
using namespace std;
using namespace CryptoPP;
string password = "";
unsigned int iterations = 1;
// AutoSeededX917RNG<AES> rng;
// SecByteBlock iv(AES::BLOCKSIZE);
// rng.GenerateBlock(iv,iv.size());
// See NIST SP 800-132 for detailed recommendations on length, generation and
// format of the salt. This test program will just generate a random one. That
// might not be sufficient for every application.
// SecByteBlock pwsalt(AES::DEFAULT_KEYLENGTH);
// rng.GenerateBlock(pwsalt,pwsalt.size());
SecByteBlock recoveredsalt(AES::DEFAULT_KEYLENGTH);
StringSource saltDecoder(HEXSALT,true,new HexDecoder(new ArraySink(recoveredsalt, recoveredsalt.size())));
SecByteBlock recoverediv(AES::BLOCKSIZE);
StringSource ivDecoder(HEXIV,true,new HexDecoder(new ArraySink(recoverediv, recoverediv.size())));
SecByteBlock derivedkey(AES::DEFAULT_KEYLENGTH);
cout << "Password is " << password << endl;
cout << "Deriving key from password:" << endl;
PKCS5_PBKDF2_HMAC<SHA256> pbkdf;
pbkdf.DeriveKey(
// buffer that holds the derived key
derivedkey, derivedkey.size(),
// purpose byte. unused by this PBKDF implementation.
0x00,
// password bytes. careful to be consistent with encoding...
(byte *) password.data(), password.size(),
// salt bytes
recoveredsalt, recoveredsalt.size(),
// iteration count. See SP 800-132 for details. You want this as large as you can tolerate.
// make sure to use the same iteration count on both sides...
iterations
);
cout << "Done" << endl;
string message = "ceciestuntest";
string ciphertext;
CBC_Mode<AES>::Encryption aesencryption(derivedkey,derivedkey.size(), recoverediv);
// encrypt message using key derived above, storing the hex encoded result into ciphertext
StringSource encryptor(message,true,
new StreamTransformationFilter(aesencryption, new HexEncoder( new StringSink(ciphertext)))
);
// hex encode salt and IV for "transport"
// string hexsalt, hexiv;
// ArraySource saltEncoder(pwsalt,pwsalt.size(), true, new HexEncoder(new StringSink(hexsalt)));
// ArraySource ivEncoder(iv,iv.size(), true, new HexEncoder(new StringSink(hexiv)));
cout << "Salt: " << HEXSALT << endl;
cout << "IV: " << HEXIV << endl;
cout << "Ciphertext: " << ciphertext << endl;
// now recover the plain text given the password, salt, IV and ciphertext
SecByteBlock recoveredkey(AES::DEFAULT_KEYLENGTH);
cout << "Re-deriving encryption key based on encoded values above." << endl;
pbkdf.DeriveKey(recoveredkey, recoveredkey.size(), 0x00, (byte *) password.data(), password.size(),
recoveredsalt, recoveredsalt.size(), iterations);
cout << "Done." << endl;
CBC_Mode<AES>::Decryption aesdecryption(recoveredkey, recoveredkey.size(), recoverediv);
string recoveredtext;
cout << "-------------------------" << endl;
ciphertext = "F4302E98C80FC97FE72BA52CFF810B5C";
cout << "Ciphertext: " << ciphertext << endl;
try {
StringSource decryptor(ciphertext, true, new HexDecoder(
new StreamTransformationFilter(aesdecryption, new StringSink(recoveredtext))
));
} catch(Exception e) {
cerr << "\nException raised: " << e.what() << endl;
}
cout << "Recovered plaintext value: " << recoveredtext << endl;
return 0;
}