繁体   English   中英

从C中的文件读取64个字节的块

[英]Reading in 64 byte chunks from a file in C

我似乎无法全神贯注于这种读取​​64字节块的概念,然后使用河豚,

BF_cfb64_encrypt(source, dest, sizeof(source), &bf_key, iv, &enc, BF_DECRYPT) 

功能加密? 它。 我知道如何使用BF函数,但是从一个4096字节的文件中读取64字节是我的困惑。 任何提示或建议,将不胜感激。 我的理解是1个char是一个字节,所以这意味着我只保留一个计数,而当char计数为8时,则意味着我已读取64个字节,因此进行加密,然后写入文件,并重复进行直到整个文件被解析为止。 ?

为什么不使用读取系统调用?

必需的包含文件

#include <unistd.h>

功能定义

size_t read(int fildes, void *buf, size_t nbytes);

参量

int fildes :读取输入位置的文件描述符。 您可以使用从开放系统调用获得的文件描述符,也可以使用0、1或2分别引用标准输入,标准输出或标准错误。

const void * buf :将存储读取内容的字符数组。

size_t nbytes :截断数据之前要读取的字节数。 如果要读取的数据小于nbytes,则所有数据都保存在缓冲区中。

返回值 :返回已读取的字节数。 如果value为负,则系统调用返回错误。

样品

#include <unistd.h>
int main()
{
   char data[128];

   if(read(0, data, 128) < 0)
       write(2, "An error occurred in the read.\n", 31);

   exit(0);
}

**伪代码**

int no_byte_read = 0; // Will store number of byte read
void *buffer; //temporary storage for read data   
FILE *fp; //File pointer of the file to be read 

buffer = (void*) malloc(64); //Allocate space to temporary buffer.

fp = open file to be read;

do{
      no_byte_read = read(fp, buffer, 64); // read 64 byte from file and store in buffer
      if(no_byte_read < 0){
           printf("Error occoured in read");
           break;
      }
   }while(no_byte_read == 64) //If this condition is true that means still some bytes
                                remain in file which must be read.  

首先,可能需要熟悉流密码。 Blowfish使用64 的块大小进行加密/解密; 不是字节。 只要您了解所指的64个“字节”是您的要求,而不是Blowfishes,并且Blowfish仅需要8个字节的块。

也就是说,循环通过一个文件的大小是算法块大小的倍数的循环,一次提取一个64字节帧的解密数据是可行的。

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <openssl/blowfish.h>


int main(int argc, char *argv[])
{
    if (argc < 2)
        return EXIT_FAILURE;

    FILE * fp = fopen(argv[1], "rb");
    if (fp == NULL)
    {
        perror(argv[1]);
        return EXIT_FAILURE;
    }

    // your key bytes would be here (obviously).
    unsigned char key[16] = "1234567890123456";
    int key_len = sizeof(key)-1;

    // setup the key schedule.
    BF_KEY bf_key;
    BF_set_key(&bf_key, key_len, key);

    // and setup the initialization vector. normally the IV is
    //  randomly generated when encrypting, then stored as the
    //  lead 8 bytes of ciphertext output. this assumes you're
    //  iv is static (all zeros) similar to SSH
    unsigned char iv[8] = {0};
    int n = 0;

    // finally, begin reading the data in chunks of 64 bytes
    //  sending it through the blowfish algorithm
    unsigned char source[64];
    unsigned char dest[64];
    while (fread(source, sizeof(source), 1, fp) == 1)
    {
        BF_cfb64_encrypt(source, dest, sizeof(dest), &bf_key, iv, &n, BF_DECRYPT);

        // do something with your dest[] plaintext block
    }
    fclose(fp);

    return 0;
}

该示例相当琐碎,但是它带来了关于对称块算法和填充的一些您可能没有考虑的事情(或者您可能已经考虑过,并且与该问题完全无关)。

像Blowfish这样的对称块算法按块大小运行。 在河豚的情况下,块大小为64 (8字节)。 这意味着加密/解密操作始终以64位大小的块进行。 如果您使用的是较低级别的openssl api,则必须加密(和解密)不超过该块的数据。 更高级别的API(例如BF_cfb64_encrypt )旨在允许“流模式”,这意味着您可以按更大的块提交数据,只要它们的大小是块大小的倍数即可。 在链接的API连续调用之间保留ivn值。

最后,我开始写有关对称块算法和填充模式的文章,但是意识到这并不适合这个问题,所以我只能建议您对其进行研究。 我怀疑您有时需要这样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM