Page 1 of 1

Lightweight de-/encoding for strings.

Posted: Sat Sep 13, 2008 11:59 am
by Hroudtwolf
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

Posted: Sat Sep 13, 2008 12:24 pm
by Michael Vogel
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)?

Posted: Sat Sep 13, 2008 12:43 pm
by Hroudtwolf
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

Posted: Thu Sep 25, 2008 3:33 pm
by Xombie
Thanks! I was looking for a simple and nice function like this :)

Posted: Thu Sep 25, 2008 4:29 pm
by rsts
Nice and handy.

Thanks

Posted: Wed Apr 01, 2009 12:10 am
by Guimauve
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