Skip to content
BrutalWizard edited this page Dec 24, 2023 · 14 revisions

Example:

#include <QByteArray>

#include "QRsa.h"

int main() {
    QSimpleCrypto::QRsa rsa;
    
    EVP_PKEY* key = rsa.generateRsaKeys(2048, 3);
    if (key) {
        rsa.savePublicKey(key, "publicKey.pem");
        rsa.savePrivateKey(key, "privateKey.pem", "password", EVP_aes_256_cbc());

        EVP_PKEY* publicKey = rsa.getPublicKeyFromFile("publicKey.pem");
        EVP_PKEY* privateKey = rsa.getPrivateKeyFromFile("privateKey.pem", "password");

        QByteArray encrypted = rsa.encrypt("Hello World!", publicKey, RSA_PKCS1_PADDING);
        QByteArray decrypted = rsa.decrypt(encrypted, privateKey, RSA_PKCS1_PADDING);

        EVP_PKEY_free(publicKey); 
        EVP_PKEY_free(privateKey); 
    }

    EVP_PKEY_free(rsa);
}

Detailed Description

RSA* generateRsaKeys(quint32 bits, quint32 rsaBigNumber)
  • bits - RSA key size. For example: 2048, 4096.
  • rsaBigNumber - The exponent is an odd number, typically 3, 17 or 65537

\details In order to maintain adequate security level, the maximum number of permitted primes depends on modulus bit length:

      <1024 | >=1024 | >=4096 | >=8192
      ------+--------+--------+-------
        2   |   3    |   4    |   5

https://www.openssl.org/docs/manmaster/man3/RSA_generate_key_ex.html

Returns 'OpenSSL EVP RSA structure' or 'nullptr', if error happened. Returned value must be cleaned up with 'EVP_PKEY_free()' to avoid memory leak.

void savePublicKey(RSA *rsa, const QByteArray& publicKeyFileName)
  • key - RSA key. Must be provided with not null EVP_PKEY OpenSSL struct.
  • filePath - Path and file name where the file will be saved. Example: "/root/ca.pem"

void savePrivateKey(EVP_PKEY* key, const QByteArray& filePath, QByteArray password = "", const EVP_CIPHER* cipher = nullptr)
  • key - RSA key. Must be provided with not null EVP_PKEY OpenSSL struct.
  • filePath - Path and file name where the file will be saved. Example: "/root/ca.pem".
  • password - Private key password.
  • cipher - Can be used with OpenSSL EVP_CIPHER (ecb, cbc, cfb, ofb, ctr) - 128, 192, 256. Example: EVP_aes_256_cbc().

EVP_PKEY* getPublicKeyFromFile(const QByteArray& filePath)
  • filePath - File path to public key file.

Returns OpenSSL EVP_PKEY on success and nullptr on failure. Returned value must be cleaned up with 'EVP_PKEY_free()' to avoid memory leak.

EVP_PKEY* getPrivateKeyFromFile(const QByteArray& filePath, const QByteArray& password = "")
  • filePath - File path to public key file.
  • password - Private key password

Returns OpenSSL EVP_PKEY on success and nullptr on failure. Returned value must be cleaned up with 'EVP_PKEY_free()' to avoid memory leak.

QByteArray encrypt(QByteArray plainText, RSA* rsa, const int& padding = RSA_PKCS1_PADDING)
  • plainText - Text that must be encrypted
  • key - RSA key. Must be provided with not null EVP_PKEY OpenSSL struct.
  • padding - OpenSSL RSA padding can be used with: 'RSA_PKCS1_PADDING', 'RSA_NO_PADDING' and etc

Returns encrypted data on success and ""* on failure.

QByteArray decrypt(QByteArray cipherText, RSA* rsa, const int& padding = RSA_PKCS1_PADDING)
  • cipherText - Text that must be decrypted
  • key - RSA key. Must be provided with not null EVP_PKEY OpenSSL struct.
  • padding - OpenSSL RSA padding can be used with: 'RSA_PKCS1_PADDING', 'RSA_NO_PADDING' and etc

Returns decrypted data on success and ""* on failure.

Clone this wiki locally