OpenSSL rsa例程:RSA_padding_check_PKCS1_type_2:pkcs解码错误

时间:2018-05-02 05:51:08

标签: c encryption aes rsa

我尝试使用RS中的openssl使用RSA加密算法加密/解密AES密钥/ iv。

在将加密数据存储到文件之前,解密正在进行。但是解密在解密存储在文件中的相同加密数据时引发了错误。

这是我的代码:

#include <stdio.h>
#include <stdbool.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <string.h>

int padding = RSA_PKCS1_PADDING;
#define RSA_KEY_Size 384;
#define AES_256_KEY_SIZE 32;

// testing encryption/decryption
int func(const char* pubkeyfile, unsigned char *key, unsigned char *iv)
{
    unsigned char  *encryptedkey = (unsigned char*)malloc(RSA_KEY_Size);
    unsigned char  *encryptediv = (unsigned char*)malloc(RSA_KEY_Size);
    unsigned char  *decryptedkey = (unsigned char*)malloc(AES_256_KEY_SIZE);
    unsigned char  *decryptediv = (unsigned char*)malloc(AES_256_KEY_SIZE);

    int result1 = rsaEncrypt(key, pubkeyfile, encryptedkey);
    int result2 = rsaEncrypt(iv, pubkeyfile, encryptediv);

    serialize(encryptedkey, encryptediv);

    ///////tested here, this part working fine
    int r1 = rsaDecrypt(encryptedkey, privkeyfile, decryptedkey);
    int r2 = rsaDecrypt(encryptediv, privkeyfile, decryptediv);
    ///////////////////////////////

    unsigned char *getkey = (unsigned char*)malloc(RSA_KEY_Size);;
    unsigned char *getiv = (unsigned char*)malloc(RSA_KEY_Size);;

    deserialize(getkey, getiv);

    unsigned char *ikey = (unsigned char*)malloc(AES_256_KEY_SIZE);
    unsigned char *iiv = (unsigned char*)malloc(AES_256_KEY_SIZE);

    //////////tested here, failed to decrypt after taking encrypted data from a file/////////////////
    int r22 = rsaDecrypt(getiv, privkeyfile, iiv);
    int r21 = rsaDecrypt(getkey, privkeyfile, ikey);
    //////////////////////////////////////////////////
    return 0;
}

typedef struct item {
    uint8_t keyivlen;
    char keyiv[RSA_KEY_Size];
    struct item *next;
} list;

int serialize(unsigned char* key, unsigned char* iv)
{
    list *ptr;
    char *buffer;
    int listLength;
    list first, second;
    ptr = &first;

    FILE *filePtr;
    memcpy(first.keyiv, key, strlen(key));
    first.keyivlen = strlen(first.keyiv);
    first.next = &second;

    memcpy(second.keyiv, iv, strlen(iv));
    second.keyivlen = strlen(second.keyiv);
    second.next = 0;

    listLength = listSize(ptr);
    buffer = (char *)malloc(listLength);
    serializeList(ptr, buffer);
    filePtr = fopen("example.data", "wb+");
    fwrite(buffer, listLength, 1, filePtr);
    fclose(filePtr);
    free(buffer);
    return 0;
}

int deserialize(unsigned char* key, unsigned char* iv)
{
    FILE *filePtr;
    int listLength = 0;
    int done = 0;
    uint8_t arrayLen;
    unsigned char *buffer;
    int i = 0;
    listLength = fileSize("example.data");

    filePtr = fopen("example.data", "rb");
    while (done < listLength) {
        fread(&arrayLen, 1, 1, filePtr);
        buffer = (unsigned char *)malloc(arrayLen + 1);
        fread(buffer, arrayLen, 1, filePtr);
        buffer[arrayLen] = '\0';
        if (i == 0)
        {
            memcpy(key, buffer, arrayLen + 1);
        }
        else
        {
            memcpy(iv, buffer, arrayLen + 1);
        }
        //addToList(arrayLen, buffer);
        done += arrayLen + 1;
        i++;
        free(buffer);
    }
    //printList(start);

    return 0;
}

这是错误:

error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error
error:04065072:rsa routines:rsa_ossl_private_decrypt:padding check failed

它失败了:

    int r22 = rsaDecrypt(getiv, privkeyfile, iiv);
    int r21 = rsaDecrypt(getkey, privkeyfile, ikey);

出现此错误的原因是什么?

1 个答案:

答案 0 :(得分:1)

arrayLen仅为uint8_t,最多可容纳255个,因此您的反序列化数据太短。您需要RSA_KEY_Size个字节。