我想读取二进制文件,操作64位加密逻辑并将返回的文件保存在二进制文件中。由于加密操作2个4字节整数块,我读取文件两次并转换为long int。但是当我尝试使用相同的进程解密时,我看到我从加密输出中保存的内容是错误的。到目前为止,这是我的代码。
while (fread(buffer, 1, 4, src) == 4) {
//convert char to long
message_left = buffer[3] | (unsigned long) (buffer[2] << 8) | (unsigned long) (buffer[1] << 16) | (unsigned long)(buffer[0] << 24);
fread(buffer, 1, 4, src);
//convert char to long
message_right = buffer[3] | (unsigned long) (buffer[2] << 8) | (unsigned long) (buffer[1] << 16) | (unsigned long)(buffer[0] << 24);
//encrypt
Encrypt(ctx,&message_left,&message_right);
//convert back to char and save
buffer[3] = (unsigned char)message_left;
buffer[2] = (unsigned char)(message_left >> 8);
buffer[1] = (unsigned char)(message_left >> 16);
buffer[0] = (unsigned char)(message_left >> 24);
fwrite(buffer,4,1,dest);
buffer[3] = message_rigt;
buffer[2] = (unsigned char)(message_right >> 8);
buffer[1] = (unsigned char)(message_right >> 16);
buffer[0] = (unsigned char)(message_right >> 24);
fwrite(buffer,4,1,dest);
}
答案 0 :(得分:0)
这样的事情会起作用吗?
unsigned long n[2];
while (fread(n, sizeof(n), 1, src) == 1) {
Encrypt(ctx, &n[0], &n[1]);
fwrite(n, sizeof(n), 1, dest);
}