1 | #include"qca.h"
|
---|
2 | #include<stdio.h>
|
---|
3 |
|
---|
4 | static QCString arrayToCString(const QByteArray &);
|
---|
5 | static QByteArray cstringToArray(const QCString &);
|
---|
6 | static void doDynTest(QCA::Cipher *c, const QString &name, const QCString &cs);
|
---|
7 |
|
---|
8 | int main(int argc, char **argv)
|
---|
9 | {
|
---|
10 | QCA::init();
|
---|
11 | QCString cs = (argc >= 2) ? argv[1] : "hello";
|
---|
12 |
|
---|
13 | // AES128 test
|
---|
14 | if(!QCA::isSupported(QCA::CAP_AES128))
|
---|
15 | printf("AES128 not supported!\n");
|
---|
16 | else {
|
---|
17 | // encrypt
|
---|
18 | QByteArray key = QCA::AES128::generateKey();
|
---|
19 | QByteArray iv = QCA::AES128::generateIV();
|
---|
20 | printf("aes128:key:%s\n", QCA::arrayToHex(key).latin1());
|
---|
21 | printf("aes128:iv:%s\n", QCA::arrayToHex(iv).latin1());
|
---|
22 | QCA::AES128 c(QCA::Encrypt, QCA::CBC, key, iv);
|
---|
23 | c.update(cstringToArray(cs));
|
---|
24 | QByteArray f = c.final();
|
---|
25 | QString result = QCA::arrayToHex(f);
|
---|
26 | printf(">aes128(\"%s\") = [%s]\n", cs.data(), result.latin1());
|
---|
27 |
|
---|
28 | // decrypt
|
---|
29 | QCA::AES128 d(QCA::Decrypt, QCA::CBC, key, iv);
|
---|
30 | d.update(f);
|
---|
31 | QCString dec = arrayToCString(d.final());
|
---|
32 | printf("<aes128(\"%s\") = [%s]\n", result.latin1(), dec.data());
|
---|
33 | }
|
---|
34 |
|
---|
35 | // BlowFish, TripleDES, and AES256 tested dynamically
|
---|
36 | if(!QCA::isSupported(QCA::CAP_BlowFish))
|
---|
37 | printf("BlowFish not supported!\n");
|
---|
38 | else
|
---|
39 | doDynTest(new QCA::BlowFish, "bfish", cs);
|
---|
40 |
|
---|
41 | if(!QCA::isSupported(QCA::CAP_TripleDES))
|
---|
42 | printf("TripleDES not supported!\n");
|
---|
43 | else
|
---|
44 | doDynTest(new QCA::TripleDES, "3des", cs);
|
---|
45 |
|
---|
46 | if(!QCA::isSupported(QCA::CAP_AES256))
|
---|
47 | printf("AES256 not supported!\n");
|
---|
48 | else
|
---|
49 | doDynTest(new QCA::AES256, "aes256", cs);
|
---|
50 |
|
---|
51 | return 0;
|
---|
52 | }
|
---|
53 |
|
---|
54 | QCString arrayToCString(const QByteArray &a)
|
---|
55 | {
|
---|
56 | QCString cs;
|
---|
57 | cs.resize(a.size()+1);
|
---|
58 | memcpy(cs.data(), a.data(), a.size());
|
---|
59 | return cs;
|
---|
60 | }
|
---|
61 |
|
---|
62 | QByteArray cstringToArray(const QCString &cs)
|
---|
63 | {
|
---|
64 | QByteArray a(cs.length());
|
---|
65 | memcpy(a.data(), cs.data(), a.size());
|
---|
66 | return a;
|
---|
67 | }
|
---|
68 |
|
---|
69 | void doDynTest(QCA::Cipher *c, const QString &name, const QCString &cs)
|
---|
70 | {
|
---|
71 | // encrypt
|
---|
72 | QByteArray key = c->dyn_generateKey();
|
---|
73 | QByteArray iv = c->dyn_generateIV();
|
---|
74 | printf("%s:key:%s\n", name.latin1(), QCA::arrayToHex(key).latin1());
|
---|
75 | printf("%s:iv:%s\n", name.latin1(), QCA::arrayToHex(iv).latin1());
|
---|
76 | c->reset(QCA::Encrypt, QCA::CBC, key, iv);
|
---|
77 | c->update(cstringToArray(cs));
|
---|
78 | QByteArray f = c->final();
|
---|
79 | QString result = QCA::arrayToHex(f);
|
---|
80 | printf(">%s(\"%s\") = [%s]\n", name.latin1(), cs.data(), result.latin1());
|
---|
81 |
|
---|
82 | // decrypt
|
---|
83 | c->reset(QCA::Decrypt, QCA::CBC, key, iv);
|
---|
84 | c->update(f);
|
---|
85 | QCString dec = arrayToCString(c->final());
|
---|
86 | printf("<%s(\"%s\") = [%s]\n", name.latin1(), result.latin1(), dec.data());
|
---|
87 | delete c;
|
---|
88 | }
|
---|
89 |
|
---|