Page 1 of 1

Crypt String with Xor

Posted: Sat May 21, 2005 10:28 pm
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)

Posted: Mon Jul 04, 2005 1:18 am
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]

Posted: Mon Jul 04, 2005 2:23 am
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.

Posted: Mon Jul 04, 2005 6:08 am
by Droopy
Yes a XOR between 2 same code = 0 = end string !!

You must select a good password

Posted: Mon Jul 04, 2005 8:10 am
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

Re: Crypt String with Xor

Posted: Fri Jun 20, 2025 9:11 pm
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?

Re: Crypt String with Xor

Posted: Fri Jun 20, 2025 11:11 pm
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]

Re: Crypt String with Xor

Posted: Fri Jun 20, 2025 11:58 pm
by pthien
Oh thank you Taz!

I completely forgot about unicode again.