Mcrypt 库将结果写入文件
Posted
技术标签:
【中文标题】Mcrypt 库将结果写入文件【英文标题】:Mcrypt library writing result to file 【发布时间】:2011-08-18 03:11:12 【问题描述】:我正在尝试将加密数据写入文件。但是,当将其读回程序并尝试对其进行解密时,我只会将垃圾回收。如果不将其写入文件,它似乎可以工作..我做错了什么?
代码如下:
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;
【问题讨论】:
为什么要打开文件进行追加(myFile = fopen("encrypted","ab");
)而不是简单的写入?运行程序时文件是否已经存在?您是否将encrypted
的十六进制转储与您的内存缓冲区进行了比较?
谢谢 mu,我第一次打开它时只需使用 a.我想可能是因为格式化而丢失了一些信息,所以我想尝试以二进制文件保存并打开它,看看它是否会有所作为。我该如何做一个hexdump?一个教程的链接就足够了..
【参考方案1】:
这行得通。我认为您只需要将 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;
【讨论】:
以上是关于Mcrypt 库将结果写入文件的主要内容,如果未能解决你的问题,请参考以下文章
使用 FFMPEG 将可搜索的 AAC 音频流写入 MP4 文件
将.3gp文件写入.wav格式python Flask服务器
是否可以使用 Expo 将文件写入 iOS 文件系统,以便可以通过 iOS 11 Files 应用程序访问它