-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTest.cpp
106 lines (70 loc) · 2.5 KB
/
Test.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
#include <algorithm>
#include "BCryptWrap.hpp"
#include <iostream>
#include <string>
#include <windows.h>
std::ostream& operator<<(std::ostream& o, nk125::bytes& bs) {
std::copy(bs.begin(), bs.end(), std::ostream_iterator<unsigned char>(o));
return o;
}
void asHex(nk125::bytes& bs) {
int flags = std::cout.flags();
std::cout << std::hex;
std::copy(bs.begin(), bs.end(), std::ostream_iterator<unsigned int>(std::cout));
std::cout.setf(flags);
return;
}
void encdecTest(nk125::BCryptWC& bc, nk125::bytes& bufa, nk125::bytes& bufb) {
bufb = bufa;
std::cout << " = Plain Text: ";
asHex(bufb);
std::cout << " =\n\n";
std::cout << "Encrypting...\n";
bc.encrypt(bufb, true);
std::cout << " = Cipher Text: ";
asHex(bufb);
std::cout << " =\n\n";
std::cout << "Decrypting...\n";
bc.decrypt(bufb, true);
std::cout << " = Recovered : ";
asHex(bufb);
std::cout << " =\n";
std::cout << "Encryption/Decryption routine " << (bufb == bufa ? "passed succesfully" : "failed") << "\n";
}
int main() {
nk125::BCryptWC bcEnc;
try {
bcEnc.init(bcEnc.AES128);
std::cout << "Initialized AES-128\n";
bcEnc.genKey();
std::cout << "Generated AES 128 bits password (ECB, no IV)\n";
std::string myPassStr = "hai iamapassword", con = "Plaintext";
nk125::bytes genPass = bcEnc.exportKey(), myPass(myPassStr.begin(), myPassStr.end()), a(con.begin(), con.end()), b;
std::cout << "Generated password: ";
asHex(genPass);
std::cout << "\n";
std::cout << "Hardcoded password: " << myPass << "\n";
bcEnc.preventKeyStorage();
std::cout << "exportKey() returns empty after preventKeyStorage()?: " << (bcEnc.exportKey().empty() ? "true" : "false") << "\n";
std::cout << "Content to encrypt: " << con << " (";
asHex(a);
std::cout << ")\n";
auto testBothKeys = [&]() {
bcEnc.importKey(genPass);
std::cout << "\nTesting encryption/decryption with generated key...\n";
encdecTest(bcEnc, a, b);
std::cout << "\nImporting hardcoded key...\n";
bcEnc.importKey(myPass);
std::cout << "Testing encryption/decryption with hardcoded key...\n";
encdecTest(bcEnc, a, b);
};
testBothKeys();
std::cout << "\n = Testing with initOSR() function =\n";
bcEnc.initOSR(nk125::BCryptWC::CipherAlgorithm(BCRYPT_AES_ALGORITHM, 128));
testBothKeys();
std::cout << "All tests passed succesfully!\n";
}
catch (nk125::BCryptWC::Exception& e) {
std::cerr << "Error: " << e.what() << "\n";
}
}