Mcrypt 库将结果写入文件

发布于 2024-11-05 02:13:33 字数 1426 浏览 0 评论 0原文

我正在尝试将加密数据写入文件。然而,当将其读回程序并尝试解密时,我只得到垃圾。如果不将其写入文件,它似乎可以工作..我做错了什么?

这是代码:

MCRYPT td, td2;    
char * string = "My secret message";
int i;
char *key; /* created using mcrypt_gen_key */
char *IV;
char * block_buffer;
int blocksize;
int keysize = 32; /* 192 bits == 24 bytes */
key = calloc(1, keysize);
strcpy(key, "This-is-my-key#########");
td = mcrypt_module_open("saferplus", NULL, "cbc", NULL);
td2 = mcrypt_module_open("saferplus", NULL, "cbc", NULL);

blocksize = mcrypt_enc_get_block_size(td);
block_buffer = malloc(blocksize);
IV=malloc(mcrypt_enc_get_iv_size(td));
for (i=0; i < mcrypt_enc_get_iv_size(td); i++) {
IV[i]=rand();
}
mcrypt_generic_init(td, key, keysize, IV);
mcrypt_generic_init(td2, key, keysize, IV);
strcpy(block_buffer, string);

printf("1: %s\n", block_buffer);
mcrypt_generic (td, block_buffer, blocksize);

FILE *myFile;   
myFile = fopen("encrypted","ab");
fwrite(block_buffer, 1, blocksize, myFile);
fclose(myFile);
printf("2: %s\n", block_buffer);

myFile = fopen("encrypted","rb");
fread(block_buffer, 1, blocksize, myFile);
fclose(myFile);

printf("2.5: %s\n", block_buffer);

mdecrypt_generic (td2, block_buffer, blocksize);
printf("3: %s\n", block_buffer);


/* deinitialize the encryption thread */
mcrypt_generic_deinit (td);
mcrypt_generic_deinit(td2);
/* Unload the loaded module */
mcrypt_module_close(td);
mcrypt_module_close(td2);

return 0;

I'm trying to write encrypted data to a file. However, when reading it back into the program and trying to decrypt it, I only get garbage back out. Without writing it to a file it seems to work.. What am I doing wrong?

Here's the code:

MCRYPT td, td2;    
char * string = "My secret message";
int i;
char *key; /* created using mcrypt_gen_key */
char *IV;
char * block_buffer;
int blocksize;
int keysize = 32; /* 192 bits == 24 bytes */
key = calloc(1, keysize);
strcpy(key, "This-is-my-key#########");
td = mcrypt_module_open("saferplus", NULL, "cbc", NULL);
td2 = mcrypt_module_open("saferplus", NULL, "cbc", NULL);

blocksize = mcrypt_enc_get_block_size(td);
block_buffer = malloc(blocksize);
IV=malloc(mcrypt_enc_get_iv_size(td));
for (i=0; i < mcrypt_enc_get_iv_size(td); i++) {
IV[i]=rand();
}
mcrypt_generic_init(td, key, keysize, IV);
mcrypt_generic_init(td2, key, keysize, IV);
strcpy(block_buffer, string);

printf("1: %s\n", block_buffer);
mcrypt_generic (td, block_buffer, blocksize);

FILE *myFile;   
myFile = fopen("encrypted","ab");
fwrite(block_buffer, 1, blocksize, myFile);
fclose(myFile);
printf("2: %s\n", block_buffer);

myFile = fopen("encrypted","rb");
fread(block_buffer, 1, blocksize, myFile);
fclose(myFile);

printf("2.5: %s\n", block_buffer);

mdecrypt_generic (td2, block_buffer, blocksize);
printf("3: %s\n", block_buffer);


/* deinitialize the encryption thread */
mcrypt_generic_deinit (td);
mcrypt_generic_deinit(td2);
/* Unload the loaded module */
mcrypt_module_close(td);
mcrypt_module_close(td2);

return 0;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮颜 2024-11-12 02:13:33

这有效。我认为你只需要将 block_buffer 初始化为零。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mcrypt.h>

