- 网络安全凯撒密码作业,C写着没意思,所以想用汇编写。
C语言
/*
* =====================================================================================
*
* Filename: Caesar_cipher.c
*
* Description: Caesar cipher
*
* Version: 1.0
* Created: 2020年04月02日 12时02分48秒
* Revision: none
* Compiler: gcc
*
* Author: Kali-Team (KT), root@kali-team.cn
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define caesar(x) rot(13, x)
#define decaesar(x) rot(13, x)
#define decrypt_rot(x, y) rot((26-x), y)
void rot(int c, char *str)
{
int l = strlen(str);
const char *alpha[2] = { "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
int i;
for (i = 0; i < l; i++)
{
if (!isalpha(str[i]))
continue;
if (isupper(str[i]))
str[i] = alpha[1][((int)(tolower(str[i]) - 'a') + c) % 26];
else
str[i] = alpha[0][((int)(tolower(str[i]) - 'a') + c) % 26];
}
}
int main()
{
char str[] = "This is a top secret text message!";
printf("Original: %s\n", str);
caesar(str);
printf("Encrypted: %s\n", str);
decaesar(str);
printf("Decrypted: %s\n", str);
return 0;
}
C语言加内联汇编
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define caesar(x) rot(13, x, strlen(x))
#define decaesar(x) rot(13, x, strlen(x))
#define decrypt_rot(x, y) rot((26-x), y, strlen(y))
void rot(int c, char *str, int l)
{
char alpha_down[27];
__asm
{
mov byte ptr [ebp-17h],66h
mov byte ptr [ebp-2],0
mov byte ptr [ebp-15h],68h
mov byte ptr [ebp-0Ch],71h
mov byte ptr [ebp-19h],64h
mov byte ptr [ebp-1Bh],62h
mov byte ptr [ebp-4],79h
mov byte ptr [ebp-14h],69h
mov byte ptr [ebp-8],75h
mov byte ptr [ebp-13h],6Ah
mov byte ptr [ebp-5],78h
mov byte ptr [ebp-12h],6Bh
mov byte ptr [ebp-16h],67h
mov byte ptr [ebp-1Ah],63h
mov byte ptr [ebp-1Ch],61h
mov byte ptr [ebp-11h],6Ch
mov byte ptr [ebp-3],7Ah
mov byte ptr [ebp-10h],6Dh
mov byte ptr [ebp-0Fh],6Eh
mov byte ptr [ebp-18h],65h
mov byte ptr [ebp-0Eh],6Fh
mov byte ptr [ebp-0Dh],70h
mov byte ptr [ebp-6],77h
mov byte ptr [ebp-0Ah],73h
mov byte ptr [ebp-7],76h
mov byte ptr [ebp-0Bh],72h
mov byte ptr [ebp-9],74h
}
char alpha_up[27];
__asm{
mov byte ptr [ebp-1Eh],0
mov byte ptr [ebp-35h],44h
mov byte ptr [ebp-38h],41h
mov byte ptr [ebp-34h],45h
mov byte ptr [ebp-22h],57h
mov byte ptr [ebp-36h],43h
mov byte ptr [ebp-24h],55h
mov byte ptr [ebp-2Ah],4Fh
mov byte ptr [ebp-30h],49h
mov byte ptr [ebp-21h],58h
mov byte ptr [ebp-37h],42h
mov byte ptr [ebp-2Fh],4Ah
mov byte ptr [ebp-2Eh],4Bh
mov byte ptr [ebp-2Dh],4Ch
mov byte ptr [ebp-33h],46h
mov byte ptr [ebp-20h],59h
mov byte ptr [ebp-2Ch],4Dh
mov byte ptr [ebp-32h],47h
mov byte ptr [ebp-2Bh],4Eh
mov byte ptr [ebp-29h],50h
mov byte ptr [ebp-28h],51h
mov byte ptr [ebp-27h],52h
mov byte ptr [ebp-25h],54h
mov byte ptr [ebp-1Fh],5Ah
mov byte ptr [ebp-26h],53h
mov byte ptr [ebp-31h],48h
mov byte ptr [ebp-23h],56h
}
char *alpha[2];
__asm{
lea eax,alpha_down
mov dword ptr [ebp-40h],eax
lea ecx,alpha_up
mov dword ptr [ebp-3Ch],ecx
}
for (int i = 0; i < l; i++)
{
if (!isalpha(str[i]))
__asm jmp rot+111h
if (isupper(str[i]))
{
int index,list = 0;
__asm{
mov eax,dword ptr [ebp+0Ch]
add eax,dword ptr [ebp-44h]
movsx ecx,byte ptr [eax]
push ecx
call tolower
add esp,4
mov edx,dword ptr [ebp+8]
lea eax,[eax+edx-61h]
mov dword ptr [list],eax
}
//index = list % 26;
__asm{
xor edx,edx
mov ecx,1Ah
div ecx
mov dword ptr [index],edx
}
//str[i] = alpha[1][index];
__asm{
mov eax,dword ptr [ebp+0Ch]
add eax,dword ptr [ebp-44h]
mov ecx,dword ptr [ebp-3Ch]
add ecx,dword ptr [index]
mov dl,byte ptr [ecx]
mov byte ptr [eax],dl
}
}
else
{
int index,list = 0;
//list = (int)(tolower(str[i]) - 97) + c;
__asm{
mov eax,dword ptr [ebp+0Ch]
add eax,dword ptr [ebp-44h]
movsx ecx,byte ptr [eax]
push ecx
call tolower
add esp,4
mov edx,dword ptr [ebp+8]
lea eax,[eax+edx-61h]
mov dword ptr [list],eax
}
//index = list % 26;
__asm{
xor edx,edx
mov ecx,1Ah
div ecx
mov dword ptr [index],edx
}
//str[i] = alpha[0][index];
__asm{
mov eax,dword ptr [ebp+0Ch]
add eax,dword ptr [ebp-44h]
mov ecx,dword ptr [ebp-40h]
add ecx,dword ptr [index]
mov dl,byte ptr [ecx]
mov byte ptr [eax],dl
}
}
}
}
int main()
{
char str[] = "This is a top secret text message!";
printf("Original: %s\n", str);
caesar(str);
printf("Encrypted: %s\n", str);
decaesar(str);
printf("Decrypted: %s\n", str);
return 0;
}