The code below shows some of the capabilities for how to use RSA.
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 <QCoreApplication>
#include <QtCrypto>
#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);
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;
}
Convenience method for initialising and cleaning up QCA.
Definition qca_core.h:660
Class for generating asymmetric key pairs.
Definition qca_publickey.h:1138
PrivateKey createRSA(int bits, int exp=65537, const QString &provider=QString())
Generate an RSA key of the specified length.
static QList< Type > supportedIOTypes(const QString &provider=QString())
PublicKey toPublicKey() const
Interpret this key as a PublicKey.
bool isNull() const
Test if the key is null (empty)
@ RSA
RSA key.
Definition qca_publickey.h:258
Generic private key.
Definition qca_publickey.h:833
void update(const MemoryRegion &a)
Update the signature process.
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 decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg)
Decrypt the message.
QByteArray signature()
The resulting signature.
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.
void startSign(SignatureAlgorithm alg, SignatureFormat format=DefaultFormat)
Initialise the message signature process.
bool canSign() const
Test if this key can be used for signing.
Generic public key.
Definition qca_publickey.h:529
void update(const MemoryRegion &a)
Update the signature verification process with more data.
bool verifyMessage(const MemoryRegion &a, const QByteArray &sig, SignatureAlgorithm alg, SignatureFormat format=DefaultFormat)
Single step message verification.
bool canEncrypt() const
Test if this key can be used for encryption.
bool validSignature(const QByteArray &sig)
void startVerify(SignatureAlgorithm alg, SignatureFormat format=DefaultFormat)
Initialise the signature verification process.
bool canVerify() const
Test if the key can be used for verifying signatures.
SecureArray encrypt(const SecureArray &a, EncryptionAlgorithm alg)
Encrypt a message using a specified algorithm.
Secure array of bytes.
Definition qca_tools.h:317
char * data()
Pointer to the data in the secure array.
QByteArray toByteArray() const
Copy the contents of the secure array out to a standard QByteArray.
bool isEmpty() const
Test if the array contains any bytes.
@ EME_PKCS1_OAEP
Optimal asymmetric encryption padding (PKCS#1, Version 2.0)
Definition qca_publickey.h:57
QCA_EXPORT QString arrayToHex(const QByteArray &array)
Convert a byte array to printable hexadecimal representation.
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
@ EMSA3_MD5
MD5, with EMSA3 (ie PKCS#1 Version 1.5) encoding (this is the usual RSA algorithm)
Definition qca_publickey.h:78