int main()
{
    MCRYPT td, td2;
    char * string = "My secret message";
    int i;
    char *key; /* created using mcrypt_gen_key */
    char *IV;
    char * block_buffer;
    int blocksize;
    int keysize = 32; /* 192 bits == 24 bytes */
    FILE *myFile;

    key = calloc(1, keysize);
    strcpy(key, "This-is-my-key#########");
    td = mcrypt_module_open("saferplus", NULL, "cbc", NULL);
    td2 = mcrypt_module_open("saferplus", NULL, "cbc", NULL);

    blocksize = mcrypt_enc_get_block_size(td);
    block_buffer = calloc(1, blocksize); /* Fixed issue here */
    IV = malloc(mcrypt_enc_get_iv_size(td));
    if ((block_buffer == NULL) || (IV == NULL)) {
            fprintf(stderr, "Failed to allocate memory\n");
            exit(EXIT_FAILURE);
    }
    for (i = 0; i < mcrypt_enc_get_iv_size(td); i++) {
            IV[i] = rand();
    }
    mcrypt_generic_init(td, key, keysize, IV);
    mcrypt_generic_init(td2, key, keysize, IV);
    strcpy(block_buffer, string);

    printf("1: %s\n", block_buffer);
    mcrypt_generic (td, block_buffer, blocksize);

    myFile = fopen("encrypted","w");
    if ((myFile == NULL) || (fwrite(block_buffer, blocksize, 1, myFile) != 1)) {
            fprintf(stderr, "Failed to write data\n");
            exit(EXIT_FAILURE);
    }
    fclose(myFile);
    printf("2: %s\n", block_buffer);

    myFile = fopen("encrypted","r");
    if ((myFile == NULL) || (fread(block_buffer, blocksize, 1, myFile) != 1)) {
            fprintf(stderr, "Failed to read data\n");
            exit(EXIT_FAILURE);
    }
    fclose(myFile);

    printf("2.5: %s\n", block_buffer);

    mdecrypt_generic (td2, block_buffer, blocksize);
    printf("3: %s\n", block_buffer);

    /* deinitialize the encryption thread */
    mcrypt_generic_deinit (td);
    mcrypt_generic_deinit(td2);
    /* Unload the loaded module */
    mcrypt_module_close(td);
    mcrypt_module_close(td2);

    return 0;
}

This works. I think you just needed to zero initialize block_buffer.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mcrypt.h>

int main()
{
    MCRYPT td, td2;
    char * string = "My secret message";
    int i;
    char *key; /* created using mcrypt_gen_key */
    char *IV;
    char * block_buffer;
    int blocksize;
    int keysize = 32; /* 192 bits == 24 bytes */
    FILE *myFile;

    key = calloc(1, keysize);
    strcpy(key, "This-is-my-key#########");
    td = mcrypt_module_open("saferplus", NULL, "cbc", NULL);
    td2 = mcrypt_module_open("saferplus", NULL, "cbc", NULL);

    blocksize = mcrypt_enc_get_block_size(td);
    block_buffer = calloc(1, blocksize); /* Fixed issue here */
    IV = malloc(mcrypt_enc_get_iv_size(td));
    if ((block_buffer == NULL) || (IV == NULL)) {
            fprintf(stderr, "Failed to allocate memory\n");
            exit(EXIT_FAILURE);
    }
    for (i = 0; i < mcrypt_enc_get_iv_size(td); i++) {
            IV[i] = rand();
    }
    mcrypt_generic_init(td, key, keysize, IV);
    mcrypt_generic_init(td2, key, keysize, IV);
    strcpy(block_buffer, string);

    printf("1: %s\n", block_buffer);
    mcrypt_generic (td, block_buffer, blocksize);

    myFile = fopen("encrypted","w");
    if ((myFile == NULL) || (fwrite(block_buffer, blocksize, 1, myFile) != 1)) {
            fprintf(stderr, "Failed to write data\n");
            exit(EXIT_FAILURE);
    }
    fclose(myFile);
    printf("2: %s\n", block_buffer);

    myFile = fopen("encrypted","r");
    if ((myFile == NULL) || (fread(block_buffer, blocksize, 1, myFile) != 1)) {
            fprintf(stderr, "Failed to read data\n");
            exit(EXIT_FAILURE);
    }
    fclose(myFile);

    printf("2.5: %s\n", block_buffer);

    mdecrypt_generic (td2, block_buffer, blocksize);
    printf("3: %s\n", block_buffer);

    /* deinitialize the encryption thread */
    mcrypt_generic_deinit (td);
    mcrypt_generic_deinit(td2);
    /* Unload the loaded module */
    mcrypt_module_close(td);
    mcrypt_module_close(td2);

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文