Lightweight de-/encoding for strings.
Posted: 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
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