Qt Cryptographic Architecture
|
The code below shows some of the capabilities for how to use RSA. This example also shows how to export and import a key to a file, using PEM encoding.
#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 << "RSA not supported!\n";
else {
std::cout << "Failed to make private RSA key" << std::endl;
return 1;
}
std::cout << "Error: this kind of key cannot encrypt" << std::endl;
return 1;
}
std::cout << "Error encrypting" << std::endl;
return 1;
}
std::cout <<
"\"" << arg.
data() <<
"\" encrypted with RSA is \"";
std::cout << qPrintable(rstr) << "\"" << std::endl;
seckey.
toPEMFile(QStringLiteral(
"keyprivate.pem"), passPhrase);
passPhrase,
&conversionResult);
std::cout << "Private key read failed" << std::endl;
}
std::cout << "Error decrypting.\n";
return 1;
}
std::cout << "\"" << qPrintable(rstr) << "\" decrypted with RSA is \"";
std::cout << decrypt.
data() <<
"\"" << std::endl;
std::cout << "Error: this kind of key cannot sign" << std::endl;
return 1;
}
std::cout <<
"Signature for \"" << arg.
data() <<
"\" using RSA, is ";
std::cout << "\"" << qPrintable( rstr ) << "\"" << std::endl;
std::cout << "Signature is valid" << std::endl;
} else {
std::cout << "Bad signature" << std::endl;
}
}
std::cout << "Signature is valid" << std::endl;
} else {
std::cout << "Signature could not be verified" << std::endl;
}
}
return 0;
}
bool verifyMessage(const MemoryRegion &a, const QByteArray &sig, SignatureAlgorithm alg, SignatureFormat format=DefaultFormat)
Single step message verification.
@ RSA
RSA key.
Definition: qca_publickey.h:256
@ EMSA3_MD5
MD5, with EMSA3 (ie PKCS#1 Version 1.5) encoding (this is the usual RSA algorithm)
Definition: qca_publickey.h:78
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.
PrivateKey createRSA(int bits, int exp=65537, const QString &provider=QString())
Generate an RSA key of the specified length.
QCA_EXPORT void init()
Initialise QCA.
Definition: qca_publickey.h:827
bool canEncrypt() const
Test if this key can be used for encryption.
bool canSign() const
Test if this key can be used for signing.
QByteArray toByteArray() const
Copy the contents of the secure array out to a standard QByteArray.
bool isNull() const
Test if the key is null (empty)
void startSign(SignatureAlgorithm alg, SignatureFormat format=DefaultFormat)
Initialise the message signature process.
PublicKey toPublicKey() const
Interpret this key as a PublicKey.
bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg)
Decrypt the message.
QByteArray signature()
The resulting signature.
bool toPEMFile(const QString &fileName, const SecureArray &passphrase=SecureArray(), PBEAlgorithm pbe=PBEDefault) const
Export the key in Privacy Enhanced Mail (PEM) format to a file.
bool canVerify() const
Test if the key can be used for verifying signatures.
bool validSignature(const QByteArray &sig)
Definition: qca_tools.h:317
SecureArray encrypt(const SecureArray &a, EncryptionAlgorithm alg)
Encrypt a message using a specified algorithm.
ConvertResult
Return value from a format conversion.
Definition: qca_publickey.h:118
void update(const MemoryRegion &a)
Update the signature verification process with more data.
Definition: qca_publickey.h:1121
QCA_EXPORT QString arrayToHex(const QByteArray &array)
Convert a byte array to printable hexadecimal representation.
Definition: qca_core.h:660
QCA_EXPORT bool isSupported(const char *features, const QString &provider=QString())
Test if a capability (algorithm) is available.
Definition: qca_publickey.h:527
@ ConvertGood
Conversion succeeded, results should be valid.
Definition: qca_publickey.h:119
static QList< Type > supportedIOTypes(const QString &provider=QString())
void startVerify(SignatureAlgorithm alg, SignatureFormat format=DefaultFormat)
Initialise the signature verification process.
bool isEmpty() const
Test if the array contains any bytes.
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
void update(const MemoryRegion &a)
Update the signature process.