Page 1 of 1

Encryption thingy using Xor and a key

Posted: Tue Apr 07, 2009 12:57 pm
by Joakim Christiansen
EDIT: My latest algorithm is here: http://www.purebasic.fr/english/viewtop ... 12&t=45704

Didn't really plan to post this (since there are enough stuff like this laying around). But maybe someone likes it, at least it's more secure than the "simple ones", and it's damn easy to use. Feel free to customize.

Code: Select all

Procedure XOrCrypt(*Buffer,Len.l,Key$)
  ;just some weird xor encryption algorithm by JLC
  Protected i, Byte.b, KeyByte.b
  Protected KeyLength = Len(Key$), KeyPos
  
  For i=0 To Len-1
    Byte = PeekB(*Buffer+i)
    KeyByte = PeekB(@Key$+KeyPos)
    ;PeekB(*Buffer+i) ! PeekB(@Key$+KeyPos) ! Len ! i
    Byte ! KeyByte ! Len ! i ! KeyLength ;xor the shit
    PokeB(*Buffer+i,Byte)
    KeyPos + 1
    If KeyPos > KeyLength
      KeyPos = 0
    EndIf
  Next
EndProcedure

String$ = "the string to be encrypted"
Length = Len(String$)

XOrCrypt(@String$,Length,"key of whatever length") ;encrypt
Debug String$ ;if it looks cut that's because a null-char. terminates it

XOrCrypt(@String$,Length,"key of whatever length") ;decrypt
Debug String$

Posted: Wed Apr 29, 2009 6:51 pm
by Crusiatus Black
very useful, thanks!

Posted: Wed Apr 29, 2009 8:17 pm
by ts-soft
:D you post very usefull functions the last days, but allways no unicode-
support. My actual projects all unicode, so i can't make usage of or have to
rewrite the stuff.

This snippet is easy to make unicode-compatible but the others :(

Code: Select all

Procedure XOrCrypt(*Buffer, Len.l, Key$)
  ;just some weird and powerful(?) xor encryption algorithm by JLC
  Protected i, Char.c, KeyChar.c
  Protected KeyLength = Len(Key$), KeyPos
 
  For i = 0 To Len - 1
    Char = PeekC(*Buffer + i * SizeOf(Character))
    KeyChar = PeekC(@Key$ + KeyPos)

    Char ! KeyChar ! Len ! i ! KeyLength ;xor the shit
    PokeC(*Buffer + i * SizeOf(Character), Char)
    KeyPos + SizeOf(Character)
    If KeyPos > KeyLength
      KeyPos = 0
    EndIf
  Next
EndProcedure

String$ = "the string to be encrypted"
Length = Len(String$)

XOrCrypt(@String$,Length,"key of whatever length") ;encrypt
Debug String$ ;if it looks cut that's because a null-char. terminates it

XOrCrypt(@String$,Length,"key of whatever length") ;decrypt
Debug String$
greetings

Thomas

Posted: Wed Apr 29, 2009 8:20 pm
by rsts
Please don't stop posting the tips because they're not unicode :)

They are very useful to those of us who do not require unicode and may form a basis for those who do.

cheers

Posted: Wed Apr 29, 2009 8:26 pm
by ts-soft
It is only one please.

Posted: Wed Apr 29, 2009 8:44 pm
by Joakim Christiansen
ts-soft wrote::D you post very usefull functions the last days, but allways no unicode-support. My actual projects all unicode, so i can't make usage of or have to rewrite the stuff.
Sorry about that. But I have never really taken the time to understand unicode or have had the need to use it. Hmm, I guess the trick mostly is like you've done:

Code: Select all

Char = PeekC(*Buffer + i * SizeOf(Character)) 
I'll see if I can change my habbits then.

Posted: Wed Apr 29, 2009 8:48 pm
by ts-soft
character, stringbytelength and some other small things do the trick :)