错误:FFFFFFFFFFFFFFFF:lib(255):func(4095):RSA解密期间的原因(4095)

时间:2014-09-12 13:05:42

标签: c++ qt openssl rsa crypt

我正在尝试使用Qt Gui在C ++中创建混合加密工具。 (数据将使用AES 256-CBC加密,AES密钥RSA加密并保存。) 但是这个工具的RSA部分不起作用。 我写了几次源代码,但我总是在解密时遇到同样的错误。

error:FFFFFFFFFFFFFFFF:lib(255):func(4095):reason(4095)

我希望有人可以帮助我获得有效的RSA加密+解密实现。

您可以在此处查看源代码或从我的Dropbox下载测试Qt项目..

Dropbox下载:https://db.tt/6HKsYRTa

源代码1.实施:

void MainWindow::rsaEncrypt()
{
    EVP_PKEY *pk = NULL;
    EVP_PKEY_CTX *ctx = NULL;

    QByteArray encrypted = QByteArray();

    //------------------------------------------------
    //--- READ PUBLIC KEY ----------------------------
    FILE *pkFile = fopen(ui->publicKeyPath->text().toStdString().c_str(), "r");
    if(pkFile == NULL) throw NULL;

    pk = PEM_read_PUBKEY(pkFile, NULL, NULL, NULL);
    if(pk == NULL) throw NULL;
    fclose(pkFile);
    //------------------------------------------------
    ctx = EVP_PKEY_CTX_new(pk, NULL);


    //------------------------------------------------
    //--- ENCRYPT DATA -------------------------------
    int err;

    err = EVP_PKEY_encrypt_init(ctx);
    if(err <= 0) throw NULL;

    err = EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PADDING);
    if(err <= 0) throw NULL;

    size_t outLen = 0;
    err = EVP_PKEY_encrypt(
                            ctx,
                            NULL,
                            &outLen,
                            (uchar*) ui->plainTextEdit->document()->toPlainText().toStdString().c_str(),
                            ui->plainTextEdit->document()->toPlainText().size()
    );
    if(err <= 0) throw NULL;
    encrypted.resize(outLen);

    err = EVP_PKEY_encrypt(
                            ctx,
                            (uchar*) encrypted.data(),
                            &outLen,
                            (uchar*) ui->plainTextEdit->document()->toPlainText().toStdString().c_str(),
                            ui->plainTextEdit->document()->toPlainText().size()
    );
    //------------------------------------------------
    EVP_PKEY_CTX_free(ctx);
    EVP_PKEY_free(pk);

    if(err > 0) ui->encryptedTextEdit->document()->setPlainText(QString(encrypted));
    else {
            QByteArray errStr = QByteArray();
            errStr.resize(256);
            ERR_load_ERR_strings();
            ERR_error_string(err, errStr.data());
            ui->encryptedTextEdit->document()->setPlainText( QString(errStr) );
    }
}

void MainWindow::rsaDecrypt()
{
    EVP_PKEY *pk = NULL;
    EVP_PKEY_CTX *ctx = NULL;

    QByteArray decrypted = QByteArray();

    //------------------------------------------------
    //--- READ PRIVATE KEY ---------------------------
    FILE *pkFile = fopen(ui->privateKeyPath->text().toStdString().c_str(), "r");
    if(pkFile == NULL) throw NULL;

    pk = PEM_read_PrivateKey(pkFile, NULL, NULL, NULL);
    if(pk == NULL) throw NULL;
    fclose(pkFile);
    //------------------------------------------------
    ctx = EVP_PKEY_CTX_new(pk, NULL);


    //------------------------------------------------
    //--- DECRYPT DATA -------------------------------
    int err;

    err = EVP_PKEY_decrypt_init(ctx);
    if(err <= 0) throw NULL;

    err = EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PADDING);
    if(err <= 0) throw NULL;

    size_t outLen = 0;
    err = EVP_PKEY_decrypt(
                            ctx,
                            NULL,
                            &outLen,
                            (uchar*) ui->encryptedTextEdit->document()->toPlainText().toStdString().c_str(),
                            ui->encryptedTextEdit->document()->toPlainText().size()
    );
    if(err <= 0) throw NULL;
    decrypted.resize(outLen);

    err = EVP_PKEY_decrypt(
                            ctx,
                            (uchar*) decrypted.data(),
                            &outLen,
                            (uchar*) ui->encryptedTextEdit->document()->toPlainText().toStdString().c_str(),
                            ui->encryptedTextEdit->document()->toPlainText().size()
    );
    //------------------------------------------------
    EVP_PKEY_CTX_free(ctx);
    EVP_PKEY_free(pk);

    if(err > 0) ui->decryptedTextEdit->document()->setPlainText(QString(decrypted));
    else {
            QByteArray errStr = QByteArray();
            errStr.resize(256);
            ERR_load_ERR_strings();
            ERR_error_string(err, errStr.data());
            ui->decryptedTextEdit->document()->setPlainText( QString(errStr) );
    }
}

