publickeyexample.cpp

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

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

/*
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 <iostream>
#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif
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")) {
std::cout << "Sorry, no PKI certificate support" << std::endl;
return 1;
}
// Read in a private key
QCA::PrivateKey privKey;
QCA::ConvertResult convRes;
QCA::SecureArray passPhrase = "start";
privKey = QCA::PrivateKey::fromPEMFile(QStringLiteral("Userkey.pem"), passPhrase, &convRes);
if (convRes != QCA::ConvertGood) {
std::cout << "Sorry, could not import Private Key" << std::endl;
return 1;
}
// Read in a matching public key cert
// you could also build this using the fromPEMFile() method
QCA::Certificate pubCert(QStringLiteral("User.pem"));
if (pubCert.isNull()) {
std::cout << "Sorry, could not import public key certificate" << std::endl;
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()) {
std::cout << "Error encrypting: " << msg.errorCode() << std::endl;
return 1;
}
// get the result
QCA::SecureArray cipherText = msg.read();
QCA::Base64 enc;
std::cout << plainText.data() << " encrypts to (in base 64): ";
std::cout << qPrintable(enc.arrayToString(cipherText)) << std::endl;
// Show we can decrypt it with the private key
if (!privKey.canDecrypt()) {
std::cout << "Private key cannot be used to decrypt" << std::endl;
return 1;
}
QCA::SecureArray plainTextResult;
if (0 == privKey.decrypt(cipherText, &plainTextResult, QCA::EME_PKCS1_OAEP)) {
std::cout << "Decryption process failed" << std::endl;
return 1;
}
std::cout << qPrintable(enc.arrayToString(cipherText));
std::cout << " (in base 64) decrypts to: ";
std::cout << plainTextResult.data() << std::endl;
return 0;
}
QCA::Base64
Base64 encoding / decoding
Definition qca_textfilter.h:233
QCA::CMS
Cryptographic Message Syntax messaging system.
Definition qca_securemessage.h:886
QCA::CertificateChain
A chain of related Certificates.
Definition qca_cert.h:1226
QCA::Certificate
Public Key (X.509) certificate.
Definition qca_cert.h:857
QCA::Certificate::isNull
bool isNull() const
Test if the certificate is empty (null).
QCA::Initializer
Convenience method for initialising and cleaning up QCA.
Definition qca_core.h:660
QCA::PrivateKey
Generic private key.
Definition qca_publickey.h:833
QCA::PrivateKey::decrypt
bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg)
Decrypt the message.
QCA::PrivateKey::canDecrypt
bool canDecrypt() const
Test if this key can be used for decryption.
QCA::PrivateKey::fromPEMFile
static PrivateKey fromPEMFile(const QString &fileName, const SecureArray &passphrase=SecureArray(), ConvertResult *result=nullptr, const QString &provider=QString())
Import the key in Privacy Enhanced Mail (PEM) format from a file.
QCA::SecureArray
Secure array of bytes.
Definition qca_tools.h:317
QCA::SecureArray::data
char * data()
Pointer to the data in the secure array.
QCA::SecureMessageKey
Key for SecureMessage system.
Definition qca_securemessage.h:55
QCA::SecureMessageKey::setX509CertificateChain
void setX509CertificateChain(const CertificateChain &c)
Set the public key part of this X.509 key.
QCA::SecureMessage
Class representing a secure message.
Definition qca_securemessage.h:320
QCA::SecureMessage::success
bool success() const
Indicates whether or not the operation was successful or failed.
QCA::SecureMessage::setRecipient
void setRecipient(const SecureMessageKey &key)
Set the recipient for an encrypted message.
QCA::SecureMessage::waitForFinished
bool waitForFinished(int msecs=30000)
Block until the operation (encryption, decryption, signing or verifying) completes.
QCA::SecureMessage::read
QByteArray read()
Read the available data.
QCA::SecureMessage::end
void end()
Complete an operation.
QCA::SecureMessage::errorCode
Error errorCode() const
Returns the failure code.
QCA::SecureMessage::startEncrypt
void startEncrypt()
Start an encryption operation.
QCA::SecureMessage::update
void update(const QByteArray &in)
Process a message (or the next part of a message) in the current operation.
QCA::TextFilter::arrayToString
QString arrayToString(const MemoryRegion &a)
Process an array in the "forward" direction, returning a QString.
QCA::EME_PKCS1_OAEP
@ EME_PKCS1_OAEP
Optimal asymmetric encryption padding (PKCS#1, Version 2.0).
Definition qca_publickey.h:57
QCA::isSupported
QCA_EXPORT bool isSupported(const char *features, const QString &provider=QString())
Test if a capability (algorithm) is available.
QCA::ConvertResult
ConvertResult
Return value from a format conversion.
Definition qca_publickey.h:119
QCA::ConvertGood
@ ConvertGood
Conversion succeeded, results should be valid.
Definition qca_publickey.h:120