Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将ASCII值循环到XOR输入_C++_C++11 - Fatal编程技术网

C++ 将ASCII值循环到XOR输入

C++ 将ASCII值循环到XOR输入,c++,c++11,C++,C++11,我想对.txt文件中的所有ASCII值进行异或运算,所有ASCII值最多为256。我的input.txt文件包含十六进制格式的ASCII值。例如,我的输入文件有“49 f3 54 f3 5f f3 47 f4 43 e8 49 e9”,我想用0x00对每个值进行异或运算,后跟0x01、0x02、0x03等等,并将结果打印在每个十六进制值的新行中。我已经在C++中写了下面的代码,但是它总是打印原始内容。p> #include <iostream> #include <fstrea

我想对.txt文件中的所有ASCII值进行异或运算,所有ASCII值最多为256。我的input.txt文件包含十六进制格式的ASCII值。例如,我的输入文件有“49 f3 54 f3 5f f3 47 f4 43 e8 49 e9”,我想用0x00对每个值进行异或运算,后跟0x01、0x02、0x03等等,并将结果打印在每个十六进制值的新行中。我已经在C++中写了下面的代码,但是它总是打印原始内容。p>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main() {

vector <char> decrypt; 
char ch;
int index;

ifstream infile;
ofstream outfile;

infile.open("prob3.txt");
outfile.open("results3_p1.txt");

if(!infile)
{
    cout << "Error opening input file" << endl;
    return 0;
}   

for (int a = 0; a < 256; a++)
{

    while(infile >> hex >> index)
    {
        ch = index ^ a;
        decrypt.push_back(ch);
    } 

    for (int i=0; i<decrypt.size(); i++)      // Print the results
    { 
        outfile << decrypt[i];
    }
    outfile << endl;

}

return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
矢量解密;
char ch;
整数指数;
河流充填;
出流孔的直径;
infle.open(“prob3.txt”);
outfile.open(“results3_p1.txt”);
如果(!infle)
{
cout十六进制>>索引)
{
ch=指数^a;
解密。推回(ch);
} 

对于(int i=0;i您没有更改“key”。也许您需要使用“a”来代替

在第一次迭代后,您将到达文件末尾。您可能需要重置文件指针。但这不是有效的。在这种情况下,您可能总是通过相同的解密数组进行迭代(在第一次迭代期间初始化)

最好将文件中的所有值存储在某个数组中,然后遍历该数组

我的建议是:

while(infile >> hex >> index)
{
       decrypt.push_back(index);
} 
for (int a = 0; a < 256; a++)
{  
    for (int i=0; i<decrypt.size(); i++)      // Print the results
    { 
        ch = decrypt[i] ^ a;    
        outfile << ch;
    }
    outfile << endl;
}
while(内嵌>>十六进制>>索引)
{
解密。推回(索引);
} 
对于(int a=0;a<256;a++)
{  

对于(int i=0;i,因为您希望将值与0…256进行异或运算,所以循环变量“a”实际上应该是“key”,反之亦然。

是否有效。对于我来说,它看起来仍然不起作用。我添加了示例。它是复制粘贴,因此可能有一些拼写错误。但一般来说,它应该有效。您可能需要强制转换为(char)