1 | #include<qdom.h>
|
---|
2 | #include<qtextstream.h>
|
---|
3 | #include<stdio.h>
|
---|
4 |
|
---|
5 | #include"util/cipher.h"
|
---|
6 | #include"xmlsec/xmlenc.h"
|
---|
7 |
|
---|
8 | static QCString elemToString(const QDomElement &e)
|
---|
9 | {
|
---|
10 | QString out;
|
---|
11 | QTextStream ts(&out, IO_WriteOnly);
|
---|
12 | e.save(ts, 1);
|
---|
13 | return out.utf8();
|
---|
14 | }
|
---|
15 |
|
---|
16 | int main()
|
---|
17 | {
|
---|
18 | QDomDocument doc;
|
---|
19 | QDomElement order = doc.createElement("order");
|
---|
20 | doc.appendChild(order);
|
---|
21 |
|
---|
22 | QDomElement e = doc.createElement("creditcard");
|
---|
23 | e.appendChild(doc.createTextNode("1234 5678 9012 3456"));
|
---|
24 | order.appendChild(e);
|
---|
25 | printf("---- Original\n%s----\n\n", elemToString(order).data());
|
---|
26 |
|
---|
27 | Cipher::Type mainKeyType = Cipher::TripleDES;
|
---|
28 | Cipher::Type subKeyType = Cipher::AES_256;
|
---|
29 |
|
---|
30 | RSAKey rsa = generateRSAKey();
|
---|
31 | if(rsa.isNull()) {
|
---|
32 | printf("RSA key generation failed!\n");
|
---|
33 | return 0;
|
---|
34 | }
|
---|
35 | bool ok;
|
---|
36 | QCString cs = "Hello.";
|
---|
37 | printf("cs[%d]\n", cs.size());
|
---|
38 | QByteArray result = encryptRSA2(cs, rsa, &ok);
|
---|
39 | if(!ok) {
|
---|
40 | printf("Unable to RSA encrypt!\n");
|
---|
41 | return 0;
|
---|
42 | }
|
---|
43 | printf("result[%d]\n", result.size());
|
---|
44 | QByteArray result2 = decryptRSA2(result, rsa, &ok);
|
---|
45 | if(!ok) {
|
---|
46 | printf("Unable to RSA decrypt!\n");
|
---|
47 | return 0;
|
---|
48 | }
|
---|
49 | printf("result2[%d]\n", result2.size());
|
---|
50 | printf("{%s}\n", result2.data());
|
---|
51 |
|
---|
52 | QByteArray iv = Cipher::generateIV(mainKeyType);
|
---|
53 | printf("ivsize=[%d]\n", iv.size());
|
---|
54 | Cipher::Key key1 = Cipher::generateKey(mainKeyType);
|
---|
55 | printf("keysize=[%d]\n", key1.data().size());
|
---|
56 | Cipher::Key key = Cipher::generateKey(subKeyType);
|
---|
57 | printf("Key: { ");
|
---|
58 | for(int n = 0; n < (int)key.data().size(); ++n)
|
---|
59 | printf("%02x", (unsigned char)key.data()[n]);
|
---|
60 | printf(" }\n\n");
|
---|
61 |
|
---|
62 | XmlEnc::Encrypted dat;
|
---|
63 | dat.encryptKey(key, key1);
|
---|
64 | QDomElement encKey = dat.toXml(&doc);
|
---|
65 |
|
---|
66 | //printf("---- Encrypted Key\n%s----\n\n", elemToString(encKey).data());
|
---|
67 |
|
---|
68 | XmlEnc::KeyInfo ki;
|
---|
69 | ki.setName("My Key");
|
---|
70 | //ki.attachEncryptedKey(encKey);
|
---|
71 | ki.setValue(key.data());
|
---|
72 |
|
---|
73 | dat.setKeyInfo(ki);
|
---|
74 | dat.encryptElement(e, key);
|
---|
75 | QDomElement enc = dat.toXml(&doc);
|
---|
76 | order.replaceChild(enc, e);
|
---|
77 | printf("---- Encrypt\n%s----\n\n", elemToString(order).data());
|
---|
78 |
|
---|
79 | XmlEnc::Encrypted de;
|
---|
80 | if(!de.fromXml(encKey))
|
---|
81 | printf("Error: could not read encrypted xml!\n");
|
---|
82 | QByteArray r = de.decryptKey(key1);
|
---|
83 |
|
---|
84 | Cipher::Key realKey;
|
---|
85 | realKey.setData(r);
|
---|
86 | realKey.setType(subKeyType);
|
---|
87 |
|
---|
88 | if(!de.fromXml(enc))
|
---|
89 | printf("Error: could not read encrypted xml!\n");
|
---|
90 |
|
---|
91 | QDomElement dec = de.decryptElement(&doc, realKey);
|
---|
92 | order.replaceChild(dec, enc);
|
---|
93 | printf("---- Decrypt\n%s----\n\n", elemToString(order).data());
|
---|
94 | }
|
---|