Share your advanced PureBasic knowledge/code with the community.
Hroudtwolf
Addict
Posts: 803 Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:
Post
by Hroudtwolf » Sat Sep 13, 2008 11:59 am
Hi,
This is a way to encode/decode strings very fast and easy.
The decoding function rotates the bits of each char of the whole string to the right.
Also, the encoding function rotates the bits of each char of the whole string. But to the left.
Best regards
Wolf
Code: Select all
; Hroudtwolf
; 09/10/2008
; PureBasic 4.x
; Windows, Linux, OS X
Procedure.c EncryptChar ( cValue.c )
CompilerIf #PB_Compiler_Unicode
! rol word [p.v_cValue], 1
CompilerElse
! rol byte [p.v_cValue], 1
CompilerEndIf
ProcedureReturn cValue
EndProcedure
Procedure.c DecryptChar ( cValue.c )
CompilerIf #PB_Compiler_Unicode
! ror word [p.v_cValue], 1
CompilerElse
! ror byte [p.v_cValue], 1
CompilerEndIf
ProcedureReturn cValue
EndProcedure
Procedure.s EncryptString ( sString.s )
Protected *Source.CHARACTER = @ sString
If Not *Source
ProcedureReturn ""
EndIf
While *Source\c
*Source\c = EncryptChar ( *Source\c )
*Source + SizeOf ( CHARACTER )
Wend
ProcedureReturn sString
EndProcedure
Procedure.s DecryptString ( sString.s )
Protected *Source.CHARACTER = @ sString
If Not *Source
ProcedureReturn ""
EndIf
While *Source\c
*Source\c = DecryptChar ( *Source\c )
*Source + SizeOf ( CHARACTER )
Wend
ProcedureReturn sString
EndProcedure
Define.s sOriginal = "Feel the pure power! PureBasic 4.20"
Define.s sEncoded
Define.s sDecoded
sEncoded = EncryptString ( sOriginal )
sDecoded = DecryptString ( sEncoded )
Debug "Original: " + sOriginal
Debug "Encoded : " + sEncoded
Debug "Decoded : " + sDecoded
Michael Vogel
Addict
Posts: 2810 Joined: Thu Feb 09, 2006 11:27 pm
Contact:
Post
by Michael Vogel » Sat Sep 13, 2008 12:24 pm
cool!
two questions...
1) does ROL WORD play around with one byte or two bytes?
in the second case, the string chr(32)+chr(128) would be changed to chr(65)+chr(0)
2) is it possible, to change the fixed amount of roled bits (=1) to a dynamic value (e.g. in dependency of the character position)?
Hroudtwolf
Addict
Posts: 803 Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:
Post
by Hroudtwolf » Sat Sep 13, 2008 12:43 pm
Hi,
@Michael Vogel
1) ROL does rotate a word, 2 bytes, in unicode mode. In unicode, the chartable is bigger than in ASCII mode.
2) Yes, of course. Just change the 2nd operand of ROL/ROR to a variable.
Best regards
Wolf
Xombie
Addict
Posts: 898 Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:
Post
by Xombie » Thu Sep 25, 2008 3:33 pm
Thanks! I was looking for a simple and nice function like this
rsts
Addict
Posts: 2736 Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA
Post
by rsts » Thu Sep 25, 2008 4:29 pm
Nice and handy.
Thanks
Guimauve
Enthusiast
Posts: 742 Joined: Wed Oct 22, 2003 2:51 am
Location: Canada
Post
by Guimauve » Wed Apr 01, 2009 12:10 am
I need these function for a small projet. Because the orginal code is not mine I return it here. It's just a small modification from the original.
Code: Select all
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : LightWeightEncryptString
; File : LightWeightEncryptString.pb
; File Version : 1.0.0
; Programmation : OK
; Programmed by : Hroudtwolf
; Modified by : Guimauve
; Date : 09-10-2008
; Last Update : 31-03-2009
; Coded for PureBasic V4.30
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Enumeration
#LIGHTWEIGHT_ENCRYPT
#LIGHTWEIGHT_DECRYPT
EndEnumeration
Procedure.c Private_RotateChar(Value.c, Direction.b)
Select Direction
Case #LIGHTWEIGHT_ENCRYPT
CompilerIf #PB_Compiler_Unicode
! rol word [p.v_Value], 2
CompilerElse
! rol byte [p.v_Value], 2
CompilerEndIf
Case #LIGHTWEIGHT_DECRYPT
CompilerIf #PB_Compiler_Unicode
! ror word [p.v_Value], 2
CompilerElse
! ror byte [p.v_Value], 2
CompilerEndIf
EndSelect
ProcedureReturn Value
EndProcedure
ProcedureDLL.s LightWeightEncryptString(String.s)
Protected *Source.CHARACTER = @String
If *Source = #Null
String = ""
Else
While *Source\c
*Source\c = Private_RotateChar(*Source\c, #LIGHTWEIGHT_ENCRYPT)
*Source + SizeOf(CHARACTER)
Wend
EndIf
ProcedureReturn String
EndProcedure
ProcedureDLL.s LightWeightDecryptString(String.s)
Protected *Source.CHARACTER = @String
If *Source = #Null
String = ""
Else
While *Source\c
*Source\c = Private_RotateChar(*Source\c, #LIGHTWEIGHT_DECRYPT)
*Source + SizeOf(CHARACTER)
Wend
EndIf
ProcedureReturn String
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< TESTING CODE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<
Define.s sOriginal = "Feel the pure power! PureBasic 4.20"
Define.s sEncoded
Define.s sDecoded
sEncoded = LightWeightEncryptString(sOriginal)
sDecoded = LightWeightDecryptString(sEncoded)
Debug "Original: " + sOriginal
Debug "Encoded : " + sEncoded
Debug "Decoded : " + sDecoded
; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Regards
Guimauve