[英]How to read 16 bytes from a file at a time and store them into an array
我試圖一次從文件中讀取 16 個字節,並且每 16 個字節必須加密並寫入 output 文件。 假設加密 function shift_encrypt
正在工作,我如何一次將其應用於 16 個字節。 目前我正在嘗試將字節存儲在數組plaintext
中,但它不起作用
void encryption(char filename[MAX_PATH_LEN], char password[CIPHER_BLOCK_SIZE + 1]) {
char output[256];
snprintf(output, sizeof(output), "%s.ecb", filename);
FILE *output_stream = fopen(output, "wb");
if (output_stream == NULL) {
perror(output);
}
FILE *input_stream = fopen(filename, "rb");
if (input_stream == NULL) {
perror(filename);
}
char plaintext[17];
while (fread(plaintext, 16, 1, input_stream) != 0) {
fwrite(shift_encrypt(plaintext, password), 16, 1, output_stream);
}
fclose(output_stream);
}
如何一次從文件中讀取 16 個字節並將它們存儲到數組中(?)
代碼在這方面做得很好。
假設它是一個字符串,我的代碼可以工作嗎? 因為我在運行它時遇到堆棧緩沖區溢出錯誤。 例如,我正在讀取包含“AAAABBBBCCCCDDDDE”的文本文件,密碼為“passwordpassword”
假設是不正確的。
考慮一下為什么代碼是plaintext[]
大小為 17,但從未分配plaintext[16]
並且只讀取 16。代碼不是讀取“AAAABBBBCCCCDDDDE”,而只有“AAAABBBBCCCCDDDD”,並且從未分配plaintext[]
的最后(第 17 個)字符。
試試char plaintext[17]; = { 0 };
char plaintext[17]; = { 0 };
初始化數組。 那么字符plaintext[16]
將是一個null 字符,並且在成功fread(plaintext, 16, 1, input_stream)
后, plainttext[]
將是一個適合shift_encrypt(plaintext, password)
的字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.