3
암호화 된 데이터를 파일에 쓰려고합니다. 그러나 프로그램으로 다시 읽어 들여서 해독하려고 할 때만 쓰레기를 꺼냅니다. 그것을 파일에 쓰지 않고도 작동하는 것 같습니다. 내가 뭘 잘못하고 있니?파일에 대한 Mcrypt 라이브러리 쓰기 결과
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'의 hexdump와 메모리상의 버퍼를 비교 했습니까? –
감사합니다 뮤, 나는 처음에 그것을 사용하여 간단히 열었습니다. 포맷으로 인해 일부 정보가 손실 될 수 있다고 생각 했으므로 변경 사항을 저장할지 여부를 확인하기 위해 바이너리로 저장하려고했습니다. hexdump를 만들려면 어떻게해야합니까? 튜토리얼에 대한 링크로 충분할 것입니다 .. –