2014-08-31 46 views
-4

我試圖獲得加密扭轉這種解密,但真的很難做..如何反轉?

unsigned int decrypt(unsigned char *encBuffer, unsigned int encBufferLen, unsigned char *decBuffer) 
{ 
unsigned int decBufferLen = 0; 

unsigned char table[] = { NULL, ' ', '-', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 0x0D }; 

unsigned int offset = 0, i = 0; 
unsigned char encStrLen = 0, c = 0; 

while (offset < encBufferLen) 
{ 
    if (encBuffer[offset] == 0xFFU) // if byte is 0xFF then add 0x0D to new buffer and continue 
    { 
     *(decBuffer + decBufferLen++) = 0x0D; 
     offset++; 
     continue; 
    } 

    encStrLen = encBuffer[offset++]; // get string length 

    i = 0; 
    if ((encStrLen & 0x80U) == 0) // if result is 0 then decrypt the string by Xor 33 
    { 
     while ((i++ < encStrLen) && (offset < encBufferLen)) 
     { 
      *(decBuffer + decBufferLen++) = (encBuffer[offset++]^0x33U); 
     } 
    } 
    else // otherwise 
    { 
     encStrLen &= 0x7FU; 
     while ((i < encStrLen) && (offset < encBufferLen)) 
     { 
      c = encBuffer[offset++]; // c = current byte, increment the index 
      *(decBuffer + decBufferLen++) = (table[(c & 0xF0U) >> 4]); 

      if (table[c & 0x0FU] != NULL) 
      { 
       *(decBuffer + decBufferLen++) = table[c & 0x0FU]; 
      } 

      i += 2; 
     } 
    } 
} 

return decBufferLen; 

}

這是我的一些測試後得到了

std::vector<unsigned char> encrypt(std::vector<unsigned char> decryptedBuf) 
{ 
    std::vector<unsigned char> vector; 

    unsigned int offset = 0, decryptedStringLength = 0; 

    unsigned char currentByte = 0; 

    while (offset < decryptedBuf.size()) 
    { 
     if (decryptedBuf[offset] == 0x0D) 
     { 
      vector.push_back(0xFF); 
      offset++; 
      continue; 
     } 

     decryptedStringLength = decryptedBuf.size() - 1; // <--- to edit if encrypt works 

     vector.push_back(decryptedStringLength); // <--- i'm not sure it will be interpreted right 

     if (decryptedStringLength < 0x80) // < '128' Xor 33 http://pastebin.com/b18JfBFK 
     { 
      for (unsigned int i = 0; i < decryptedStringLength; i++) 
       vector.push_back(decryptedBuf[offset++]^0x33); 
     } 
     else // >= '128' Table 
     { 
      decryptedStringLength -= 0x80; // & 0x7F http://pastebin.com/THZjZJfs 

      for (unsigned int i = 0; i < decryptedStringLength; i += 2) 
      { 
       currentByte = decryptedBuf[offset++]; 


      } 
     } 
    } 

    return vector; 
} 

有你關於如何扭轉它的一些想法?!我嘗試了2天,但我沒有得到任何東西。

+1

** - 1 **,代碼太多。你應該發佈儘可能少的代碼。 – GingerPlusPlus 2014-08-31 14:10:53

+0

問題是什麼? – ntoskrnl 2014-08-31 14:57:28

+0

@ntoskrnl你有一些想法如何扭轉它? – JMII89 2014-08-31 14:58:35

回答

0

decrypt不是一對一的 - 很多不同的密碼文本可能會解密爲相同的明文。相應的「加密」功能的最簡單方法是將明文分成不超過127字節的塊,並輸出塊長度,然後輸出塊異或0x33。這樣,你只會練習一小部分decrypt算法。 Working example

std::vector<unsigned char> encrypt(std::vector<unsigned char> decryptedBuf) { 
    vector<unsigned char> result; 

    size_t cur_offset = 0; 
    while (cur_offset < decryptedBuf.size()) { 
    unsigned char chunk_size = static_cast<unsigned char>(
     min(decryptedBuf.size() - cur_offset, size_t(0x7F))); 
    result.push_back(chunk_size); 
    result.insert(result.end(), decryptedBuf.begin() + cur_offset, 
        decryptedBuf.begin() + cur_offset + chunk_size); 

    for (vector<unsigned char>::iterator it = result.end() - chunk_size; 
     it != result.end(); ++it) { 
     *it ^= 0x33; 
    } 

    cur_offset += chunk_size; 
    } 
    return result; 
}