在C中将文件拆分为固定大小的块

时间:2012-12-20 15:48:46

标签: c file split partitioning

我被要求将文件拆分为固定大小的块来进行文件加密。更具体地说,我有很多文件,可以是二进制文件或文本文件。

我被要求编写一个用户程序,将这些文件作为输入,将每个文件分成多个32位块,然后发送出32位块。

方案是用户通过将文件拆分为块,加密块(使用RSA),然后将密文发送到服务器来备份远程服务器中的文件。

考虑两个文件,一个是33位文本文件A,另一个是34位二进制文​​件B. A可以划分为两个32位块A1和A2(A2的最后31位都是0) B可以被分成两个32位块B1和B2(B2的最后30个二进制都是0)。 如果我获得A1(或A2,B1,B2),那么我将A1视为32位整数并且可以进行RSA加密。

我能够为RSA加密编写代码,但遗憾的是我不知道如何编写C代码来获取A1,A2,B1,B2。

有人可以帮我写一个示例代码或给我一些参考吗?

2 个答案:

答案 0 :(得分:0)

以下是您可以轻松转换为C代码的高级算法:

char* get_Next_Block_From_File(FILE *fp, int seek_offset, int blockSize)
{
    // Open file
    // lseek to seek_offset

    len = blockSize / 8;
    bit_pos = blockSize % 8;

    for (i=0; i<len; i++) {
        copy_from_file_to_local_buffer_byte_by_byte();
    }

    if (bit_pos) {
        byte[i] <<= (8 - bit_pos);
        append_byte_to_local_buffer();
    }

    return local_buffer;
}

答案 1 :(得分:0)

C Program for splitting the given file into fixed size blocks

// split.cpp : main project file.

#include "stdafx.h"

using namespace System;

#include <stdio.h>
#include <string.h>
#include <conio.h>

#define SEGMENT 600 //approximate target size of small file

long file_size(char *name);//function definition below
int splitFile(char *fp1, size_t maxSize);


long file_size(char *name)
{
    FILE *fp = fopen(name, "rb"); //must be binary read to get bytes

    long size=-1;
    if(fp)
    {
        fseek (fp, 0, SEEK_END);
        size = ftell(fp)+1;
        fclose(fp);
    }
    return size;
}

/*
**  splitFile
**  Splits an existing input file into multiple output files with a specified
**  maximum file size.
**
**  Return Value:
**  Number of created result files, or 0 in case of bad input data or a negative
**  value in case of an error during file splitting.
*/
int splitFile()
{
    int result = 0;

    char buffer[1024];//change see //2098 * 16
    size_t size;
    size_t read;
    size_t written;

    int segments=0, i, len, accum;

    //FILE *fp1, *fp2;

    char filename[260]={""};
    //char filename[260]={"D:\\smallFileName_"};//base name for small files.
    char largeFileName[]={"D:\\Payload_data\\Datafile_to_split.txt"};//change to your path
    long sizeFile = file_size(largeFileName);
    char smallFileName[260];
    char line[1024];
    long maxSize= sizeFile/SEGMENT + 1;
    int filecounter=1;

    FILE *fIn;
    FILE *fOut;
    fIn = fopen(largeFileName, "rb");

    if ((fIn != NULL) && (maxSize > 0))
    {
       // fIn = fopen(fIn, "rb");
        if (fIn != NULL)
        {
            fOut = NULL;
            result = 1;   /* we have at least one part */

            // Splitting Data from pchar into multiple files
                for(i=1; i<25 ;i++)
                    {               
                        sprintf(smallFileName, "%s%d.txt", filename, i);                      
                         printf("\n File number %d",i);
                         while (!feof(fIn))
                         {
                         /* initialize (next) output file if no output file opened */
                              if (fOut == NULL)
                              {  
                                 filecounter++;

                                sprintf(buffer, "smallFileName_%d", filecounter);
                                fOut = fopen(buffer, "wb");
                             if (fOut == NULL)
                             { 
                                 result *= -1;   
                                 break; 
                             }

                                size = 0;
                             }

                /* calculate size of data to be read from input file in order to not exceed maxSize */
                read = sizeof(buffer);
                if ((size + read) > maxSize)
                {
                    read = maxSize - size;
                }

                /* read data from input file */
                read = fread(buffer, 1, read, fIn);
                if (read == 0)
                {
                    result *= -1;
                    break;
                }

                /* write data to output file */
                written = fwrite(buffer, 1, read, fOut);

                if (written != read)
                {
                    result *= -1;
                    break;
                }

                /* update size counter of current output file */
                size += written;
                if (size >= maxSize)   /* next split? */
                {
                    fclose(fOut);
                    fOut = NULL;
                    result++;
                }
            }

            /* clean up */
            if (fOut != NULL)
            {
                fclose(fOut);
            }
            fclose(fIn);
        }
    } 
}
    return (result);
}


int main(void)
{


    //segments = sizeFile/SEGMENT + 1;//ensure end of file


    //fp1 = fopen(largeFileName, "r");

    ////if(fp1)
    ////{
    //    for(i=0;i<segments;i++)
    //    {
    //        accum = 0;
    //        sprintf(smallFileName, "%s%d.txt", filename, i);
    //        fp2 = fopen(smallFileName, "w");
    //        if(fp2)
    //        {
    //            while(fgets(line, 1080, fp1) && accum <= SEGMENT)
    //            {
    //                accum += strlen(line);//track size of growing file
    //                fputs(line, fp2);
    //            }
    //            fclose(fp2);
    //        }
    //    }
    //    fclose(fp1);
    //}


    //maxSize =sizeFile/SEGMENT + 1;
    //ensure end of file
    splitFile();

    getch();
    printf("\n File splitted Successfully");
    return 0;
}