CPU x86命令セット、133バイト
000009F8 53 push ebx
000009F9 56 push esi
000009FA 57 push edi
000009FB 55 push ebp
000009FC 55 push ebp
000009FD BF00010000 mov edi,0x100
00000A02 29FC sub esp,edi
00000A04 8B6C3C18 mov ebp,[esp+edi+0x18]
00000A08 31DB xor ebx,ebx
00000A0A 8A5D00 mov bl,[ebp+0x0]
00000A0D 45 inc ebp
00000A0E 31C0 xor eax,eax
00000A10 880404 mov [esp+eax],al
00000A13 40 inc eax
00000A14 39F8 cmp eax,edi
00000A16 72F8 jc 0xa10
00000A18 31F6 xor esi,esi
00000A1A 31C9 xor ecx,ecx
00000A1C 89F0 mov eax,esi
00000A1E 31D2 xor edx,edx
00000A20 F7F3 div ebx
00000A22 8A0434 mov al,[esp+esi]
00000A25 02441500 add al,[ebp+edx+0x0]
00000A29 00C1 add cl,al
00000A2B 8A0434 mov al,[esp+esi]
00000A2E 8A140C mov dl,[esp+ecx]
00000A31 88040C mov [esp+ecx],al
00000A34 881434 mov [esp+esi],dl
00000A37 46 inc esi
00000A38 39FE cmp esi,edi
00000A3A 72E0 jc 0xa1c
00000A3C 8B443C1C mov eax,[esp+edi+0x1c]
00000A40 01E8 add eax,ebp
00000A42 722F jc 0xa73
00000A44 48 dec eax
00000A45 89C6 mov esi,eax
00000A47 01DD add ebp,ebx
00000A49 31C0 xor eax,eax
00000A4B 31D2 xor edx,edx
00000A4D 31C9 xor ecx,ecx
00000A4F 39F5 cmp ebp,esi
00000A51 7320 jnc 0xa73
00000A53 FEC2 inc dl
00000A55 8A0414 mov al,[esp+edx]
00000A58 00C1 add cl,al
00000A5A 8A1C0C mov bl,[esp+ecx]
00000A5D 88040C mov [esp+ecx],al
00000A60 881C14 mov [esp+edx],bl
00000A63 00D8 add al,bl
00000A65 8A1C04 mov bl,[esp+eax]
00000A68 8A4500 mov al,[ebp+0x0]
00000A6B 30D8 xor al,bl
00000A6D 884500 mov [ebp+0x0],al
00000A70 45 inc ebp
00000A71 EBDC jmp short 0xa4f
00000A73 01FC add esp,edi
00000A75 5D pop ebp
00000A76 5D pop ebp
00000A77 5F pop edi
00000A78 5E pop esi
00000A79 5B pop ebx
00000A7A C20800 ret 0x8
00000A7D
A7D-9F8 = 85h = 133バイトですが、同じ関数の結果のバイト数が130バイトであるため、計算が正常かどうかわかりません...「cript」という名前の関数の最初の引数は文字列です。 2番目の引数は文字列の長さです(最初のバイト+キーの長さ+メッセージの長さ)。以下に、そのクリプトルーチンを取得するためのアセンブリ言語ファイルがあります。
; nasmw -fobj this.asm
section _DATA use32 public class=DATA
global cript
section _TEXT use32 public class=CODE
cript:
push ebx
push esi
push edi
push ebp
push ebp
mov edi, 256
sub esp, edi
mov ebp, dword[esp+ edi+24]
xor ebx, ebx
mov bl, [ebp]
inc ebp
xor eax, eax
.1: mov [esp+eax], al
inc eax
cmp eax, edi
jb .1
xor esi, esi
xor ecx, ecx
.2: mov eax, esi
xor edx, edx
div ebx
mov al, [esp+esi]
add al, [ebp+edx]
add cl, al
mov al, [esp+esi]
mov dl, [esp+ecx]
mov [esp+ecx], al
mov [esp+esi], dl
inc esi
cmp esi, edi
jb .2
mov eax, dword[esp+ edi+28]
add eax, ebp
jc .z
dec eax
mov esi, eax
add ebp, ebx
xor eax, eax
xor edx, edx
xor ecx, ecx
.3: cmp ebp, esi
jae .z
inc dl
mov al, [esp+edx]
add cl, al
mov bl, [esp+ecx]
mov [esp+ecx], al
mov [esp+edx], bl ; swap S[c] S[r]
add al, bl
mov bl, [esp+eax]
mov al, [ebp]
xor al, bl
mov [ebp], al
inc ebp
jmp short .3
.z:
add esp, edi
pop ebp
pop ebp
pop edi
pop esi
pop ebx
ret 8
チェック結果のCファイルの下:
// Nasmw -fobj fileasm.asm
// bcc32 -v filec.c fileasm.obj
#include <stdio.h>
void _stdcall cript(char*,unsigned);
char es1[]="\x01\x00\x00\x00\x00\x00\x00";
char es2[]="\x0Dthis is a keythis is some data to encrypt";
char es3[]="\x0dthis is a key\xb5\xdb?i\x1f\x92\x96\226e!\xf3\xae(!\xf3\xea\x43\xd4\x9fS\xbd?d\x82\x84{\xcdN";
char es4[]="Sthis is a rather long key because the value of S is 83 so the key length must matchand this is the data to be encrypted";
void printMSGKeyC(unsigned char* a, unsigned len)
{unsigned i,j,k;
unsigned char *p,*end;
printf("keylen = %u\nKey = [", (unsigned)*a);
for(i=1, j=*a;i<=j;++i) printf("%c", a[i]);
printf("]\nMessage= [");
for(p=a+i,end=a+len-1;p<end;++p)printf("%c", *p);
printf("]\n");
}
void printMSGKeyHex(unsigned char* a, unsigned len)
{unsigned i,j,k;
unsigned char *p,*end;
printf("keylen = %u\nKey = [", (unsigned)*a);
for(i=1, j=*a;i<=j;++i) printf("%02x", a[i]);
printf("]\nMessage= [");
for(p=a+i,end=a+len-1;p<end;++p)printf("%02x", *p);
printf("]\n");
}
main()
{printf("sizeof \"%s\"= %u [so the last byte 0 is in the count]\n", "this", sizeof "this");
printf("Input:\n");
printMSGKeyHex(es1, sizeof es1);
cript(es1, (sizeof es1)-1);
printf("Afther I cript:\n");
printMSGKeyHex(es1, sizeof es1);
printf("Input:\n");
printMSGKeyC(es2, sizeof es2);
printMSGKeyHex(es2, sizeof es2);
cript(es2, (sizeof es2)-1);
printf("Afther I cript:\n");
printMSGKeyC(es2, sizeof es2);
printMSGKeyHex(es2, sizeof es2);
cript(es2, (sizeof es2)-1);
printf("Afther II cript:\n");
printMSGKeyC(es2, sizeof es2);
printMSGKeyHex(es2, sizeof es2);
printf("----------------------\n");
printf("Input:\n");
printMSGKeyHex(es3, sizeof es3);
cript(es3, (sizeof es3)-1);
printf("Afther I cript:\n");
printMSGKeyHex(es3, sizeof es3);
printf("----------------------\n");
printf("Input:\n");
printMSGKeyHex(es4, sizeof es4);
cript(es4, (sizeof es4)-1);
printf("Afther I cript:\n");
printMSGKeyHex(es4, sizeof es4);
cript(es4, (sizeof es4)-1);
printf("Afther II cript:\n");
printMSGKeyHex(es4, sizeof es4);
return 0;
}
結果:
sizeof "this"= 5 [so the last byte 0 is in the count]
Input:
keylen = 1
Key = [00]
Message= [0000000000]
Afther I cript:
keylen = 1
Key = [00]
Message= [de188941a3]
Input:
keylen = 13
Key = [this is a key]
Message= [this is some data to encrypt]
keylen = 13
Key = [746869732069732061206b6579]
Message= [7468697320697320736f6d65206461746120746f20656e6372797074]
Afther I cript:
keylen = 13
Key = [this is a key]
Message= [Á█?iÆûûe!¾«(!¾ÛCȃS¢?déä{═N]
keylen = 13
Key = [746869732069732061206b6579]
Message= [b5db3f691f9296966521f3ae2821f3ea43d49f53bd3f6482847bcd4e]
Afther II cript:
keylen = 13
Key = [this is a key]
Message= [this is some data to encrypt]
keylen = 13
Key = [746869732069732061206b6579]
Message= [7468697320697320736f6d65206461746120746f20656e6372797074]
----------------------
Input:
keylen = 13
Key = [746869732069732061206b6579]
Message= [b5db3f691f9296966521f3ae2821f3ea43d49f53bd3f6482847bcd4e]
Afther I cript:
keylen = 13
Key = [746869732069732061206b6579]
Message= [7468697320697320736f6d65206461746120746f20656e6372797074]
----------------------
Input:
keylen = 83
Key = [74686973206973206120726174686572206c6f6e67206b65792062656361757365207468652076616c7565206f66205320697320383320736f20746865206b6579206c656e677468206d757374206d61746368]
Message= [616e64207468697320697320746865206461746120746f20626520656e63727970746564]
Afther I cript:
keylen = 83
Key = [74686973206973206120726174686572206c6f6e67206b65792062656361757365207468652076616c7565206f66205320697320383320736f20746865206b6579206c656e677468206d757374206d61746368]
Message= [961f2c8fa3259ba3665b6d6bdfbcac8b8efafe96423d21fc3b13606316710411d886ee07]
Afther II cript:
keylen = 83
Key = [74686973206973206120726174686572206c6f6e67206b65792062656361757365207468652076616c7565206f66205320697320383320736f20746865206b6579206c656e677468206d757374206d61746368]
Message= [616e64207468697320697320746865206461746120746f20626520656e63727970746564]