This repository has been archived by the owner on Mar 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
enctest.cpp
94 lines (79 loc) · 2.39 KB
/
enctest.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
#include<qdom.h>
#include<qtextstream.h>
#include<stdio.h>
#include"util/cipher.h"
#include"xmlsec/xmlenc.h"
static QCString elemToString(const QDomElement &e)
{
QString out;
QTextStream ts(&out, IO_WriteOnly);
e.save(ts, 1);
return out.utf8();
}
int main()
{
QDomDocument doc;
QDomElement order = doc.createElement("order");
doc.appendChild(order);
QDomElement e = doc.createElement("creditcard");
e.appendChild(doc.createTextNode("1234 5678 9012 3456"));
order.appendChild(e);
printf("---- Original\n%s----\n\n", elemToString(order).data());
Cipher::Type mainKeyType = Cipher::TripleDES;
Cipher::Type subKeyType = Cipher::AES_256;
RSAKey rsa = generateRSAKey();
if(rsa.isNull()) {
printf("RSA key generation failed!\n");
return 0;
}
bool ok;
QCString cs = "Hello.";
printf("cs[%d]\n", cs.size());
QByteArray result = encryptRSA2(cs, rsa, &ok);
if(!ok) {
printf("Unable to RSA encrypt!\n");
return 0;
}
printf("result[%d]\n", result.size());
QByteArray result2 = decryptRSA2(result, rsa, &ok);
if(!ok) {
printf("Unable to RSA decrypt!\n");
return 0;
}
printf("result2[%d]\n", result2.size());
printf("{%s}\n", result2.data());
QByteArray iv = Cipher::generateIV(mainKeyType);
printf("ivsize=[%d]\n", iv.size());
Cipher::Key key1 = Cipher::generateKey(mainKeyType);
printf("keysize=[%d]\n", key1.data().size());
Cipher::Key key = Cipher::generateKey(subKeyType);
printf("Key: { ");
for(int n = 0; n < (int)key.data().size(); ++n)
printf("%02x", (unsigned char)key.data()[n]);
printf(" }\n\n");
XmlEnc::Encrypted dat;
dat.encryptKey(key, key1);
QDomElement encKey = dat.toXml(&doc);
//printf("---- Encrypted Key\n%s----\n\n", elemToString(encKey).data());
XmlEnc::KeyInfo ki;
ki.setName("My Key");
//ki.attachEncryptedKey(encKey);
ki.setValue(key.data());
dat.setKeyInfo(ki);
dat.encryptElement(e, key);
QDomElement enc = dat.toXml(&doc);
order.replaceChild(enc, e);
printf("---- Encrypt\n%s----\n\n", elemToString(order).data());
XmlEnc::Encrypted de;
if(!de.fromXml(encKey))
printf("Error: could not read encrypted xml!\n");
QByteArray r = de.decryptKey(key1);
Cipher::Key realKey;
realKey.setData(r);
realKey.setType(subKeyType);
if(!de.fromXml(enc))
printf("Error: could not read encrypted xml!\n");
QDomElement dec = de.decryptElement(&doc, realKey);
order.replaceChild(dec, enc);
printf("---- Decrypt\n%s----\n\n", elemToString(order).data());
}