我的解密哪里出了问题

Where am i going wrong with my decryption

本文关键字:问题 解密 我的      更新时间:2023-10-16

我正在尝试解密一个我不知道密钥的.bin文件,以回答我们在课堂上遇到的这个问题:8) 假设您不知道在6)中使用的"key"的值。你还能解密加密文件吗?如果是这样的话,编写一个程序来实现这一点。(没有分数,这个只是为了好玩!)证明

int main(int argc, char* argv[]) {                               
uint8_t c, key = 0, uintOutput = 0;
string Encrypted, everything[255];
char b, charOutput;
int x = 0;
while ((b = getchar()) != EOF) {
    Encrypted = Encrypted + b;                  //read in chars from file and add them to a string (still encrypted)
}

我试过几种方法,我真的认为这一种会奏效。

for (int i = 0; i < Encrypted.length(); i++) //for the length of the encrypted string
        {
            x = 0;                              //resets the string it is entering into; Array position 0
            for (key = 0; key < 255; key++)     //key used must be a lowercase letter
            {
                c = (uint8_t)Encrypted[x];                      //convert char 'x' of encrypted string to a byte
                uintOutput = (c ^ key);                         //XOR with current key guess
                charOutput = (char)uintOutput;                  //convert byte back to char again
                everything[x] = everything[x] + charOutput;     //add xor'd char to currently selected string
                x++;                                            //move to next array position; to next string
            }
        }

其中,everything是字符串数组-everything[255],key是表示加密/解密密钥的字节。

            for (int y = 0; y < (255* Encrypted.length()); y++)     //print out for number of possibilities
        {
            cout << everything[y] << endl << endl;
        }
return 0;

}为了运行程序,我使用了一个带有文件重定向的.bat文件,在我的情况下,bat文件包含以下内容:program.exe < EncryptedText.bin >> ciphertext.bin其中EncryptedText.bin是使用未知密钥加密的文件,而密文.bin是输出解密文本的位置。

该程序构建良好,文件重定向也很好,因为我在另一个程序中使用过它,该程序猜测密钥只有一个字符,而不是不受限制的长度。

我遇到的主要问题是当我运行.bat文件时弹出一条错误消息

调试断言失败!

程序:C:\WINDOWS\SYSTEM32\MSVCP140D.ddl

文件i:\microsoft visual studio\vc\include\xstring

线路:1681

表达式:字符串下标超出范围

(此处重试等)

我真的很感激对此有任何见解,我只是看不出我哪里出了问题,我已经在这个问题上纠缠了几个小时

以下是您出错的地方(只有相关的代码行):

for (int i = 0; i < Encrypted.length(); i++)
{
       x = 0;
       for (key = 0; key < 255; key++)
       {
                c = (uint8_t)Encrypted[x];
                // Some code here that's not relevant
                x++;
       }
}

很明显,您的代码将尝试通过Encrypted[254]访问Encrypted[0],因此除非您的Encrypted字符串长254个字符,否则这将导致未定义的行为。

它看起来真的像这里:

for (int y = 0; y < (255* Encrypted.length()); y++)

索引超出CCD_ 5。