c++ XOR 字符串键十六进制

c++ XOR string key hex

本文关键字:十六进制 字符串 XOR c++      更新时间:2023-10-16

我正在尝试对一些已经加密的文件进行XOR。
我知道异或键是 0x14 或 dec(20)。

我的代码除了一件事之外有效。所有的"4"都消失了。

这是我的异或函数:

void xor(string &nString)    // Time to undo what we did from above :D
{
   const int KEY = 0x14;
   int strLen = (nString.length());
   char *cString = (char*)(nString.c_str());
   for (int i = 0; i < strLen; i++)
   {
    *(cString+i) = (*(cString+i) ^ KEY);
   }
}

这是我的主要内容的一部分:

ifstream inFile;
inFile.open("ExpTable.bin");
if (!inFile) {
    cout << "Unable to open file";
}
string data;                                 
while (inFile >> data) {
    xor(data);
    cout << data << endl;
}
inFile.close();

以下是加密文件的一部分:

$y{bq      //0 move
%c|{       //1 who
&c|qfq     //2 where 
'saufp     //3 guard
 x{wu`}{z  //4 location

但是x{wu }{z' 返回//location。它不显示 4。
请注意 X 前面的空间,它应该被解码为 4。

我错过了什么?为什么它没有显示所有 4 个? <space> = 4 // 4 = <space>


更新

以下是所有特定转化的列表:

HEX(enc) ASCII(dec)
20       4                  
21       5                  
22       6                  
23       7                  
24       0                  
25       1                  
26       2                  
27       3                  
28       <                  
29       =                  
2a       >                  
2b       ?                  
2c       8                  
2d       9                  
2e       :                  
2f       ;                  
30       $
31       %
32       &
33       '
34       
35       !
36       "
37       #
38       ,
39       -
3a       .
3b       /
3c       (
3d       )
3e       *
3f       +
40       T
41       U
42       V
43       W
44       P
45       Q
46       R
47       S
48       
49       ]
4a       ^
4b       _
4c       X
4d       Y
4e       Z
4f       [
50       D
51       E
52       F
53       G
54       @
55       A
56       B
57       C
58       L
59       M
5a       N
5b       O
5c       H
5d       I
5e       J
5f       K
60       t
61       u
62       v
63       w
64       p
65       q
66       r
67       s
68       |
69       }
6a       
6b       
6c       x
6d       y
6e       z
6f       {
70       d
71       e
72       f
73       g
75       a
76       b
77       c
78       l
79       m
7a       n
7b       o
7c       h
7d       i
7e       j
7f       k
1d       /tab
1e       /newline
  1. 摆脱所有石膏。
  2. 不要使用>>进行输入。

这应该可以解决您的问题。

编辑:

// got bored, wrote some (untested) code
ifstream inFile;
inFile.open("ExpTable.bin", in | binary);
if (!inFile) {
    cerr << "Unable to open ExpTable.bin: " << strerror(errno) << "n";
    exit(EXIT_FAILURE);
}
char c;
while (inFile.get(c)) {
    cout.put(c ^ 'x14');
}
inFile.close();

你确定它正在打印'//location'吗?我认为它会打印"//位置"——注意双斜杠后面的空格。你正在与0x14 0x34进行异或。结果是0x20,这是一个空格字符。无论如何,您为什么要与0x14一起异化所有内容?

**

编辑 ** 忽略上述内容;我错过了你的部分问题。真正的答案:

你完全确定 x 前面的字符是0x20吗?也许是一些看起来像空格的不可打印字符?我会检查十六进制值。