flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Examples and Tutorials > Random hex string via Windows API

Author
Thread Post new topic Reply to topic
tesis



Joined: 26 Jan 2016
Posts: 1

Random hex string via Windows API

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  CryptAcquireContexthcryptNULLNULLPROV_RSA_FULLCRYPT_VERIFYCONTEXT
            invoke  CryptGenRandom, [hcrypt], LENGTHbuffer
            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 ediLENGTH * 2    ; edi is index of hex string
                    mov esiLENGTH        ; esi is index of random generated byte
                    mov [buffer + edi], 0  ; terminate our hex string
                    mov eax0             ; eax is index of ascii character
      bin2hex:
                    mov al, [buffer + esi - 1]  ; copy random byte to AL
                    shr al4                   ; 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 al4                   ; get first 4 bits of the byte
                    shr al4                   ;
                    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  MessageBox0buffer'Random Hex'MB_OK
      finish:
            invoke  ExitProcess0


.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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2017, Tomasz Grysztar.