Initial commit
[aes.git] / aes_test.cpp
1 #include "aes.hpp"
2 #include <iostream>
3 #include <fstream>
4
5 static void print_by_16s (const byteArray b, std::string name)
6 {
7         int i, j;
8
9         std::cout << "Printing: " << name << "\n" << std::endl;
10
11         for (i=0; i<b.size()/16; ++i)
12         {
13                 for (j=0; j<16; ++j)
14                         std::printf ("%.2x ", b.at(i*16+j));
15
16                 std::printf ("\n");
17         }
18 }
19
20
21 int main (int argc, char *argv[])
22 {
23
24 //#define DEBUG 1
25 //#define MY_TEST2 1
26 //#define PROBLEM2 1
27 //#define PROBLEM3 1
28
29 #if PROBLEM2
30         int i;
31
32         /* Read the Key */
33         byteArray key;
34
35         for (i=0; i<16; ++i)
36                 key.push_back (getchar());
37
38         /* Create the AES object */
39         AES aes (key);
40
41         /* Read all characters of input */
42         byteArray input;
43
44         for (char c=getchar(); c != EOF; c=getchar())
45         {
46                 input.push_back (c);
47
48                 if (input.size() == 16)
49                 {
50                         byteArray ciphertext = aes.encrypt (input);
51                         byteArray plaintext  = aes.decrypt (ciphertext);
52
53                         /* Print it */
54                         for (i=0; i<plaintext.size(); ++i)
55                                 std::printf ("%c", plaintext.at(i));
56
57                         input.clear();
58                 }
59
60         }
61 #endif // PROBLEM2
62
63 #if PROBLEM3
64         std::ifstream fin ("Problem3.out", ios::binary);
65         int i;
66
67         byteArray key;
68
69         for (i=0; i<24; ++i)
70                 key.push_back (fin.get());
71
72         /* Create the AES Object */
73         AES aes (key);
74
75         byteArray input;
76
77         /* Read the input and decrypt */
78         for (char c=fin.get(); !fin.eof(); c=fin.get())
79         {
80                 input.push_back (c);
81
82                 if (input.size() == 16)
83                 {
84                         byteArray plaintext = aes.decrypt (input);
85
86                         /* Print it */
87                         for (i=0; i<plaintext.size(); ++i)
88                                 std::printf ("%c", plaintext.at(i));
89
90                 input.clear();
91                 }
92         }
93 #endif //PROBLEM3
94
95 #if MY_TEST
96         const int inputSize = 4096;
97
98         byteArray key (16, 2);
99         byteArray input (inputSize, 0);
100         int i, j;
101
102         for (i=0; i<inputSize; ++i)
103                 input.at (i) = i;
104
105         AES aes (key);
106
107         print_by_16s (input, "Input");
108
109         byteArray ciphertext = aes.encrypt (input);
110
111         print_by_16s (ciphertext, "Ciphertext");
112
113         byteArray plaintext  = aes.decrypt (ciphertext);
114
115         print_by_16s (plaintext, "Plaintext");
116 #endif
117
118 #if MY_TEST2
119         int i;
120
121         byteArray key (16, 0);
122
123         for (i=0; i<16; ++i)
124                 key.at (i) = i;
125
126         print_by_16s (key, "Key");
127
128         byteArray plaintext (16, 0);
129
130         for (i=0; i<16; ++i)
131                 plaintext.at (i) = (i << 4) | i;
132
133         print_by_16s (plaintext, "Plaintext");
134
135         AES aes (key);
136
137         byteArray ciphertext = aes.encrypt (plaintext);
138
139         print_by_16s (ciphertext, "Ciphertext");
140 #endif // MY_TEST2
141
142         return 0;
143
144 }
145
146 /* vim: set ts=4 sts=4 sw=4 noet tw=120 nowrap: */