首页 > 解决方案 > 使用 EOT 的 Playfair 算法解密问题

问题描述

我正在尝试使用 16x16 矩阵实现 playfair 算法。目前,如果没有一对字符可以将自己加密为 ascii 表中的 EOT 字符,那么我在加密和解密文件时没有问题。

下面是我用于读取 2 个字符的当前代码:

    ifstream input_file(file);
    ofstream output_file("out_d.txt");

    if (input_file.is_open()) {
        char A, B;

        //get total number of characters in the file
        input_file.seekg(0, input_file.end);
        int length = input_file.tellg();
        input_file.seekg(0, input_file.beg);

        //while (input_file.get(A)) {
        for (int cnt = 0; cnt < length / 2; cnt++) {
            input_file.get(A);
            //if there are 2 same characters in the pair
            if (A == input_file.peek()) {
                B = char(0);
            }
            //if there is a EOT charater ignore it and skip 1 character
            else if (input_file.peek() == char(3) && cnt != (length / 2) - 1) {
                input_file.ignore();
                B = char(3);
            }
            else {
                input_file.get(B);
            }
            //down here is the decryption section
            ...
        }
    }

我要问的是是否可以跳过或更改文件迭代器的位置,所以即使有多个 EOT 字符,我也可以解密整个文件。

标签: c++algorithmifstreameof

解决方案


推荐阅读