~ubuntu-branches/ubuntu/wily/qca2/wily-proposed

« back to all changes in this revision

Viewing changes to qca/examples/publickeyexample/publickeyexample.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2007-10-27 18:51:54 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20071027185154-4ir9ys3h2q9fofrw
Tags: 2.0.0-2
Upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 Copyright (C) 2003 Justin Karneges <justin@affinix.com>
3
 
 Copyright (C) 2005 Brad Hards <bradh@frogmouth.net>
4
 
 
5
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
6
 
 of this software and associated documentation files (the "Software"), to deal
7
 
 in the Software without restriction, including without limitation the rights
8
 
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 
 copies of the Software, and to permit persons to whom the Software is
10
 
 furnished to do so, subject to the following conditions:
11
 
 
12
 
 The above copyright notice and this permission notice shall be included in
13
 
 all copies or substantial portions of the Software.
14
 
 
15
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18
 
 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19
 
 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
 
 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
 
*/
22
 
 
23
 
 
24
 
#include <QtCrypto>
25
 
 
26
 
#include <QCoreApplication>
27
 
 
28
 
#include <iostream>
29
 
 
30
 
 
31
 
int main(int argc, char** argv)
32
 
{
33
 
    // the Initializer object sets things up, and
34
 
    // also does cleanup when it goes out of scope
35
 
    QCA::Initializer init;
36
 
 
37
 
    QCoreApplication app(argc, argv);
38
 
 
39
 
    // We need to ensure that we have certificate handling support
40
 
    if ( !QCA::isSupported( "cert" ) ) {
41
 
        std::cout << "Sorry, no PKI certificate support" << std::endl;
42
 
        return 1;
43
 
    }
44
 
 
45
 
    // Read in a private key
46
 
    QCA::PrivateKey privKey;
47
 
    QCA::ConvertResult convRes;
48
 
    QCA::SecureArray passPhrase = "start";
49
 
    privKey = QCA::PrivateKey::fromPEMFile( "Userkey.pem", passPhrase, &convRes );
50
 
    if ( convRes != QCA::ConvertGood ) {
51
 
        std::cout << "Sorry, could not import Private Key" << std::endl;
52
 
        return 1;
53
 
    }
54
 
 
55
 
    // Read in a matching public key cert
56
 
    // you could also build this using the fromPEMFile() method
57
 
    QCA::Certificate pubCert( "User.pem" );
58
 
    if ( pubCert.isNull() ) {
59
 
        std::cout << "Sorry, could not import public key certificate" << std::endl;
60
 
        return 1;
61
 
    }
62
 
    // We are building the certificate into a SecureMessageKey object, via a
63
 
    // CertificateChain
64
 
    QCA::SecureMessageKey secMsgKey;
65
 
    QCA::CertificateChain chain;
66
 
    chain += pubCert;
67
 
    secMsgKey.setX509CertificateChain( chain );
68
 
 
69
 
    // build up a SecureMessage object, based on our public key certificate
70
 
    QCA::CMS cms;
71
 
    QCA::SecureMessage msg(&cms);
72
 
    msg.setRecipient(secMsgKey);
73
 
 
74
 
    // Some plain text - we use the first command line argument if provided
75
 
    QByteArray plainText = (argc >= 2) ? argv[1] : "What do ya want for nuthin'";
76
 
 
77
 
    // Now use the SecureMessage object to encrypt the plain text.
78
 
    msg.startEncrypt();
79
 
    msg.update(plainText);
80
 
    msg.end();
81
 
    // I think it is reasonable to wait for 1 second for this
82
 
    msg.waitForFinished(1000);
83
 
 
84
 
    // check to see if it worked
85
 
    if(!msg.success())
86
 
    {
87
 
        std::cout << "Error encrypting: " << msg.errorCode() << std::endl;
88
 
        return 1;
89
 
    }
90
 
 
91
 
    // get the result
92
 
    QCA::SecureArray cipherText = msg.read();
93
 
    QCA::Base64 enc;
94
 
    std::cout << plainText.data() << " encrypts to (in base 64): ";
95
 
    std::cout << qPrintable( enc.arrayToString( cipherText ) ) << std::endl;
96
 
 
97
 
    // Show we can decrypt it with the private key
98
 
    if ( !privKey.canDecrypt() ) {
99
 
        std::cout << "Private key cannot be used to decrypt" << std::endl;
100
 
        return 1;
101
 
    }
102
 
    QCA::SecureArray plainTextResult;
103
 
    if ( 0 == privKey.decrypt(cipherText, &plainTextResult, QCA::EME_PKCS1_OAEP ) ) {
104
 
        std::cout << "Decryption process failed" << std::endl;
105
 
        return 1;
106
 
    }
107
 
 
108
 
    std::cout << qPrintable( enc.arrayToString( cipherText ) );
109
 
    std::cout << " (in base 64) decrypts to: ";
110
 
    std::cout << plainTextResult.data() << std::endl;
111
 
 
112
 
    return 0;
113
 
}
114