publickeyexample.cpp

The code below shows how to do public key encryption, decryption, signing and verification.

00001 /*
00002  Copyright (C) 2003 Justin Karneges <justin@affinix.com>
00003  Copyright (C) 2005 Brad Hards <bradh@frogmouth.net>
00004 
00005  Permission is hereby granted, free of charge, to any person obtaining a copy
00006  of this software and associated documentation files (the "Software"), to deal
00007  in the Software without restriction, including without limitation the rights
00008  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009  copies of the Software, and to permit persons to whom the Software is
00010  furnished to do so, subject to the following conditions:
00011 
00012  The above copyright notice and this permission notice shall be included in
00013  all copies or substantial portions of the Software.
00014 
00015  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00018  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00019  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00020  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00021 */
00022 
00023 
00024 #include <QtCrypto>
00025 
00026 #include <QCoreApplication>
00027 
00028 #include <iostream>
00029 
00030 
00031 int main(int argc, char** argv)
00032 {
00033     // the Initializer object sets things up, and
00034     // also does cleanup when it goes out of scope
00035     QCA::Initializer init;
00036 
00037     QCoreApplication app(argc, argv);
00038 
00039     // We need to ensure that we have certificate handling support
00040     if ( !QCA::isSupported( "cert" ) ) {
00041         std::cout << "Sorry, no PKI certificate support" << std::endl;
00042         return 1;
00043     }
00044 
00045     // Read in a private key
00046     QCA::PrivateKey privKey;
00047     QCA::ConvertResult convRes;
00048     QCA::SecureArray passPhrase = "start";
00049     privKey = QCA::PrivateKey::fromPEMFile( "Userkey.pem", passPhrase, &convRes );
00050     if ( convRes != QCA::ConvertGood ) {
00051         std::cout << "Sorry, could not import Private Key" << std::endl;
00052         return 1;
00053     }
00054 
00055     // Read in a matching public key cert
00056     // you could also build this using the fromPEMFile() method
00057     QCA::Certificate pubCert( "User.pem" );
00058     if ( pubCert.isNull() ) {
00059         std::cout << "Sorry, could not import public key certificate" << std::endl;
00060         return 1;
00061     }
00062     // We are building the certificate into a SecureMessageKey object, via a
00063     // CertificateChain
00064     QCA::SecureMessageKey secMsgKey;
00065     QCA::CertificateChain chain;
00066     chain += pubCert;
00067     secMsgKey.setX509CertificateChain( chain );
00068 
00069     // build up a SecureMessage object, based on our public key certificate
00070     QCA::CMS cms;
00071     QCA::SecureMessage msg(&cms);
00072     msg.setRecipient(secMsgKey);
00073 
00074     // Some plain text - we use the first command line argument if provided
00075     QByteArray plainText = (argc >= 2) ? argv[1] : "What do ya want for nuthin'";
00076 
00077     // Now use the SecureMessage object to encrypt the plain text.
00078     msg.startEncrypt();
00079     msg.update(plainText);
00080     msg.end();
00081     // I think it is reasonable to wait for 1 second for this
00082     msg.waitForFinished(1000);
00083 
00084     // check to see if it worked
00085     if(!msg.success())
00086     {
00087         std::cout << "Error encrypting: " << msg.errorCode() << std::endl;
00088         return 1;
00089     }
00090 
00091     // get the result
00092     QCA::SecureArray cipherText = msg.read();
00093     QCA::Base64 enc;
00094     std::cout << plainText.data() << " encrypts to (in base 64): ";
00095     std::cout << qPrintable( enc.arrayToString( cipherText ) ) << std::endl;
00096 
00097     // Show we can decrypt it with the private key
00098     if ( !privKey.canDecrypt() ) {
00099         std::cout << "Private key cannot be used to decrypt" << std::endl;
00100         return 1;
00101     }
00102     QCA::SecureArray plainTextResult;
00103     if ( 0 == privKey.decrypt(cipherText, &plainTextResult, QCA::EME_PKCS1_OAEP ) ) {
00104         std::cout << "Decryption process failed" << std::endl;
00105         return 1;
00106     }
00107 
00108     std::cout << qPrintable( enc.arrayToString( cipherText ) );
00109     std::cout << " (in base 64) decrypts to: ";
00110     std::cout << plainTextResult.data() << std::endl;
00111 
00112     return 0;
00113 }
00114 

Generated on Fri Jul 6 13:22:42 2007 for Qt Cryptographic Architecture by  doxygen 1.4.6