Page 1 of 1

Save and LoadJSONCrypted()

Posted: Thu Feb 06, 2020 10:06 am
by infratec
Maybe this is of interest:

Save it as "JSONCrypted.pbi"

Code: Select all

;
; JSONCrypted.pbi
;
; https://www.purebasic.fr/english/viewtopic.php?p=548630#p548630
;


CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf


Procedure.i LoadJSONCrypted(JSON.i, Filename$, *Key, *InitializationVector, Flags.i=0)
  
  Protected.i Result, File, FileSize
  Protected JSON$
  Protected *InBuffer, *OutBuffer
  
  
  File = ReadFile(#PB_Any, Filename$)
  If File
    FileSize = Lof(File)
    *InBuffer = AllocateMemory(FileSize, #PB_Memory_NoClear)
    If *InBuffer
      If ReadData(File, *InBuffer, FileSize) = FileSize
        
        *OutBuffer = AllocateMemory(FileSize, #PB_Memory_NoClear)
        If *OutBuffer
          
          If AESDecoder(*InBuffer, *OutBuffer, FileSize, *Key, 256, *InitializationVector)
            JSON$ = PeekS(*OutBuffer, FileSize, #PB_UTF8|#PB_ByteLength)
            Result = ParseJSON(JSON, JSON$, Flags)
          EndIf
          
          FreeMemory(*OutBuffer)
        EndIf
        
      EndIf
      FreeMemory(*InBuffer)
    EndIf
    CloseFile(File)
  EndIf
  
  ProcedureReturn Result
  
EndProcedure


Procedure.i SaveJSONCrypted(JSON.i, Filename$, *Key, *InitializationVector, Flags.i=0)
  
  Protected.i Result, JSONSize, File
  Protected *InBufffer, *OutBuffer
  
  
  If IsJSON(JSON)
    JSONSize = ExportJSONSize(JSON)
    If JSONSize > 0
      *InBufffer = AllocateMemory(JSONSize, #PB_Memory_NoClear)
      If *InBufffer
        JSONSize = ExportJSON(JSON, *InBufffer, JSONSize)
        If JSONSize > 0
          *OutBuffer = AllocateMemory(JSONSize, #PB_Memory_NoClear)
          If *OutBuffer
            If AESEncoder(*InBufffer, *OutBuffer, JSONSize, *Key, 256, *InitializationVector)
              File = CreateFile(#PB_Any, Filename$)
              If File
                If WriteData(File, *OutBuffer, JSONSize) = JSONSize
                  Result = #True
                EndIf
                CloseFile(File)
              EndIf
            EndIf
            FreeMemory(*OutBuffer)
          EndIf
        EndIf
        FreeMemory(*InBufffer)
      EndIf
    EndIf
  EndIf
  
  ProcedureReturn Result
  
EndProcedure





CompilerIf #PB_Compiler_IsMainFile
  
  
  Structure UserStructure
    Name$
    HiScore.i
  EndStructure
  
  
  Structure GameStructure
    List UserList.UserStructure()
    HighScore.i
  EndStructure
  
  
  Define.i JSON, Person, Values, i, NewJSON
  Define.GameStructure Game, Loaded
  
  AddElement(Game\UserList())
  Game\UserList()\Name$ = "John Player"
  Game\UserList()\HiScore = 1234
  
  AddElement(Game\UserList())
  Game\UserList()\Name$ = "Donald Looser"
  Game\UserList()\HiScore = 123
  
  Game\HighScore = 1234
  
  ; Build JSON data from scratch
  ;
  JSON = CreateJSON(#PB_Any)
  If JSON
    InsertJSONStructure(JSONValue(JSON), @Game, GameStructure)
    
    Debug "---------- Compact format ----------"
    Debug ""
    Debug ComposeJSON(JSON)
    Debug ""
    Debug "---------- Pretty-Printed format ----------"
    Debug ""
    Debug ComposeJSON(JSON, #PB_JSON_PrettyPrint)
    Debug ""
    
    
    If SaveJSONCrypted(JSON, "test.dat", ?JSONCryptedKey, ?JSONCryptedInitializationVector)
      Debug "Saved crypted"
      
      NewJSON = LoadJSONCrypted(#PB_Any, "test.dat", ?JSONCryptedKey, ?JSONCryptedInitializationVector)
      If NewJSON
        Debug "Load crypted:"
        Debug ComposeJSON(JSON)
        
        ExtractJSONStructure(JSONValue(NewJSON), @Loaded, GameStructure)
        ForEach Loaded\UserList()
          Debug Loaded\UserList()\Name$ + ": " + Str(Loaded\UserList()\HiScore)
        Next
        Debug "Game Highscore: " + Str(Loaded\HighScore)
        
        FreeJSON(NewJSON)
      EndIf
      
    EndIf
    
    FreeJSON(JSON)
  EndIf
  
  
  
  
  
  DataSection
    JSONCryptedKey:
    Data.a $06, $a9, $21, $40, $36, $b8, $a1, $5b, $51, $2e, $03, $d5, $34, $12, $00, $06, $06, $a9, $21, $40, $36, $b8, $a1, $5b, $51, $2e, $03, $d5, $34, $12, $00, $06
    
    JSONCryptedInitializationVector:
    Data.a $3d, $af, $ba, $42, $9d, $9e, $b4, $30, $b4, $22, $da, $80, $2c, $9f, $ac, $41
  EndDataSection
  
CompilerEndIf

Re: Save and LoadJSONCrypted()

Posted: Fri Feb 07, 2020 5:00 pm
by Kwai chang caine
Works fine here, thanks for sharing 8)

Re: Save and LoadJSONCrypted()

Posted: Fri Feb 07, 2020 10:41 pm
by tester
ParseJSON return error "Unexpected character" without this correction:

Code: Select all

If AESDecoder(*InBuffer, *OutBuffer, FileSize, *Key, 256, *InitializationVector)
  ;JSON$ = PeekS(*OutBuffer, -1, #PB_UTF8)
  JSON$ = PeekS(*OutBuffer, FileSize, #PB_UTF8)
  Result = ParseJSON(JSON, JSON$, Flags)
EndIf

Re: Save and LoadJSONCrypted()

Posted: Sat Feb 08, 2020 11:11 am
by infratec
@tester

I fixed this in the code above.
But if you use a specific length in byte, you need to use

Code: Select all

JSON$ = PeekS(*OutBuffer, FileSize, #PB_UTF8|#PB_ByteLength)