Warning, file /libraries/qca/examples/publickeyexample/publickeyexample.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  Copyright (C) 2003 Justin Karneges <justin@affinix.com>
0003  Copyright (C) 2005 Brad Hards <bradh@frogmouth.net>
0004 
0005  Permission is hereby granted, free of charge, to any person obtaining a copy
0006  of this software and associated documentation files (the "Software"), to deal
0007  in the Software without restriction, including without limitation the rights
0008  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0009  copies of the Software, and to permit persons to whom the Software is
0010  furnished to do so, subject to the following conditions:
0011 
0012  The above copyright notice and this permission notice shall be included in
0013  all copies or substantial portions of the Software.
0014 
0015  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
0018  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
0019  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0020  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0021 */
0022 
0023 #include <QtCrypto>
0024 
0025 #include <QCoreApplication>
0026 
0027 #include <iostream>
0028 
0029 #ifdef QT_STATICPLUGIN
0030 #include "import_plugins.h"
0031 #endif
0032 
0033 int main(int argc, char **argv)
0034 {
0035     // the Initializer object sets things up, and
0036     // also does cleanup when it goes out of scope
0037     QCA::Initializer init;
0038 
0039     QCoreApplication app(argc, argv);
0040 
0041     // We need to ensure that we have certificate handling support
0042     if (!QCA::isSupported("cert")) {
0043         std::cout << "Sorry, no PKI certificate support" << std::endl;
0044         return 1;
0045     }
0046 
0047     // Read in a private key
0048     QCA::PrivateKey    privKey;
0049     QCA::ConvertResult convRes;
0050     QCA::SecureArray   passPhrase = "start";
0051     privKey                       = QCA::PrivateKey::fromPEMFile(QStringLiteral("Userkey.pem"), passPhrase, &convRes);
0052     if (convRes != QCA::ConvertGood) {
0053         std::cout << "Sorry, could not import Private Key" << std::endl;
0054         return 1;
0055     }
0056 
0057     // Read in a matching public key cert
0058     // you could also build this using the fromPEMFile() method
0059     QCA::Certificate pubCert(QStringLiteral("User.pem"));
0060     if (pubCert.isNull()) {
0061         std::cout << "Sorry, could not import public key certificate" << std::endl;
0062         return 1;
0063     }
0064     // We are building the certificate into a SecureMessageKey object, via a
0065     // CertificateChain
0066     QCA::SecureMessageKey secMsgKey;
0067     QCA::CertificateChain chain;
0068     chain += pubCert;
0069     secMsgKey.setX509CertificateChain(chain);
0070 
0071     // build up a SecureMessage object, based on our public key certificate
0072     QCA::CMS           cms;
0073     QCA::SecureMessage msg(&cms);
0074     msg.setRecipient(secMsgKey);
0075 
0076     // Some plain text - we use the first command line argument if provided
0077     QByteArray plainText = (argc >= 2) ? argv[1] : "What do ya want for nuthin'";
0078 
0079     // Now use the SecureMessage object to encrypt the plain text.
0080     msg.startEncrypt();
0081     msg.update(plainText);
0082     msg.end();
0083     // I think it is reasonable to wait for 1 second for this
0084     msg.waitForFinished(1000);
0085 
0086     // check to see if it worked
0087     if (!msg.success()) {
0088         std::cout << "Error encrypting: " << msg.errorCode() << std::endl;
0089         return 1;
0090     }
0091 
0092     // get the result
0093     QCA::SecureArray cipherText = msg.read();
0094     QCA::Base64      enc;
0095     std::cout << plainText.data() << " encrypts to (in base 64): ";
0096     std::cout << qPrintable(enc.arrayToString(cipherText)) << std::endl;
0097 
0098     // Show we can decrypt it with the private key
0099     if (!privKey.canDecrypt()) {
0100         std::cout << "Private key cannot be used to decrypt" << std::endl;
0101         return 1;
0102     }
0103     QCA::SecureArray plainTextResult;
0104     if (0 == privKey.decrypt(cipherText, &plainTextResult, QCA::EME_PKCS1_OAEP)) {
0105         std::cout << "Decryption process failed" << std::endl;
0106         return 1;
0107     }
0108 
0109     std::cout << qPrintable(enc.arrayToString(cipherText));
0110     std::cout << " (in base 64) decrypts to: ";
0111     std::cout << plainTextResult.data() << std::endl;
0112 
0113     return 0;
0114 }