The code below shows how to do public key encryption, decryption, signing and verification.
#include <QtCrypto>
#include <QCoreApplication>
#include <iostream>
#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
std::cout << "Sorry, no PKI certificate support" << std::endl;
return 1;
}
std::cout << "Sorry, could not import Private Key" << std::endl;
return 1;
}
std::cout << "Sorry, could not import public key certificate" << std::endl;
return 1;
}
chain += pubCert;
QByteArray plainText = (argc >= 2) ? argv[1] : "What do ya want for nuthin'";
std::cout <<
"Error encrypting: " << msg.
errorCode() << std::endl;
return 1;
}
std::cout << plainText.data() << " encrypts to (in base 64): ";
std::cout << qPrintable(enc.
arrayToString(cipherText)) << std::endl;
std::cout << "Private key cannot be used to decrypt" << std::endl;
return 1;
}
std::cout << "Decryption process failed" << std::endl;
return 1;
}
std::cout << " (in base 64) decrypts to: ";
std::cout << plainTextResult.
data() << std::endl;
return 0;
}
Base64 encoding / decoding
Definition: qca_textfilter.h:233
Cryptographic Message Syntax messaging system.
Definition: qca_securemessage.h:886
A chain of related Certificates.
Definition: qca_cert.h:1226
Public Key (X.509) certificate.
Definition: qca_cert.h:857
bool isNull() const
Test if the certificate is empty (null)
Convenience method for initialising and cleaning up QCA.
Definition: qca_core.h:660
Generic private key.
Definition: qca_publickey.h:833
bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg)
Decrypt the message.
bool canDecrypt() const
Test if this key can be used for decryption.
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.
Secure array of bytes.
Definition: qca_tools.h:317
char * data()
Pointer to the data in the secure array.
Key for SecureMessage system.
Definition: qca_securemessage.h:55
void setX509CertificateChain(const CertificateChain &c)
Set the public key part of this X.509 key.
Class representing a secure message.
Definition: qca_securemessage.h:320
bool success() const
Indicates whether or not the operation was successful or failed.
void setRecipient(const SecureMessageKey &key)
Set the recipient for an encrypted message.
bool waitForFinished(int msecs=30000)
Block until the operation (encryption, decryption, signing or verifying) completes.
QByteArray read()
Read the available data.
void end()
Complete an operation.
Error errorCode() const
Returns the failure code.
void update(const QByteArray &in)
Process a message (or the next part of a message) in the current operation.
QString arrayToString(const MemoryRegion &a)
Process an array in the "forward" direction, returning a QString.
QCA_EXPORT void init()
Initialise QCA.
@ EME_PKCS1_OAEP
Optimal asymmetric encryption padding (PKCS#1, Version 2.0)
Definition: qca_publickey.h:57
QCA_EXPORT bool isSupported(const char *features, const QString &provider=QString())
Test if a capability (algorithm) is available.
ConvertResult
Return value from a format conversion.
Definition: qca_publickey.h:119
@ ConvertGood
Conversion succeeded, results should be valid.
Definition: qca_publickey.h:120