News:

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

recursive algorithm

Started by Psycho, February 24, 2006, 08:00:45 AM

Previous topic - Next topic

Psycho

Hello my name is Antony, I am a somewhat experienced VB coder (have been coding it for 6 years or so). A few years ago I decided to write my own encryption algorithm, and I have met with somewhat large success (apart from lack of speed and one annoying little bug that pops up completely at random that i can't for the life of me find the source of). I have started to teach myself MASM to address the speed issues and have so far met with fairly limited success as even though I have the full collection of Iczelion's tutorials, I find that some of his documentation is tedious to get through and understand.

After Emailing Iczelion and recieving no reply I have turned to Hutch and this forum to help me along a little. While Hutch has given me an example of code which can be used, it is as he states only suitable for a single pass through a file and the file must be longer than the key used.

While one condition of the example is met (the file being longer than the key), It is not a single pass algorithm, but in fact uses several keys depending on the length of the original user defined key.

For example, an 8 character user defined key will produce 7 other 8 character keys generated by the VB inbuilt PRNG, to be used one after the other on the file, that is to say that the file is encrpted with one key and then the second and so on.

The code in the DLL that I wrote is written with multithreading in mind although I have yet to find a decent implementation of this, so as such have left it as a single thread.

I was wondering if I posted the code if someone could translate it into MASM for me. It would be greatly appreciated and I will be sure to give you credit in my program.

hutch--

Anthony,

You missed something in my email, the algo I sent you will take ANY length key from very short to longer than the source to encrypt. It just happens to be better if its long rather than short.

What you must pass it is the source to encrypt, its length, the key to encrypt against and its length. The algo will handle any length of either source of key.

The technology is at hand for you to write a simple DLL that will do what you need as masm32 has a DLL tutorial attached to it. Somewhere along the line, if you wish to learn to use asm, you will need to learn how to write it as while you will get help where you need it, no-one is going to write it for you.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Mark Jones

Hi Antony, can't your compiler spit out assembler code by passing it a command-line option or something? Put a few NOPs surrounding the code of interest and create an .asm output, then search for the NOPs. :U
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

hutch--

Here is a quick DLL that can be called from VB. It contains the algo I sent you by email built into a miniature DLL. It requires the address of zero terminated strings and their lengths to  be passed to it and it is worth writing the VB code to ensure you are not doing ANSI to UNICODE text conversions as this will kill any speed advantage stone dead.

With streamed XOR encryption, the value you can get from the encryption is purely how random the key is and the longer the random key, the better the encryption. Reuse of the same key degrades the encryption. Once you learn how to write this stuff, you will just pass the file to an assembler routine and perform the entire task outside of VB.

[attachment deleted by admin]
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

zooba

Quote from: Mark Jones on February 24, 2006, 06:38:13 PM
Hi Antony, can't your compiler spit out assembler code by passing it a command-line option or something? Put a few NOPs surrounding the code of interest and create an .asm output, then search for the NOPs. :U

I'm assuming VB version 6, since that was the latest available 6 years ago (and IMHO is still the best version out there :wink)

It's doable (by some API hooks) but ugly. VB doesn't directly support output other than EXE or DLL files, but it does convert to ASM before assembling. The emitted code is much more optimised than most people give it credit for but it's quite ugly to read (in terms of variable names, labels, etc). Also, inline ASM is not supported (though with said-API hooks it's possible, I recommend using a DLL though :U).

Cheers,

Zooba

Psycho

while the DLL you just posted Hutch performs some of the requirements it does not perform others. For example. The DLL must be able to accept strings, which this one does... but then it must export strings to the parent program for the purpose of the encrypted string then becoming content of the source file which is being handled as a variable, this variable is then passed back into the DLL along with a new key. This can also be done internally i think( although i have no idea how as i am still getting my head around the tutorials), and the key used changes with the source variable. At it's weakest there are 8 keys of 8 characters long and each key is used in one pass of the source. By this i mean pass1 uses key1, pass2 uses key2. Maybe it does meet the full requirements and im just reading it wrong. My knowledsge of ASM is not all that great at the moment because i am still learning the syntax.

Psycho

Additional, if you would like the VB source code for the function I require please feel free to ask, it may be easier than trying to explain things.

BogdanOntanu

I do not see what is "recursive" about this ... maybe "repetitive" but not "recursive".

For me "recursive" means a function that calls itself inside its own body pushing values and return locations on stack until an recursion end criteria is meet.

And nope, nobody will do the job for you here... we will give hints and indications...even source code at times.
But the real work and understandings will have to be done by yourself.
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

Psycho

I understand that Bogdan, i am not asking anyone to do the whole thing for me. The algorithm is loosely recursive, as in the output of the previous loop becomes the input for the next. As for doing it myself, as i have said my current knowledge of masm is extremely limited. I know how to do two of the basic programming structures structures, as in sequence and repetition. As yet I do know now how to do anything that involves selection, and am slowly grasping the passing of strings into the module, but as yet no one has shown me an example of how to pass a string from a masm module. Passing a string from the module is essential. It is the only thing retuired to complete the module as I am going to leave the loops and such in VB and have the masm module do the encryption and nothing else. Also is there a way to pass the string(file contents) and key directly into the module rather than passing the addresses of each?

Mincho Georgiev


zooba

There is no way to pass anything longer than a DWORD by value from VB. Passing a VB String (BSTR) 'ByVal' will pass the address of a copy of the string, while passing 'ByRef' will pass an address to the address to the string. For some reason, ByVal seems to be quicker.

Return values in VB6 behave like this:

Long, Integer, Byte - Returned in EAX
Double, Single - Returned in ST
Strings - Pointer returned in EAX
Structures (Types) - Pointer passed in first parameter

Strings must be allocated with a variant of SysAllocString (OLEAUT32) so they may be freed using SysFreeString.

The pointer passed in the first parameter for structures points to an uninitialised memory location of the size of the original structure.

Hope this helps :U

Zooba

donkey

Hi zooba,

I don't know anything at all about VB, never used it and was never really interested in it. But I would assume that since you must be making the declarations yourself you can just pass 2 DWORDs (low,high) to a function that needs a QWORD. In reality that is what any language does as no language can pass more than a DWORD at a time, it is after all the width of the stack in IA32 architecture.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

zooba

Of course you can do that. However, VB6 doesn't provide any 64-bit integer support (Long=32, Integer=16, Byte=8) without using structures so it's a pain to use for that anyway. As fair as VB is concerned, passing two DWORDs is just that - two parameters. It doesn't attempt to interpret it as anything else. The exceptions are 'ByVal ... Double' and 'ByVal ... Currency' (Currency is a fixed-point integer type) which are passed in two DWORD slots. However, VB has strict value checking and I haven't found a way of filling a floating point type with an arbitrary integer unless it can be converted to FP form.

MichaelW

"To handle strings that are allocated by one component and freed by another..."

MSDN: Component Automation, String Manipulation Functions

zooba,

If you passed a VB6 string ByVal to one of these functions, would it work?

eschew obfuscation

zooba

I believe so. The 'ByVal' version of a String is a pointer to a null-terminated string (which may contain embedded nulls). The 4-bytes preceeding the string provide the length (not including the terminating null, but including any embedded nulls).

The 'ByRef' version is a pointer to this pointer.