汇编代码。加密到解密例程

时间:2013-07-25 14:37:11

标签: assembly encryption x86 cpu-registers cpu-architecture

  • 有人可以为我看看这个并帮我解决一个可以反转用户输入的字符串的解密。我并不是说只是反过来这个程序。

      push edx 
      push ecx 
      not eax 
      add eax,0x04 
      mov edx,eax 
      pop eax 
      xor eax,edx 
      pop edx 
      rol al,1 
      rol al,1
      rol al,1 
      sub al,0x02 
      ret
    

    *

寄存器是:Inwards-ecx:加密密钥。 eax:要加密的字符。

Outwards- eax:加密字符

感谢您抽出宝贵时间来寻找。

1 个答案:

答案 0 :(得分:0)

算法是对称的,因为我可以解密每个字符和组合键。

这两个函数通过循环进行了测试,以便在解密值时出现任何错误:

#include <iostream>
using namespace std;

unsigned char enc(unsigned char ch, unsigned char key)
{
    unsigned char tmp = key^(~ch+(unsigned char)0x04);
    return (( (tmp<<3) | (tmp>>5) ) & 0xff)-0x02;
}

unsigned char dec(unsigned char ch, unsigned char key)
{
    unsigned char tmp = (ch+0x02);
    tmp = ((tmp>>3) | (tmp<<5)) & 0xff;
    return ~((tmp^key )-(unsigned char)0x04);
}

int main()
{
    // single encryption test
    char c = 'A';
    char key = 'K';
    cout << "single test:" << (char)enc(c, key) << endl;

    bool problem = false;
    int k, ch;
    for(k=0;k<256;k++)
    {

        for(ch=0;ch<256;ch++)
        {
            if( enc( dec((unsigned char)ch, (unsigned char)k), k) != ch )
            {
                problem = true;
                cout << "error k=" << k << "c=" << ch
                     << "result=" <<  (unsigned int)enc( dec((unsigned char)ch, (unsigned char)key), (unsigned char)key) << endl;

            }
        }
    }
    if(problem) cout << "There was a problem." << endl;
    else cout << "There was no problem." << endl;
}