C ++:fread每次读取不同的大小

时间:2019-05-17 11:26:31

标签: c++ visual-studio file encryption openssl

这是我的代码:

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

#define KEY_LENGTH  2048
#define PUB_EXP     3
#define PRINT_KEYS
#define WRITE_TO_FILE

int main(void) {

    size_t pri_len;            // Length of private key
    size_t pub_len;            // Length of public key
    char   *pri_key;           // Private key
    char   *pub_key;           // Public key
    char   *msg = (char *)malloc(256);  // Message to encrypt
    char   *encrypt = NULL;    // Encrypted message
    char   *decrypt = NULL;    // Decrypted message
    char   *err = NULL;               // Buffer for any error messages
    size_t red;

    RSA *keypair = RSA_generate_key(KEY_LENGTH, 3, NULL, NULL);
    FILE *out;
    FILE *in;

    char *direct = (char*)malloc(256);


    if (fopen_s(&in, "test.txt", "r") != 0)
    {
        printf("in Error is %d.\n", GetLastError());
        return -1;
    }
    if (fopen_s(&out, "etest.txt", "w") != 0)
    {
        printf("out Error is %d.\n", GetLastError());
        return -1;
    }

    encrypt = (char *)malloc(RSA_size(keypair));

    setvbuf(out, encrypt, _IOFBF, RSA_size(keypair));

    for (;;)
    {
        red = fread(msg, 1, RSA_size(keypair) - 42, in);
        std::cout << "encrypt read -> " << red << std::endl;
        if (red == 0) break;
        if (((red = RSA_public_encrypt(RSA_size(keypair) - 42, (unsigned char*)msg, (unsigned char*)encrypt,
            keypair, RSA_PKCS1_OAEP_PADDING))) == -1) {
            ERR_load_crypto_strings();
            ERR_error_string(ERR_get_error(), err);
            fprintf(stderr, "Error encrypting message: %d\n", err);
        }
        std::cout << "encrypted size -> " << red << std::endl;
        if (fwrite(encrypt, 1, red, out) == 1)
        {
            printf("fwrite Error is %d (%d).\n", errno);
        }

    }

    fclose(in);
    fclose(out);

    if (fopen_s(&in, "etest.txt", "r") != 0)
    {
        printf("in Error is %d (%s).\n", errno);
    }
    if (fopen_s(&out, "dtest.txt", "w") != 0)
    {
        printf("out Error is %d (%s).\n", errno);
    }


    decrypt = (char *)malloc(RSA_size(keypair) - 42);

    for (;;)
    {
        red = fread(msg, 1, RSA_size(keypair), in);

        std::cout << "red decrypt -> " << red << std::endl;

        if (red < RSA_size(keypair)) break;
        if ((red = RSA_private_decrypt(red, (unsigned char*)msg, (unsigned char*)decrypt,
            keypair, RSA_PKCS1_OAEP_PADDING)) == -1) {

            ERR_load_crypto_strings();
            ERR_error_string(ERR_get_error(), err);
            fprintf(stderr, "Error decrypting message: %d\n", err);
        }

        fwrite(decrypt, 1, red, out);
        std::cout << decrypt << std::endl;



    }
    fclose(in);
    fclose(out);
    RSA_free(keypair);
    return 0;
}

这个想法很简单,我在GIO中有一个单词test.txt,打开它并使用生成的RSA密钥进行加密,然后将加密后的数据写入{{1 }},然后打开它,尝试解密并将解密的数据写入etest.txt。这是出于教育目的的简单示例。

我遇到的问题是,当我加密数据dtest.txt返回RSA_public_encrypt时,这很棒,我将256个字节写入256中。但是,当我尝试打开同一文件进行解密并从中读取日志消息etest.txt时,返回的金额不同。例如,有时是std::cout << "red decrypt -> " << red << std::endl;2447,有时甚至是50。对于256,我打开256,然后看到数据

dtest.txt

这不是我加密的。所以我的问题是为什么我每次都从GIOÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ读取不同数量的数据?以及为什么我读取etest.txt字节时会得到错误的解密数据?

0 个答案:

没有答案