2

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

我被要求编写一个用户程序,将这些文件作为输入,将每个文件拆分为多个 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。

谁能帮我写一个示例代码或给我一些参考?

4

2 回答 2

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;
}
于 2012-12-21T06:12:52.417 回答
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;
}
于 2017-05-10T12:02:52.660 回答