Qt Cryptographic Architecture
|
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;
}
bool success() const
Indicates whether or not the operation was successful or failed.
QByteArray read()
Read the available data.
Error errorCode() const
Returns the failure code.
void end()
Complete an operation.
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.
Key for SecureMessage system.
Definition: qca_securemessage.h:55
QCA_EXPORT void init()
Initialise QCA.
Cryptographic Message Syntax messaging system.
Definition: qca_securemessage.h:887
Generic private key.
Definition: qca_publickey.h:827
void setX509CertificateChain(const CertificateChain &c)
Set the public key part of this X.509 key.
void update(const QByteArray &in)
Process a message (or the next part of a message) in the current operation.
bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg)
Decrypt the message.
A chain of related Certificates.
Definition: qca_cert.h:1208
bool waitForFinished(int msecs=30000)
Block until the operation (encryption, decryption, signing or verifying) completes.
bool canDecrypt() const
Test if this key can be used for decryption.
Secure array of bytes.
Definition: qca_tools.h:317
bool isNull() const
Test if the certificate is empty (null)
Public Key (X.509) certificate.
Definition: qca_cert.h:849
Base64 encoding / decoding
Definition: qca_textfilter.h:233
Class representing a secure message.
Definition: qca_securemessage.h:321
ConvertResult
Return value from a format conversion.
Definition: qca_publickey.h:118
Convenience method for initialising and cleaning up QCA.
Definition: qca_core.h:660
QCA_EXPORT bool isSupported(const char *features, const QString &provider=QString())
Test if a capability (algorithm) is available.
@ ConvertGood
Conversion succeeded, results should be valid.
Definition: qca_publickey.h:119
QString arrayToString(const MemoryRegion &a)
Process an array in the "forward" direction, returning a QString.
void setRecipient(const SecureMessageKey &key)
Set the recipient for an encrypted message.
char * data()
Pointer to the data in the secure array.
@ EME_PKCS1_OAEP
Optimal asymmetric encryption padding (PKCS#1, Version 2.0)
Definition: qca_publickey.h:57