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

时间:2013-09-08 16:38:35

标签: c algorithm blowfish

我似乎无法围绕这个以64字节块读取的概念,然后使用河豚,

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

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

2 个答案:

答案 0 :(得分:0)

为什么不使用read系统调用?

必须包含文件

#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.  

答案 1 :(得分:0)

首先,您可能需要熟悉流密码。 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这样的对称块算法以块大小运行。在Blowfish的情况下,块大小为64 (8字节)。这意味着加密/解密操作总是以64位大小的块发生。如果您使用较低级别的openssl apis,则必须加密(并解密)不大于该块的数据。更高级别的API(例如BF_cfb64_encrypt)旨在允许“流模式”,这意味着您可以以更大的块提交数据,只要它们的大小是块大小的倍数即可。 在链接的API连续调用之间保留ivn值。

最后,我开始编写关于对称块算法和填充模式的这个相当长的dia骂,但是意识到这不适合这个问题,所以我只建议你研究它们。我怀疑你在某个时候需要。