News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

RolData/RorData 2 encrypt/decrypt text file?

Started by devilhorse, June 18, 2006, 11:21:39 PM

Previous topic - Next topic

devilhorse

Does anyone have an example or advice on how to use RorData and RolData procs to encrypt\decrypt text files on disk? i know how to use these two procs with hard-coded strings in my source code. i do know how to open a file. What i don;t know is how to open the file and put the contents of that file into an appropriate buffer that can be used with the RorData\RolData procs. My purpose is to open a text file, get the contents, encrypt the contents and then write the encrypted content to a different file using the write_disk_file proc.
Any suggestions?. I am still learning assembly because I HATE C++ and C!!! Yea, I know c/c++ is what puts food on the table, but for small programs I write and sell for myself, I prefer assembly. It is straight forward and fast to work with using EasyCode IDE.

hutch--

devilhorse,

Easycode is a good choice, Ramon has done very good work here and it seems to be popular. The two algos are pretty simple and what you need to pass to it is both addresses and both data lengths.


RolData proc lpSource:DWORD,ln:DWORD,lpKey:DWORD,lnKey:DWORD


If you can load the file into a memory buffer, pass that as the first argument. The second will be the file length which you can get either from opening the file or by using one of the LEN based algos. Then do the same with the "key", pass its address and its length as the next two arguments and it should work OK.

Something worth doing if the security is subject to large risk is to make the key as big as possible and if it is at least the length of the source, it get a lot harder to break. The other factor is that with a determined attacker, each key needs to be unique and highly random in nature or it can be broken by an attacker with enough computer resources.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

devilhorse


devilhorse

 :green2 Hutch, here is the code Iused to encrypt/decrypt following your directions. Hopefully someone else can use it. Basically, i read the masm lib file and used bits and pieces of code found there to make these two procedures. Again, thank you so much.

.Const

.Data?

.Data
key DB "This is the key", 0
.Code



WriteEncryptedFile Proc lpSourceFile:DWord, lpDestinationFile:DWord
   Local hDestFile:DWord
   Local hFile:DWord
   Local fl:DWord
   Local bRead:DWord
   Local hmem$:DWord     ;source memory handle


   Invoke CreateFile, lpSourceFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, NULL, NULL
   Mov hFile, Eax

   Invoke GetFileSize, hFile, NULL
   Mov fl, Eax

   stralloc fl
   Mov hmem$, Eax         ;source file memory

   Invoke ReadFile, hFile, hmem$, fl, Addr bRead, NULL ;read file into buffer
   Invoke CloseHandle, hFile

   Invoke RolData, hmem$, fl, Addr key, 16 ;encrypt data

   Invoke write_disk_file, lpDestinationFile, hmem$, fl

   strfree hmem$

   Ret
WriteEncryptedFile EndP



ReadEncryptedFile Proc lpSourceFile:DWord, hParent:DWord, hControl:DWord


   Local hFile:DWord
   Local fl:DWord
   Local bRead:DWord
   Local hmem$:DWord     ;source memory handle

   Invoke CreateFile, lpSourceFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, NULL, NULL
   Mov hFile, Eax

   Invoke GetFileSize, hFile, NULL
   Mov fl, Eax

   stralloc fl
   Mov hmem$, Eax         ;source file memory

   Invoke ReadFile, hFile, hmem$, fl, Addr bRead, NULL ;read file into buffer
   Invoke CloseHandle, hFile

   Invoke RorData, hmem$, fl, Addr key, 16 ;encrypt data

   Invoke SendDlgItemMessage, hParent, hControl, WM_SETTEXT, NULL, hmem$

   strfree hmem$

   Ret
ReadEncryptedFile EndP