源代码2.实施:

void MainWindow::rsaEncrypt()
{
    RSA *rsa = createRSAFromFile(ui->publicKeyPath->text().toStdString().c_str(), 1);

    QByteArray encrypted = QByteArray();
    encrypted.resize(2048);

    int err = RSA_public_encrypt(
                    ui->plainTextEdit->document()->toPlainText().size(),
                    (uchar*) ui->plainTextEdit->document()->toPlainText().toStdString().c_str(),
                    (uchar*) encrypted.data(),
                    rsa,
                    RSA_PADDING
    );

    RSA_free(rsa);
    if(err > 0) ui->encryptedTextEdit->document()->setPlainText( QString(encrypted) );
    else {
            QByteArray errStr = QByteArray();
            errStr.resize(256);
            ERR_load_ERR_strings();
            ERR_error_string(err, errStr.data());
            ui->encryptedTextEdit->document()->setPlainText( QString(errStr) );
    }
}

void MainWindow::rsaDecrypt()
{
    RSA *rsa = createRSAFromFile(ui->privateKeyPath->text().toStdString().c_str(), 0);

    QByteArray decrypted = QByteArray();
    decrypted.resize(2048);

    int err = RSA_private_decrypt(
                    ui->encryptedTextEdit->document()->toPlainText().size(),
                    (uchar*) ui->encryptedTextEdit->document()->toPlainText().toStdString().c_str(),
                    (uchar*) decrypted.data(),
                    rsa,
                    RSA_PADDING
    );


    RSA_free(rsa);
    if(err > 0) ui->decryptedTextEdit->document()->setPlainText( QString(decrypted) );
    else {
            QByteArray errStr = QByteArray();
            errStr.resize(256);
            ERR_load_ERR_strings();
            ERR_error_string(err, errStr.data());
            ui->decryptedTextEdit->document()->setPlainText( QString(errStr) );
    }
}

RSA *MainWindow::createRSAFromFile(const char *keyPath, int pub)
{
    FILE *keyFile = fopen(keyPath, "rb");
    if(keyFile==NULL)
    {
            return 0;
    }
    RSA *rsa = RSA_new();

    if(pub)
    {
            rsa = PEM_read_RSA_PUBKEY(keyFile, &rsa, NULL, NULL);
    }
    else
    {
            rsa = PEM_read_RSAPrivateKey(keyFile, &rsa, NULL, NULL);
    }
    fclose(keyFile);
    return rsa;
}

包含并定义两种实现:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QByteArray>

#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>

#define RSA_PADDING RSA_PKCS1_OAEP_PADDING

2 个答案:

答案 0 :(得分:0)

尝试使用此代码创建RSA对象。绝对有效。您应该先阅读.pem文件,然后调用此函数:

RSA *CryptClassRSA::createRSA(unsigned char *key, int isPublic){
    RSA *rsa = NULL;
    BIO *keybio;
    keybio = BIO_new_mem_buf(key, -1);
    if (keybio==NULL){
        printf( "Failed to create key BIO");
        return NULL;
    }
    if(isPublic){
        rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa,NULL, NULL);
    }
    else{
        rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa,NULL, NULL);
    }
    if(rsa == NULL){
        printf( "Failed to create RSA");
    }

    return rsa;
}

答案 1 :(得分:0)

  

C ++ RSA解密错误:FFFFFFFFFFFFFFFF:lib(255):func(4095):reason(4095)

int err;
...
err = EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PADDING);
if(err <= 0) throw NULL;
...

我不清楚 错误发生在哪里或者您如何获得 {{1}的 errstr 输出} 的。我选择了 error:FFFFFFFFFFFFFFFF:lib(255):func(4095):reason(4095) 作为代码段,因为我认为返回值为-2(并且具有如下所述的重要性)。

EVP_PKEY_CTX_set_rsa_padding只是一个返回码。要获得实际错误,您需要拨打 err 。也许是这样的:

ERR_get_error

您还应该访问EVP_PKEY_CTX_set_rsa_paddingERR_get_error手册页。

通常,OpenSSL将返回0表示成功,因此我不确定某些地方的 int rc; unsigned long err; ... rc = EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PADDING); err = ERR_get_error(); if(rc <= 0) { // err is valid } 。此外,由于您的错误是 err <= 0 (而不是 0xffff...4095 ),我认为你正在获得手册页中讨论的-2返回值:

  

EVP_PKEY_CTX_ctrl()及其宏返回成功的正值,0或失败的负值。特别是返回值为-2表示公钥算法不支持该操作。

另请注意......如果您收集错误并以十六进制打印:

4096

然后您可以使用 rc = EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PADDING); err = ERR_get_error(); if(rc <= 0) { // err is valid std::cerr << std::hex << err << std::endl; } 进行打印。