flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > Random hex string via Windows API

Author
Thread Post new topic Reply to topic
tesis



Joined: 26 Jan 2016
Posts: 1
tesis 01 Feb 2016, 14:13
Hello, all! I was having a problem trying to find asm-example to generate random bytes via winapi. Here is my version, maybe someone will find it useful. This example will show message box with random hex string.

Code:
format PE GUI
include 'win32ax.inc'

; MSDN Random Example:
; https://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx

.data
      LENGTH               equ 10         ; number of random BYTES
      buffer               rb  255        ; random hex string
      hcrypt               dd  ?
      ascii                db  '0123456789ABCDEF'

      RESERVED             equ NULL
      PROV_RSA_FULL        equ 1           ; values from wincrypt.h:
      CRYPT_VERIFYCONTEXT  equ 0xF0000000  ; http://sourceforge.net/u/earnie/winapi/winapi/ci/Initial/tree/include/wincrypt.h

.code
      start:
                    ; wincrypto funcitons are from Advapi32.dll
            invoke  CryptAcquireContext, hcrypt, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT
            invoke  CryptGenRandom, [hcrypt], LENGTH, buffer
            invoke  CryptReleaseContext, [hcrypt], RESERVED

                    ; now buffer contains LENGTH random BYTES
                    ; we need to transform it to HEX string
                    ; gonna fill buffer with hex characters backwards
                    mov edi, LENGTH * 2    ; edi is index of hex string
                    mov esi, LENGTH        ; esi is index of random generated byte
                    mov [buffer + edi], 0  ; terminate our hex string
                    mov eax, 0             ; eax is index of ascii character
      bin2hex:
                    mov al, [buffer + esi - 1]  ; copy random byte to AL
                    shr al, 4                   ; get last 4 bits of the byte
                    mov cl, [ascii + eax]       ; copy corresponding ascii character to its place
                    mov [buffer + edi - 1], cl  ;
                    dec edi
                    mov al, [buffer + esi - 1]
                    shl al, 4                   ; get first 4 bits of the byte
                    shr al, 4                   ;
                    mov cl, [ascii + eax]
                    mov [buffer + edi - 1], cl  ; copy ascii character to its place
                    dec edi
                    dec esi
                    jnz bin2hex                 ; repeat until index of random generated byte is ZERO

            invoke  MessageBox, 0, buffer, 'Random Hex', MB_OK
      finish:
            invoke  ExitProcess, 0


.end  start
    
Post 01 Feb 2016, 14:13
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.