2017-05-14 90 views
0

所以我有这个功能。我需要帮助扭转功能

UINT32 Encrypt(UINT32 instruction, int increment) 
{ 
    UINT32 linstruction = _rotl(instruction, 7); 
    UINT32 rinstruction = _rotr(instruction, 3); 

    UINT32 key = (0x1F3D8AF * increment)^(rinstruction^linstruction); 
    return (key^instruction); 
} 

我需要做一个函数,实际解密这个并从结果中获得使用一个键的指令,所以它会像。

t = encrypt(t, i); 
t = decrypt(t, key); 

基本上我想要它逆转加密的整个过程,所以它解密它,并得到我的指令。

他们在这个函数中使用

int RbxOpEncoder::encode(Instruction op, int i, int key) { 
    int orig = ConvertInstruction(op); 
    int t = orig; 
    switch (GET_OPCODE(op)) { 
    case OP_JMP: 
     t = ((Encrypt(t, i) & 0x3FFFFFF) | ((orig >> 26 & MASK1(6, 0)) << 0x1A)); 
    break; 
    case OP_CALL: 
    case OP_TAILCALL: 
    case OP_CLOSURE: 
    case OP_RETURN: 
     t = ((Calldecrypt(t, i) & 0x3FFFFFF) | ((orig >> 26 & MASK1(6, 0)) << 0x1A)); 
     break; 
    } 
    t = EncryptOpcode(t, key); 
    return t; 
} 
+0

是'key'提供什么'解密'? – Jarod42

+0

它在函数encrypt中的一个参数被使用,我编辑它。 –

+1

这似乎更像是一个数学或密码学问题。一旦你找出数学,把它变成C++代码应该是微不足道的。 – Barmar

回答

0

您可以使用:

std::uint32_t decrypt(std::uint32_t instruction, int increment) 
{ 
    instruction = instruction^(0x1F3D8AF * increment); 

    for (int i = 0; i != 15; ++i) { 
     instruction = Encrypt(instruction, 0); 
    } 
    return instruction; 
} 

然后,你还要

decrypt(Encrypt(value, increment), increment) == value 

Demo

+0

你在世界上是怎么想出来的?为什么15次迭代? –

+0

@MooingDuck:这种功能(双射)应该有一个循环。我希望从加密方法中获得更大的BTW。我通过'Encrypt'循环查看1的进化,并看到重复,然后检查其他值高达'20000000'。 – Jarod42

+0

确认:http://coliru.stacked-crooked.com/a/83b0befbd35d3d2d –