Encryption thingy using Xor and a key

Share your advanced PureBasic knowledge/code with the community.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Encryption thingy using Xor and a key

Post 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$
Last edited by Joakim Christiansen on Wed Mar 09, 2011 7:39 am, edited 3 times in total.
I like logic, hence I dislike humans but love computers.
User avatar
Crusiatus Black
Enthusiast
Enthusiast
Posts: 389
Joined: Mon May 12, 2008 1:25 pm
Location: The Netherlands
Contact:

Post by Crusiatus Black »

very useful, thanks!
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post 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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post 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
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

It is only one please.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post 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.
I like logic, hence I dislike humans but love computers.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

character, stringbytelength and some other small things do the trick :)
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Post Reply