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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| // AW - 21.02.2013
//
// example slightly modified by myself from
// http://harmattan-dev.nokia.com/docs/platform-api-reference/xml/daily-docs/libqca2/publickeyexample_8cpp-example.html
/*
Copyright (C) 2003 Justin Karneges <justin@affinix.com>
Copyright (C) 2005 Brad Hards <bradh@frogmouth.net>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <QtCrypto>
#include <QCoreApplication>
#include <QDebug>
int main(int argc, char** argv)
{
// the Initializer object sets things up, and
// also does cleanup when it goes out of scope
QCA::Initializer init;
QCoreApplication app(argc, argv);
// We need to ensure that we have certificate handling support
if ( !QCA::isSupported( "cert" ) ) {
qDebug() << "Sorry, no PKI certificate support";
return -1;
}
//---------- added by AW - 21.02.2013 ------------
// we first must generate the missing key 'Userkey.pem'
QCA::PrivateKey privateKey = QCA::KeyGenerator().createRSA(1024);
if( privateKey.isNull() ) {
qDebug() << "Failed to make private key";
return -1;
}
// save the private key - in a real example, make sure this goes
// somewhere secure and has a good pass phrase
// You can use the same technique with the public key too.
QCA::SecureArray passPhrase = "start";
privateKey.toPEMFile("Userkey.pem", passPhrase);
//------------ end of addition -------------------
// Read in a private key
QCA::PrivateKey privKey;
QCA::ConvertResult convRes;
// QCA::SecureArray PassPhrase = "start"; // AW - 21.02.2013
privKey = QCA::PrivateKey::fromPEMFile( "Userkey.pem", passPhrase, &convRes );
if ( convRes != QCA::ConvertGood ) {
qDebug() << "Sorry, could not import Private Key";
return -1;
}
// Read in a matching public key cert
// you could also build this using the fromPEMFile() method
QCA::Certificate pubCert( "User.pem" );
if ( pubCert.isNull() ) {
qDebug() << "Sorry, could not import public key certificate";
return -1;
}
// We are building the certificate into a SecureMessageKey object,
// via a CertificateChain
QCA::SecureMessageKey secMsgKey;
QCA::CertificateChain chain;
chain += pubCert;
secMsgKey.setX509CertificateChain( chain );
// build up a SecureMessage object, based on our public key certificate
QCA::CMS cms;
QCA::SecureMessage msg(&cms);
msg.setRecipient(secMsgKey);
// Some plain text - we use the first command line argument if provided
QByteArray plainText = (argc >= 2) ? argv[1] : "What do ya want for nuthin'";
// Now use the SecureMessage object to encrypt the plain text.
msg.startEncrypt();
msg.update(plainText);
msg.end();
// I think it is reasonable to wait for 1 second for this
msg.waitForFinished(1000);
// check to see if it worked
if(!msg.success())
{
qDebug() << "Error encrypting: " << msg.errorCode();
return -1;
}
// get the result
QCA::SecureArray cipherText = msg.read();
QCA::Base64 enc;
qDebug() << plainText.data() << " encrypts to (in base 64): "
<< qPrintable( enc.arrayToString( cipherText ));
// Show we can decrypt it with the private key
if ( !privKey.canDecrypt() ) {
qDebug() << "Private key cannot be used to decrypt";
return -1;
}
QCA::SecureArray plainTextResult;
if ( 0 == privKey.decrypt(cipherText, &plainTextResult, QCA::EME_PKCS1_OAEP ) ) {
qDebug() << "Decryption process failed";
return -1;
}
qDebug() << qPrintable( enc.arrayToString( cipherText ) )
<< " (in base 64) decrypts to: " << plainTextResult.data();
return 0;
} |
Partager