c++ XOR 字符串键十六进制
c++ XOR string key hex
我正在尝试对一些已经加密的文件进行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
- 摆脱所有石膏。
- 不要使用
>>
进行输入。
这应该可以解决您的问题。
编辑:
// 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吗?也许是一些看起来像空格的不可打印字符?我会检查十六进制值。
相关文章:
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 如何在C++中将十六进制字符串转换为文本数据
- 定义宏以将前缀 0x 添加到十六进制字符串文本
- 将字符串(可以是十进制字符串或十六进制字符串)转换为整数C++
- 将 std::string( "x3BxDEx7C" ) 转换为可读的十六进制字符串?
- C++ 将十六进制字符串表示形式转换为十六进制字符串表示形式
- 字符数组到十六进制字符串的转换 - 意外输出
- C++十进制到十六进制(字符串下标超出范围)
- C++十六进制字符串转换为十六进制的字符 *
- 如何在C++中将十六进制字符串转换为字节字符串?
- 从十六进制字符串转换为十六进制字符数组
- 以C++格式将十六进制字符串转换为十进制数
- 将无符号字符 * 转换为十六进制字符串
- 在C++中将 HWND 转换为十六进制字符串
- 按位函数 AND、OR 和 XOR 在 C++ 中使用十六进制字符串
- 如何将十六进制字符串转换为 HTML 实体
- 查找十六进制字符串是 utf-8 或 utf-16
- 在 c++ 中使用字符串流将 4 个字符的十六进制字符串转换为有符号的短字符串
- 将 std::十六进制字符串转换为 std::array<T, 6>