简体   繁体   English

C++ - 将任何文件的字节读入无符号字符数组

[英]C++ - Read the bytes of any file into an unsigned char array

I have an assignment where I have to implement the Rijndael Algorithm for AES-128 Encryption.我有一个任务,我必须为 AES-128 加密实施 Rijndael 算法。 I have the algorithm operational, but I do not have proper file input/output.我有算法可操作,但我没有正确的文件输入/输出。

The assignment requires us to use parameters passed in from the command line.赋值要求我们使用从命令行传入的参数。 In this case, the parameter will be the file path to the particular file the user wishes to encrypt.在这种情况下,参数将是用户希望加密的特定文件的文件路径。

My problem is, I am lost as to how to read in the bytes of a file and store these bytes inside an array for later encryption.我的问题是,我不知道如何读取文件的字节并将这些字节存储在数组中以供以后加密。

I have tried using ifstream and ofstream to open, read, write, and close the files and it works fine for plaintext files.我曾尝试使用 ifstream 和 ofstream 来打开、读取、写入和关闭文件,它适用于纯文本文件。 However, I need the application to take ANY file as input.但是,我需要应用程序将任何文件作为输入。

When I tried my method of using fstream with a pdf as input, it would crash my program.当我尝试使用 fstream 和 pdf 作为输入的方法时,它会使我的程序崩溃。 So, I now need to learn how to take the bytes of a file, store them inside an unsigned char array for Encryption, and then store them inside another file.所以,我现在需要学习如何获取文件的字节,将它们存储在 unsigned char 数组中以进行加密,然后将它们存储在另一个文件中。 This process of encryption and storage of ciphertext needs to occur in 16 byte intervals.这个加密和存储密文的过程需要以 16 个字节为间隔进行。

The below implementation is my first attempt to read files in binary mode and then write whatever was read in another file also in binary mode.下面的实现是我第一次尝试以二进制模式读取文件,然后以二进制模式写入另一个文件中读取的任何内容。 The output is readable in a hex reader. output 可在十六进制阅读器中读取。

int main(int argc, char* argv[])
{
    if (argc < 2)
    {
        cerr << "Use: " << argv[0] << " SOURCE_FILEPATH" << endl << "Ex. \"C\\Users\\Anthony\\Desktop\\test.txt\"\n";
        return 1;
    }

    // Store the Command Line Parameter inside a string
    // In this case, a filepath.
    string src_fp = argv[1];
    string dst_fp = src_fp.substr(0, src_fp.find('.', 0)) + ".enc";

    // Open the filepaths in binary mode
    ifstream srcF(src_fp, ios::in | ios::binary);
    ofstream dstF(dst_fp, ios::out | ios::binary);

    // Buffer to handle the input and output.
    unsigned char fBuffer[16];

    srcF.seekg(0, ios::beg);
    while (!srcF.eof())
    {
        srcF >> fBuffer;

        dstF << fBuffer << endl;
    }

    dstF.close();
    srcF.close();
}

The code implementation does not work as intended.代码实现未按预期工作。

Any direction on how to solve my dilemma would be greatly appreciated.任何有关如何解决我的困境的方向将不胜感激。

Like you, I really struggled to find a way to read a binary file into a byte array in C++ that would output the same hex values I see in a hex editor.像你一样,我真的很难找到一种方法将二进制文件读入 C++ 中的字节数组,这将使 output 与我在十六进制编辑器中看到的相同十六进制值。 After much trial and error, this seems to be the fastest way to do so without extra casts.经过多次试验和错误,这似乎是无需额外演员的最快方法。

It would go faster without the counter, but then sometimes you end up with wide chars.如果没有计数器,它会更快 go ,但有时你会得到宽字符。 To truly get one byte at a time I haven't found a better way.要真正一次获得一个字节,我还没有找到更好的方法。

By default it loads the entire file into memory, but only prints the first 1000 bytes.默认情况下,它将整个文件加载到 memory 中,但只打印前 1000 个字节。

string Filename = "BinaryFile.bin";
FILE* pFile;
pFile = fopen(Filename.c_str(), "rb");
fseek(pFile, 0L, SEEK_END);
size_t size = ftell(pFile);
fseek(pFile, 0L, SEEK_SET);
uint8_t* ByteArray;
ByteArray = new uint8_t[size];
if (pFile != NULL)
{
    int counter = 0;
    do {
        ByteArray[counter] = fgetc(pFile);
        counter++;
    } while (counter <= size);
    fclose(pFile);
}
for (size_t i = 0; i < 800; i++) {
    printf("%02X ", ByteArray[i]);
}

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

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