2012-12-01 22 views
0

我想異或一些已經加密的文件。
我知道XOR鍵是0x14或dec(20)。C++異或字符串鍵十六進制

我的代碼除了一件作品。所有'4'都沒有了。

這裏是我的XOR功能:

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(); 

這裏是encypted文件的一部分:

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

x{wu} {z`正在返回//位置。它不顯示4.
請注意X. thats應該被解碼爲4的空間盈虧。

我錯過了什麼?爲什麼不顯示所有4? <space> = 4 // 4 = <space>


UPDATE

這是所有具體的轉換列表:

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
  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(); 
+0

+1 - 關鍵是'inFile >> data'跳過空格。 –

+0

in |二進制,都是不明的。 XD – madziikoy

+0

你需要'#include'正確的頭文件和'使用'正確的命名空間。 – melpomene

0

你確定它是打印 '//位置'?我認爲它會打印'// location' - 注意雙斜槓後的空格。你正在用0x14異或0x34。結果是0x20,這是一個空格字符。無論如何,你爲什麼要用0x14來異或?

**編輯**忽略以上;我錯過了你的部分問題。真正的答案:

你完全確定x之前的字符是0x20嗎?也許這是一個不可打印的角色,看起來像一個空間?我會檢查十六進制值。

+1

十六進制是1D .. – madziikoy

相關問題