This repository was archived by the owner on May 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_some_cipher.cpp
More file actions
56 lines (49 loc) · 1.6 KB
/
test_some_cipher.cpp
File metadata and controls
56 lines (49 loc) · 1.6 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
#include <cstring>
#include <iostream>
#include <random>
extern "C" {
#include "some_cipher.h"
}
#define N 16777216
int main(int argc, char *argv[]) {
enum State{TST, ENC, DEC};
State s = TST;
if (argc == 2) {
if (std::strcmp(argv[1], "test") == 0) {
std::cout << "Testing " << N << " trials for reversibility of cipher." << std::endl;
} else if (std::strcmp(argv[1], "encrypt") == 0) {
std::cout << "Encrypting " << N << " plaintexts." << std::endl;
s = ENC;
} else if (std::strcmp(argv[1], "decrypt") == 0) {
std::cout << "Decrypting " << N << " ciphertexts." << std::endl;
s = DEC;
}
} else {
std::cerr << "usage: " << argv[0] << " test/encrypt/decrypt" << std::endl;
return 1;
}
std::random_device rd;
std::default_random_engine g(rd());
std::uniform_int_distribution<> d(0, 65535);
for (int i = 0; i < N; ++i) {
uint16_t key[3], pt[3];
for (int j = 0; j < 3; ++j) key[j] = d(g);
for (int j = 0; j < 3; ++j) pt[j] = d(g);
uint16_t ct[3], n_pt[3];
if (s == TST) {
encrypt(pt, ct, key);
decrypt(ct, n_pt, key);
for (int j = 0; j < 3; ++j) {
if (pt[j] != n_pt[j]) {
std::cout << "ERROR: Decrypted ciphertext does not match the plaintext." << std::endl;
return 1;
}
}
} else if (s == ENC) {
encrypt(pt, ct, key);
} else if (s == DEC) {
decrypt(pt, ct, key);
}
}
return 0;
}