无法解密:RSA_R_PADDING_CHECK_FAILED - OpenSSL 1.1.0

时间:2016-09-13 23:11:00

标签: c windows encryption openssl

我正在创建一个简单的实用程序来使用密钥对加密和解密文件。我在Windows上并编写了1.1.1版本的Openssl。我可以加载密钥对并加密文件,但是当我尝试解密EVP_PKEY_decrypt时总是返回-1。我将此跟踪到rsa填充检查函数,它们返回-1但我无法弄清楚原因。我已经尝试将填充从RSA_PKCS1_OAEP_PADDING更改为RSA_PKCS1_PADDING,但仍然存在同样的问题。任何见解将不胜感激,这是我的加密和解密功能:

#define FILE_BUFFER_LENGTH 1
#define ENC_BUFFER_LENGTH 2048

int encryptfile(EVP_PKEY *key, FILE *srcfp, FILE *tgtfp) {

    EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(key, NULL);
    char *inbuf;
    unsigned char *outbuf;
    size_t in_len = 0;
    size_t out_len = ENC_BUFFER_LENGTH;
    int x;

    inbuf = (char*)malloc(sizeof(char)*FILE_BUFFER_LENGTH+1);
    outbuf = (char*)malloc(sizeof(char)*ENC_BUFFER_LENGTH+1);

    if (ctx == NULL) {
        fprintf(stderr, "Error while creating encryption context.\n");
        return 0;
    }

    if (EVP_PKEY_encrypt_init(ctx) <= 0) {
        fprintf(stderr, "Error while initializing encryption context.\n");
        return 0;
    }

    if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0) {
        fprintf(stderr, "Error while setting encryption padding.\n");
        return 0;
    }



    while (1) {
        in_len = fread(inbuf, 1, FILE_BUFFER_LENGTH, srcfp);

        if (in_len == 0) {break;}

        if (EVP_PKEY_encrypt(ctx, outbuf, &out_len, inbuf, in_len) <= 0) {
            fprintf(stderr, "Error while encrypting data.\n");
            return 0;
        }

        x = fwrite(outbuf, sizeof(char), in_len, tgtfp);

        if (x != in_len) {
            fprintf(stderr, "Error while writing to target file.\n");
            return 0;
        }
    }

    return 1;
}


int decryptfile(EVP_PKEY *key, FILE *srcfp, FILE *tgtfp) {

    ENGINE *e = ENGINE_new();
    EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(key, NULL);
    unsigned char *inbuf;
    unsigned char *outbuf;
    size_t in_len = 0;
    size_t out_len = ENC_BUFFER_LENGTH;
    int x;

    inbuf = (char*)malloc(sizeof(char)*FILE_BUFFER_LENGTH + 1);
    outbuf = (char*)malloc(sizeof(char)*ENC_BUFFER_LENGTH + 1);

    if (ctx == NULL) {
        fprintf(stderr, "Error while creating decryption context.\n");
        return 0;
    }

    if (EVP_PKEY_decrypt_init(ctx) <= 0) {
        fprintf(stderr, "Error while initializing decryption context.\n");
        return 0;
    }

    if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0) {
        fprintf(stderr, "Error while setting decryption padding.\n");
        return 0;
    }



    while (1) {
        in_len = fread(inbuf, 1, FILE_BUFFER_LENGTH, srcfp);

        if (in_len == 0) { break; }

        if (EVP_PKEY_decrypt(ctx, outbuf, &out_len, inbuf, in_len) <= 0) {
            fprintf(stderr, "Error while decrypting data.\n");
            return 0;
        }

        x = fwrite(outbuf, sizeof(char), in_len, tgtfp);

        if (x != in_len) {
            fprintf(stderr, "Error while writing decrypted data to target file.\n");
            return 0;
        }
    }


    return 1;
}

0 个答案:

没有答案