Crypt String with Xor

Share your advanced PureBasic knowledge/code with the community.
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Crypt String with Xor

Post by Droopy »

Code updated for 5.20+

Just a simple encoding with Xor

Code: Select all

Procedure.s XorCrypt(Key.s,String.s)
  
  For n=1 To Len(String)
    ChrString=Asc(Mid(String,n,1))
    ChrKey=Asc(Mid(Key,Ptr+1,1))
    If ChrString=ChrKey
      ChrCrypt=ChrString
    Else
      ChrCrypt=ChrString ! ChrKey
    EndIf
    
    Retour.s+Chr(ChrCrypt)
    Ptr+1
    If Ptr >Len(Key) : Ptr=0 : EndIf
  Next
  ProcedureReturn Retour
EndProcedure
Test with this :

Code: Select all

Key.s="This is the Key"
xx.s= XorCrypt(Key,"This is the String to Crypt")
Debug xx
Debug XorCrypt(Key,xx)
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Result is:

Code: Select all

This is the i:IOI0R
This is the String to Crypt
This code only encrypts a peice of the string and not the whole thing.[/code]
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Shannara wrote:Result is:

Code: Select all

This is the i:IOI0R
This is the String to Crypt
This code only encrypts a peice of the string and not the whole thing.[/code]
At a guess, that is because the unencrypted characters match (key=value) and so the test avoids this otherwise there would be a null (0) terminator embedded into the middle of the string.
@}--`--,-- A rose by any other name ..
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Yes a XOR between 2 same code = 0 = end string !!

You must select a good password
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Self encrypt/decrypt (is own key) with Xor. :)

Code: Select all

Procedure.s selfE(src.s,en.l)
  k1=Len(src)
  If k1>0
    *p=@src
    k2=PeekB(*p) & $FF
    r=k1 ! k2
    If r<>0 : PokeB(*p,r) : EndIf
    For i=2 To Len(src)
      *p+1
      If en : k1=PeekB(*p-1) & $FF : Else : k1=k2 : EndIf
      k2=PeekB(*p)
      r=k1 ! k2
      If r<>0 : PokeB(*p,r) : EndIf
    Next
  EndIf
  ProcedureReturn src
EndProcedure

w.s="Wooo! Hooo! This is self encrypting"
x.s=selfE(w,#True)
y.s=selfE(x,#False)
Debug w
Debug x
Debug y
@}--`--,-- A rose by any other name ..
pthien
Enthusiast
Enthusiast
Posts: 148
Joined: Sun Jun 29, 2003 9:39 pm

Re: Crypt String with Xor

Post by pthien »

The self-encrypting code doesn't seem to work, my debug output is:

Wooo! Hooo! This is self encrypting
瑴ᬛ瑴ᬛ㨺ᨚ剒㴽剒㴽ᰜ㰼桨桨ā牲剒;s self encrypting
瑗᭯瑯᭯㨡ᨠ剈㵯副㵯ᰡ㰠桔桨ũ牳删is self encrypting

I'm using 6.04LTS Windows x86, if that is important.

Any ideas?

If I'm reading the code correctly, this seems to encrypt each character using the previous character, and stores the starting character required with the encrypted string?
Taz
User
User
Posts: 75
Joined: Sat Jan 20, 2018 5:28 pm
Location: Germany

Re: Crypt String with Xor

Post by Taz »

here you go, without poke, and with Unicode support :-)

Code: Select all

EnableExplicit

Procedure.s selfE(src.s, en.b = #True)
	Protected i.i, k1.i, k2.i, r.i, *p.Character = @src, srcLen.i = Len(src)

	If srcLen
		k2 = *p\c
		r = srcLen ! k2
		If r
			*p\c = r
		EndIf
		For i=2 To srcLen
			*p + SizeOf(Character)
			If en
				k1 = PeekC(*p - SizeOf(Character))
			Else
				k1 = k2
			EndIf
			k2 = *p\c
			r = k1 ! k2
			If r
				*p\c = r
			EndIf
		Next
	EndIf
	ProcedureReturn src
EndProcedure

Define Text.s = "Wooo! Hooo! This is self encrypting"
Define x.s = selfE(Text)
Define y.s = selfE(x, #False)

Debug "Text["+Text+"]"
Debug "   x["+x+"]"
Debug "   y["+y+"]"

Code: Select all

Text[Wooo! Hooo! This is self encrypting]
   x[tt:R=R=<hhrR;Hh~tT1_<N7G3Z4S]
   y[Wooo! Hooo! This is self encrypting]
pthien
Enthusiast
Enthusiast
Posts: 148
Joined: Sun Jun 29, 2003 9:39 pm

Re: Crypt String with Xor

Post by pthien »

Oh thank you Taz!

I completely forgot about unicode again.
Post Reply