使用 RSA_private_decrypt() 解密二进制文件

Decrypt binary file with RSA_private_decrypt()

我目前是一名计算机科学专业的学生,​​我遇到了一个我无法解决的问题。

我必须使用函数 RSA_private_decrypt() 解密文件 "cipheredkey.bin"。因此我只有私钥 "privkey.pem".

这是我的代码:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

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


EVP_PKEY* readEVPkey(const char* keyfile)
{
        EVP_PKEY *evp_key;
        FILE *fp;

        fp = fopen(keyfile, "rb");

        if (fp == NULL) {
                printf("Unable to open file %s\n", keyfile);
        } else {
                printf("Opened file %s\n", keyfile);
        }

        evp_key = PEM_read_PrivateKey(fp, NULL, NULL, NULL);

        if (evp_key == NULL) {
                printf("EVP_PKEY is NULL\n\n");
        } else {
                printf("EVP_PKEY not NULL\n\n");
        }

        fclose(fp);
        return evp_key;
}


int main (int argc, char const *argv[])
{
        int                     flen                    = NULL;
        int                     padding                 = RSA_PKCS1_OAEP_PADDING;
        const unsigned char     *decrypted_msg          = NULL;
        unsigned char           *encrypted_msg          = NULL;
        int                     decrypted_length        = NULL;

        char                    *err                    = NULL;

        EVP_PKEY                *evp                    = NULL;
        RSA                     *privkey                = NULL;




        evp = readEVPkey("privkey.pem");
        privkey = EVP_PKEY_get1_RSA(evp);        // convert evp to rsa key

        flen = RSA_size(privkey);

        encrypted_msg = malloc(RSA_size(privkey));
        printf("size of encrypted_msg: %d\n", strlen(encrypted_msg));
        FILE *in = fopen("cipheredkey.bin", "r+");
        if (in == NULL) {
                printf("File cipheredkey.bin not found.\n");
        } else {
                fwrite(encrypted_msg,sizeof(*encrypted_msg), RSA_size(privkey), in);
                printf("encrypted_msg: %s\n", encrypted_msg);
        }

        decrypted_msg = malloc(sizeof(encrypted_msg));


        decrypted_length = RSA_private_decrypt(flen, decrypted_msg, encrypted_msg, privkey, padding);
        printf("1) decrypted_length: %i\n", decrypted_length);


        if (decrypted_length == -1) {
                ERR_load_crypto_strings();

                printf("FAIL: %s\n",ERR_error_string(ERR_get_error(),NULL));

        } else {
                printf("1) decrypted_length: %i\n", decrypted_length);
                printf("Decrypted message: %s\n", decrypted_msg);
        }
}

Output: FAIL: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error

将二进制文件读入 char* 时可能有问题,或者我的变量大小可能有问题。

这是二进制文件"cipheredkey.bin"。用猫阅读

���;PۘU;PۘU;PۘUsJT3NOVG3GyH0hqTNShmAz8LLQChDvq2PyyaA/0SZTNJcYp15Mpyj0xHzF5wKz
TMJJCLb0C3WzHFD32B89YpRu6UZs+q4s+R1xZAFgN3+GHTYAfYA0f+1m0NLg+0PW
DCSTayS/jhRiukIcthFu2htI9e41/vgX5FME3OcXuEeGxwsyA6APgN0mbOiU/9fU
x8a0ayfLKZ1k/Y6UTtODu2TeH63Q1gHG5Y36MK0pNNnbjmukXkosevbvbEqT5lS8
ILlK/F+PLYA3Qpsr+ayBaWI/NNoJM5asW0WvPq7RSaDaVzJXON16p9beu5Vw3A0+
yqUHRR6qk6d9W3ubyGvtdRtfRk1stE2BX+JxdL68VKPDnAbJFT8vMcGz/K63dT9B
CWm0caCXOaaXf4tT3RvUlUvKvtvPNxw2RRL0cWl/kEjeK2thh44syaNd1Lze4Sxn
mVkitiS0z/lZtoJNx6AWHBFzTC3zhHORtcJVOv5jR+d/HO78R/R95zjeDjFZ/UfS
liPorxu7W1ziOmpWgXx5H5p3ExqjEZXcMJaoR7qsgmHQ5HJCdCSvqbES24DfSsYT
TyPwB6IzmRf6fhD7RVScBIIRWl6TZ3urgfZoDxxmsu1hWVPNYU5hudxf1tFHIWCF
boJWTT19Lo/GbuFRZvV8IgretFJddbeGUSHHhyrthlVsnb8g08MPQFdwxCao4D4y
/20eQvBq1N4LayU28+pkxLJxImPLqjA8dBBzi70ehG9bGx2g3j/JvKdqZLN52CPg
50AMl1acBQMlj5phf6r0D5TRchd5eboeTFe4dZtDJ7RNIEu9IOcj/od9bweJpVKT
e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被你的代码弄得头破血流。

你似乎调换了 decrypted_msg 和 encrypted_msg;

所以改变

RSA_private_decrypt(flen, decrypted_msg, encrypted_msg, privkey, padding);

RSA_private_decrypt(flen, encrypted_msg, decrypted_msg, privkey, padding);

对我来说现在 运行,但结果是垃圾。 希望现在还不算太晚,它会有所帮助。