Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / String Encryption

Author
Message
Westmere
15
Years of Service
User Offline
Joined: 12th Mar 2010
Location: Germany
Posted: 26th Feb 2011 19:52
I've been using a simple function to encrypt/decrypt texts for a couple of years in Delphi now. Works perfectly there. I now TRIED to translate it to Dark Basic and for the most part it was just a work of changing := to = and removing trailing ;

The function appeared to work fine at first but after a couple of test strings I found that it occasionally mixes in a wrong letter which is especially strange since the function is built up in a way that it should return jiberish if something is wrong.

Before the code a little description of how it works or rather used to work:

It is loosely based on the enigma mechanism used for military communication by germany during WWII (thus the name, although the real enigma is a lot more complicated).
It basically uses 4 strings: The text to encrypt/decrypt, a password, a key and a filter.
1. It runs through the original text string position by position. For each new position the position will also be increased in the password and wrapped around to the 1 position in the password when there are no more positions in the password.
2. It then uses the ASCII value of the character in the password and increases the position in the key (a longer string) by that value again wrapping it's value back into a viable range if it gets to large.
3. The ASCII value found in the key is used again to advance the position in the filter but not from a previous position as in the key and the password but from the position of the character to encode/decode inside the filter. If the text is to be encrypted it will move in positiv direction, otherwise in negative direction.
4. The resulting character is then added to the encrypted string.

Advantages:
- Since especially the key and for some part the filter look like a lot of jiberish it can be hidden even as a string in a program (especially if you split it up)
- Because of the "multi-rotating strings" even strings like "AAAAAAA" will be encrypted into something that does not look like a lot of the same ("e90j8q0" with the example strings below)

Now for some reason the translated version would spit out "AAuAAAu" in this example and I can't seem to find the reason for this. It can hardly be an error in the positioning in the strings as that would result in the rest being returned as jiberish as well.

Maybe one of you can spot the mistake in this:



mr_d
DBPro Tool Maker
18
Years of Service
User Offline
Joined: 26th Mar 2007
Location: Somewhere In Australia
Posted: 27th Feb 2011 13:34 Edited at: 27th Feb 2011 13:41
hi Westmere,
I had a play with your program code, and after some debugging think I've tracked down what the problem is.
I believe the problem is with your filter definition, it should only have each valid character in there once, you have "0" in there 2 times. if you remove either one, that fixes the problem.
One other bit of the code that I am questioning whether it has the right logic (although I haven't been able to find an example that can produce a problem to confirm).
This is in your decrypt portion where there is the statement

I believe this should be:

Like I said I can't prove this, but it just looks right to me, cheers

(\___/)
(='.'=) Copy bunny into your signature to
("_(" help him gain world domination.
Westmere
15
Years of Service
User Offline
Joined: 12th Mar 2010
Location: Germany
Posted: 27th Feb 2011 15:47


You are absolutely right! The filter IS supposed to have every character just once. I must have been confused when I typed THAT up for testing purposes and never actually thought about looking there for the problem...

And of course you are right about the second part as well. I had played around with those values when I tried to find what went wrong and suspected DBP might do something with the positioning different then Delphi. Finding an example that messes up here should be pretty hard thought

This once again proves that most bugs are actually pretty stupid...

So thanks a lot for taking the time and squasing my bug
Maybe I can return the favor sometime.

mr_d
DBPro Tool Maker
18
Years of Service
User Offline
Joined: 26th Mar 2007
Location: Somewhere In Australia
Posted: 27th Feb 2011 16:00
Cool
Yeah - I've found it true myself that when trying to find those annoying problems with code, it can sometimes be really easy to mess things around that weren't part of the original issue at all and make the problem even worse.
No problem; glad to be of help. It was good to have a problem like this to look at as it keeps the old brain working Cheers.


(\___/)
(='.'=) Copy bunny into your signature to
("_(" help him gain world domination.
mr_d
DBPro Tool Maker
18
Years of Service
User Offline
Joined: 26th Mar 2007
Location: Somewhere In Australia
Posted: 28th Feb 2011 09:42
Hi Westmere,
While looking into your problem, I noticed a number of optimisations that were possible by mainly rearranging and reducing the number of times some commands/functions were called.
Please find below an updated version of your enigma function that should be comparatively faster if you have either long encryption/decryption strings, or if you call this function multiple times.


(\___/)
(='.'=) Copy bunny into your signature to
("_(" help him gain world domination.
Grog Grueslayer
Valued Member
20
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 28th Feb 2011 17:18
@ Westmere:

What I generally do for encryption/decryption is to use a random seed to generate an encryption key so creating a key string in code isn't needed and there's no chance of accidentally reusing characters.

This works because random number picking in Darkbasic Pro (anything really) isn't exactly random so using the same exact random seed number will always pick the same random numbers 100% of the time making it perfect for encryption/decryption routines.

The following code snip shows two different seeds to show that the same random seed number will indeed pick the same exact characters and encrypt/decrypt properly.



Login to post a reply

Server time is: 2025-06-07 09:33:09
Your offset time is: 2025-06-07 09:33:09