-2

可能重复:
如何解密此加密例程?

我已经参加这个项目一个星期了,我很困惑。我尝试了一切,但我没能成功。如果您能为我的程序提供一些帮助,我将不胜感激。谢谢你。

我有以下代码,我需要为它编写解密例程。

OChars = Original char -> is the word which the user types in.           
EKey = The Encryption Key (one letter)              
Length = The length of characters that the user needs to put             
EChars = Stores the Encrypted characters in so the decryption routine can use it to decrypt it.

    void encrypt_chars (int length, char EKey){
char temp_char;                 // original/encrypted char temporary store

for (int i = 0; i < length; i++){
    temp_char = OChars [i];     // get next char from original string
    __asm {                     // call the encrypt subroutine
        push   eax              // save register values on stack to be safe
        push   ecx
        movsx  ecx,temp_char    // enregister the source character
        movsx  eax,EKey         // and encryption key.
        call   encryptB         // calls the encryption subroutingencrypt the character
        mov    temp_char,al     // only need lower byte of EAX to return encrypted char
        pop    ecx              // restore original register values from stack
        pop    eax
    }
    EChars [i] = temp_char;     // Store encrypted char in the encrypted chars array
   }
     return;

encryption routine ASM          

     __asm {

encryptB: push edx          //saves register value edx on stack
          push ecx          //saves register value ecx on stack
          not  eax          //
          add  eax,0x04     //add 4 to eax register
          mov  edx,eax      //move eax to edx
          pop  eax          //brings eax back to
          xor  eax,edx      //clear values to zero
          pop  edx          //bring edx back 
          rol  al,3         //three times.
          sub  al,0x02      //subtracts 2 from al
          ret 
}               
Here ends the encryption part            
The decryption routine will start as follows             

void decrypt_chars (int length, char EKey){
  char temp_char;                       

for (int i = 0; i < length; i++){
    temp_char = EChars [i];         
    __asm {                         


     }

      DChars [i] = temp_char;           
}
 return;

decryption routine ASM

__asm {


      }
4

3 回答 3

3

尝试使用密钥 'K' 加密字符 'E' 并查看例程在每个步骤中执行的操作。然后尝试纠正他们旁边的每个操作的撤消。反转撤消指令集的顺序以获得加密例程的“撤消”。

于 2012-08-06T11:30:03.493 回答
1

我建议从替换那些只是扩展指令助记符的无用注释开始,并描述每个步骤中操作的值。就像是:

    push   eax              // avoid clobbering registers; just preamble
    push   ecx
    movsx  ecx,temp_char    // ecx = byte_to_encrypt
    movsx  eax,EKey         // eax = key
    ...
    push ecx                // Stack[0] = byte_to_encrypt
    ...
    pop eax                 // eax = byte_to_encrypt now...

等等。现在,当您拥有它时,应该很容易提取伪代码并最终提取出该操作的数学公式。这应该很容易逆转,而不是您只需将其编码回来(如果需要,在汇编中),而无需在寄存器之间进行所有毫无意义的值改组。

于 2012-08-06T11:30:24.127 回答
1

因为它与f(Key)where fis some 函数进行异或运算,所有计算都必须撤消,除了那些组成的计算f- 它们必须正常完成。

所以,像这样:(未测试)

; eax = EKey, cl = char
decryptB:
  add ecx, 2   // undo sub 2
  ror cl, 3    // undo rol
  not eax      // actually do not
  add eax, 4   // actually do add 4
  xor eax, ecx // undo xor
  ret
于 2012-08-06T13:31:27.157 回答