UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on June 26, 2011, 06:53:07 AM

Title: Encrypt Strings / Simple Game Data Protection
Post by: kevin on June 26, 2011, 06:53:07 AM
  Encrypt Strings  / Simple Game Data Protection

    Looking for a simple way to 'hide' your game data from those curious players ? - Well, all you need do is convert your sensitive save game data into something that's not easily edited via NotePad or a Hex editor.   The easiest way is to change how the data is encoded.  

    In this short example we're using the classic XOR style encoding, it's handy since it's dead simple to implement and we can use the same code to hide and show our data.

[pbcode]

   ; The data (in string form) that you want to hide
   MyData$="This is my secret message"

   ; Xor the characters to hide the message
    Secret$=Encode(MyData$)

   ; display the original and encoded data
    print "Original:"+MyData$
    print " Encoded:"+Secret$

   ; use the same Xor function to restore to it's original form.
    print " Decoded:"+Encode(Secret$)


   Sync
   waitkey
       
   


Function Encode(s$)

   For lp=1 to len(s$)
      ; grab the char as an ASCII value
      ThisChr=mid(s$,lp)

      ; Flip the bits (Xor) with the loop counter
      ThisChr=ThisChr xor lp

      ; Add this new character to the output
      r$+=chr$(ThisCHR)
      
   next

EndFunction r$

[/pbcode]



 Related Articles

  *  simple but effective encryption in PlayBasic (http://www.underwaredesign.com/forums/index.php?topic=818.0)



Title: Re: Encrypt Strings / Simple Game Data Protection
Post by: kevin on July 14, 2013, 02:24:23 PM
  Here's another way of encrypting the characters in a string using the rol8 and ror8 functions.  

[pbcode]

   s$="Hello worldHello worldHello worldHello worldHello worldHello world"
   

   // Encode string
   For lp=1 to Len(s$)
         ThisCHR=mid(s$,lp)
         ThisCHr=Rol8(ThisChr, lp and 7)
         es$+=chr$(ThisCHr)
   next


   print es$
   
   // decode string
   For lp=1 to Len(s$)
         ThisCHR=mid(es$,lp)
         ThisCHr=Ror8(ThisChr, lp and 7)
         ds$+=chr$(ThisCHr)
   next

   print ds$
   
   sync
   waitkey
      

[/pbcode]



Here's a bank version using much the same method.


[pbcode]

   ThisBank=MakeFilledBank(512)

   print "Original State:"
   ShowBank(ThisBank)


   StartingSHift=rnd(30)


   print "encoded Bank"
   EncodeBank(ThisBank,StartingSHift)
   ShowBank(ThisBank)
   

   print "decode Bank to restore"
   DecodeBank(ThisBank,StartingSHift)
   ShowBank(ThisBank)

   sync
   waitkey


      
      
Function ShowBank(ThisBank)
   ink $80a0f0

   For lp=0 to getbanksize(ThisBank)-1
         r$+= right$(hex$(PeekBankByte(ThisBank,lp)),2)
         Count++
         if Count=48
               print r$
               r$=""
               count=0
         endif
   next
         if Count>0
               print r$
         endif
   ink -1
EndFunction      
      
Function MakeFilledBank(Size)
   ThisBank=NewBank(Size)
   For lp =0 to size-1
         PokeBankByte thisbank,lp,lp
   next
EndFunction ThisBank


Function EncodeBank(ThisBank,Shift)

   if GetBankStatus(ThisBank)
      size=getbanksize(ThisBank)

      LongCount=Size/4
      if LongCount
         for lp =1 to LongCount   
               ThisInt=PeekBankInt(ThisBank,Ptr)
               ThisInt=Ror32(ThisInt,Shift)
               PokeBankInt ThisBank,Ptr,ThisInt
               Shift=mod(shift+1,30)+1
               Ptr+=4
         next

      endif

   endif
EndFunction



Function DecodeBank(ThisBank,Shift)

   if GetBankStatus(ThisBank)
      size=getbanksize(ThisBank)

      LongCount=Size/4
      if LongCount
         for lp =1 to LongCount   
               ThisInt=PeekBankInt(ThisBank,Ptr)
               ThisInt=Rol32(ThisInt,Shift)
               PokeBankInt ThisBank,Ptr,ThisInt
               Shift=mod(shift+1,30)+1
               Ptr+=4
         next

      endif

   endif
EndFunction

[/pbcode]