assembly - 我如何将这种加密变成解密?
问题描述
我想知道如何将此加密代码转换为解密代码。
我知道这意味着我必须颠倒一些指令并重新排序,但我不知道哪些需要重新排序,哪些不需要。
//---------------------------------------------------------------------------------------------------------------
//----------------- ENCRYPTION ROUTINES -------------------------------------------------------------------------
void encrypt_chars (int length, char EKey)
{
char temp_char; // Character temporary store
for (int i = 0; i < length; i++) // Encrypt characters one at a time
{
temp_char = OChars[i]; // Get the next char from Original Chars array
__asm
{
push eax // stores the "eax" register out onto the stack
push ecx // stores the "ecx" register out onto the stack
push edx // stores the "edx" register out onto the stack
//
movzx ecx, temp_char // zeroise "ecx" register and move values in "temp_char" varaible to "ecx" register
lea eax, EKey // copies address of values contained within the EKey varaible and moves it into "eax"register
//
push eax //
push ecx //
//
call encryptX // runs the function called "encryptX"
mov temp_char, dl // move values in "dl" register into "temp_char" variable
//
add esp, 8 //
pop edx // removes the "edx" register from the stack
pop ecx // removes the "ecx" register from the stack
pop eax // removes the "eax" register from the stack
} //
EChars[i] = temp_char; // store encrypted char in the Encrypted Chars array
}
return;
__asm
{
encryptX:
push ebp // stores the pointer onto the stack
mov ebp, esp // move values in "esp" register into "ebp" register
mov eax, [ebp + 12]// take value from the stack that is 8 bits above
// from the pointer a putting it in the "eax" register
mov ecx, [ebp + 8] // take value from the stack that is 8 bits above
// from the pointer a putting it on ecx
push eax // stores the Ekey address onto the stack
xchg eax, ecx // puts temp_char's value into the EKey address register and Ekey address into temp_char register
not al // multiply "al" register value by -1 (becomes equal to temp_char value)
add al, 1
inc eax // increment temp_char value in bytes by 1
rol al, 1 // rotate the al register value in bytes to the left by 1
rol al, 1 // rotate the al register value in bytes to the left by 1
mov ebx, eax // move temp_char value into "ebx" register
pop eax // removes temp_char from the stack
push ebx // stores temp_char value onto the stack
pop edx // removes "edx" register value from the stack
movzx ecx, [eax] // zeroise "ecx" register and move the address stored in "eax" register to "ecx" register
ror cl, 1 // rotate "cl" register value in bytes to the right by 1
xor cl, 0x96 // Exclusive OR (or XOR) the byte values within "cl" register with hex value 0x96 in binary
push ecx // stores the Ekey address onto the stack
and cl, 0x7 // AND the byte values within "cl" register with hex value 0x7 in binary
X: // Position X:
add dl, 2 // add 2 to value within dl
sub cl, 1 // subtract 1 from value within cl
jg X // jump to "X" position
pop ecx // removes "ecx" register value from the stack
xor ecx, edx // exclusive OR (or XOR) the byte values within "ecx" register with the byte values within "edx" register
mov[eax], cl // move "cl" register value into the address stored in "eax" register.
pop ebp // returning ebp back to the orginal value
ret // return, end of encryptX function
}
//--- End of Assembly code
}
//*** end of encrypt_chars function
//---------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------
//----------------- DECRYPTION ROUTINES -------------------------------------------------------------------------
//
void decrypt_chars(int length, char EKey)
{
char temp_char; // Character temporary store
for (int i = 0; i < length; i++) // Encrypt characters one at a time
{
temp_char = EChars[i]; // Get the next char from Original Chars array
__asm
{
push eax // stores the "eax" register out onto the stack
push ecx // stores the "ecx" register out onto the stack
push edx // stores the "edx" register out onto the stack
//
movzx ecx, temp_char // zeroise "ecx" register and move values in "temp_char" varaible to "ecx" register
lea eax, EKey // copies address of values contained within the EKey varaible and moves it into "eax"register
//
push eax //
push ecx //
//
call decryptX // runs the function called "encryptX"
mov temp_char, dl // move values in "dl" register into "temp_char" variable
//
add esp, 8 //
pop edx // removes the "edx" register from the stack
pop ecx // removes the "ecx" register from the stack
pop eax // removes the "eax" register from the stack
} //
DChars[i] = temp_char; // store encrypted char in the Encrypted Chars array
}
return;
__asm
{
decryptX:
push ebp // stores the pointer onto the stack
mov ebp, esp // move values in "esp" register into "ebp" register
mov eax, [ebp + 12]// take value from the stack that is 8 bits above
// from the pointer a putting it in the "eax" register
mov ecx, [ebp + 8] // take value from the stack that is 8 bits above
// from the pointer a putting it on ecx
push eax // stores the Ekey address onto the stack
xchg eax, ecx // puts temp_char's value into the EKey address register and Ekey address into temp_char register
ror al, 1 // rotate the al register value in bytes to the left by 1
ror al, 1 // rotate the al register value in bytes to the left by 1
dec eax
sub al, 1
mov ebx, eax // move temp_char value into "ebx" register
pop eax // removes temp_char from the stack
push ebx // stores temp_char value onto the stack
pop edx // removes "edx" register value from the stack
movzx ecx, [eax] // zeroise "ecx" register and move the address stored in "eax" register to "ecx" register
ror cl, 1 // rotate "cl" register value in bytes to the right by 1
xor cl, 0x96 // Exclusive OR (or XOR) the byte values within "cl" register with hex value 0x96 in binary
push ecx // stores the Ekey address onto the stack
and cl, 0x7 // AND the byte values within "cl" register with hex value 0x7 in binary
X: // Position X:
add dl, 2 // add 2 to value within dl
sub cl, 1 // subtract 1 from value within cl
jg X // jump to "X" position
pop ecx // removes "ecx" register value from the stack
xor ecx, edx // exclusive OR (or XOR) the byte values within "ecx" register with the byte values within "edx" register
mov[eax], cl // move "cl" register value into the address stored in "eax" register.
pop ebp // returning ebp back to the orginal value
ret // return, end of encryptX function
}
}
//*** end of decrypt_chars function
//---------------------------------------------------------------------------------------------------------------
输入是,“你好”加密后输出是,“hv^\L”解密后输出应该是你好,但我目前得到,“\x1Aíø\x19\x1F”
解决方案
推荐阅读
- android - 长时间延迟收到音量变化通知
- python - Python“从sklearn import svm”导致SyntaxError
- android - 找不到相应的方法处理程序“public void registerUser(android.view.View)”
- java - 在 Java 中使用字符串对文本文件中的数字进行排序
- uwp - 在没有控件的情况下处理 UWP 应用中的键盘事件
- c - C查询字符串解析
- javascript - 如何让这个循环打印我需要的值?
- python - 关于 Tensorflow Estimator 实践的问题,Tensorflow 操作应该在“my_model”还是其他地方进行?
- python - Python:调用一个函数并从其他函数返回
- sql - 两个位置都在同一记录中的 SQL STDistance