Read Write RollingEncryption - OOP

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Read Write RollingEncryption - OOP

Post by StarBootics »

Hello everyone,

A small library based on Bo Marchais's original code found in this forum.

The main purpose of the library is to make the data obscure in a file. It should not be used when security is needed.

I'm releasing it in hope it might be useful to some one.

Best regards
StarBootics

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; AUTOMATICALLY GENERATED CODE, DO NOT MODIFY
; UNLESS YOU REALLY, REALLY, REALLY MEAN IT !!
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Code generated by : Dev-Object - V1.1.0
; Project name : Read Write RollingEncryption
; File name : Read Write RollingEncryption - OOP.pb
; File Version : 1.0.1
; Programmation : OK
; Programmed by : StarBootics
; Creation Date : October 18th, 2020
; Last update : October 29th, 2020
; Coded for PureBasic : V5.72
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Programming notes
;
; Based on Bo Marchais's original code (english forum)
;
; The main purpose of the library is to make the data obscure.
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule RollingEncryption
  
  #Mode_String = 0
  #Mode_Binary = 1
  
  Interface RollingEncryption
    
    GetSalt.u()
    GetMode.b()
    SetSalt(P_Salt.u)
    SetMode(P_Mode.b)
    
    RE_WriteString(FileID.i, P_String.s)
    RE_WriteByte(FileID.i, P_Value.b)
    RE_WriteAsciiCharacter(FileID.i, P_Value.a)
    RE_WriteWord(FileID.i, P_Value.w)
    RE_WriteUnicodeCharacter(FileID.i, P_Value.u)
    RE_WriteCharacter(FileID.i, P_Value.c)
    RE_WriteLong(FileID.i, P_Value.l)
    RE_WriteQuad(FileID.i, P_Value.q)
    RE_WriteInteger(FileID.i, P_Value.i)
    RE_WriteFloat(FileID.i, P_Value.f)
    RE_WriteDouble(FileID.i, P_Value.d)
    
    RE_ReadString.s(FileID.i)
    RE_ReadByte.b(FileID.i)
    RE_ReadAsciiCharacter.a(FileID.i)
    RE_ReadWord.w(FileID.i)
    RE_ReadUnicodeCharacter.u(FileID.i)
    RE_ReadCharacter.c(FileID.i)
    RE_ReadLong.l(FileID.i)
    RE_ReadQuad.q(FileID.i)
    RE_ReadInteger.i(FileID.i)
    RE_ReadFloat.f(FileID.i)
    RE_ReadDouble.d(FileID.i)
    
    Free()
    
  EndInterface
  
  Declare.i New(P_Salt.u, P_Mode.b = #Mode_String)
  
EndDeclareModule

Module RollingEncryption
  
  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< Structure declaration <<<<<

  Structure Private_Members
    
    VirtualTable.i
    Salt.u
    Mode.b
    
  EndStructure
  
  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< The Private Encode operator <<<<<
  
  Procedure.s Private_Encode(*This.Private_Members, P_String.s)
    
    Max.l = Len(P_String)
    
    For i = 1 To Max
      encoded.s + Right("0000" + Hex(Asc(Mid(P_String,i,1)) ! ((*This\Salt + i) % 65535), #PB_Unicode), 4)
    Next
    
    ProcedureReturn UCase(encoded)
  EndProcedure
  
  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< The Private Decode operator <<<<<
  
  Procedure.s Private_Decode(*This.Private_Members, P_String.s)
    
    Max.l = Len(P_String) >> 2
    
    For i = 1 To Max
      decoded.s + Chr(Val("$"+Mid(P_String, ((i - 1) * 4) + 1, 4)) ! ((*This\Salt + i) % 65535))                     
    Next
    
    ProcedureReturn decoded
  EndProcedure
  
  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< The observators <<<<<

  Procedure.u GetSalt(*This.Private_Members)
    
    ProcedureReturn *This\Salt
  EndProcedure
  
  Procedure.b GetMode(*This.Private_Members)
    
    ProcedureReturn *This\Mode
  EndProcedure
  
  ; <<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< The mutators <<<<<

  Procedure SetSalt(*This.Private_Members, P_Salt.u)
    
    *This\Salt = P_Salt
    
  EndProcedure
  
  Procedure SetMode(*This.Private_Members, P_Mode.b)

    If P_Mode <= #Mode_String
      *This\Mode = #Mode_String
    ElseIf P_Mode >= #Mode_Binary
      *This\Mode = #Mode_Binary
    EndIf
    
  EndProcedure
  
  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< The Write on file operators <<<<<
  
  Procedure RE_WriteString(*This.Private_Members, FileID.i, P_String.s)
    
    If *This\Mode = #Mode_Binary
      
      Max.l = Len(P_String)
      WriteLong(FileID, Max)
      
      For i = 1 To Max
        WriteUnicodeCharacter(FileID, Asc(Mid(P_String, i, 1)) ! ((*This\Salt + i) % 65535))
      Next
      
    Else
      
      WriteStringN(FileID, Private_Encode(*This, P_String))
      
    EndIf
    
  EndProcedure
  
  Procedure RE_WriteByte(*This.Private_Members, FileID.i, P_Value.b)
    
    If P_Value < 0
      Sign.s = "-"
      P_Value = P_Value * -1
    EndIf
    
    RE_WriteString(*This, FileID, Sign + RSet(Str(P_Value), 6, "0"))
    
  EndProcedure
  
  Procedure RE_WriteAsciiCharacter(*This.Private_Members, FileID.i, P_Value.a)
    
    RE_WriteString(*This, FileID, RSet(Str(P_Value), 6, "0"))
    
  EndProcedure
  
  Procedure RE_WriteWord(*This.Private_Members, FileID.i, P_Value.w)
    
    If P_Value < 0
      Sign.s = "-"
      P_Value = P_Value * -1
    EndIf
    
    RE_WriteString(*This, FileID, Sign + RSet(Str(P_Value), 10, "0"))
    
  EndProcedure
  
  Procedure RE_WriteUnicodeCharacter(*This.Private_Members, FileID.i, P_Value.u)
    
    RE_WriteString(*This, FileID, RSet(Str(P_Value), 10, "0"))
    
  EndProcedure
  
  Procedure RE_WriteCharacter(*This.Private_Members, FileID.i, P_Value.c)
    
    RE_WriteString(*This, FileID, RSet(Str(P_Value), 10, "0"))
    
  EndProcedure
  
  Procedure RE_WriteLong(*This.Private_Members, FileID.i, P_Value.l)
    
    If P_Value < 0
      Sign.s = "-"
      P_Value = P_Value * -1
    EndIf
    
    RE_WriteString(*This, FileID, Sign + RSet(Str(P_Value), 22, "0"))
    
  EndProcedure
  
  Procedure RE_WriteQuad(*This.Private_Members, FileID.i, P_Value.q)
    
    If P_Value < 0
      Sign.s = "-"
      P_Value = P_Value * -1
    EndIf
    
    RE_WriteString(*This, FileID, Sign + RSet(Str(P_Value), 42, "0"))
    
  EndProcedure
  
  Procedure RE_WriteInteger(*This.Private_Members, FileID.i, P_Value.i)
    
    If P_Value < 0
      Sign.s = "-"
      P_Value = P_Value * -1
    EndIf
    
    RE_WriteString(*This, FileID, Sign + RSet(Str(P_Value), 42, "0"))
    
  EndProcedure
  
  Procedure RE_WriteFloat(*This.Private_Members, FileID.i, P_Value.f)
    
    RE_WriteString(*This, FileID, StrF(P_Value, 14))
    
  EndProcedure
  
  Procedure RE_WriteDouble(*This.Private_Members, FileID.i, P_Value.d)
    
    RE_WriteString(*This, FileID, StrD(P_Value, 25))
    
  EndProcedure

  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< The Read on file operators <<<<<
  
  Procedure.s RE_ReadString(*This.Private_Members, FileID.i)
    
    If *This\Mode = #Mode_Binary
      
      Max.l = ReadLong(FileID)
      
      For i = 1 To Max
        decoded.s + Chr(ReadUnicodeCharacter(FileID) ! ((*This\Salt + i) % 65535))                  
      Next
      
    Else
      decoded = Private_Decode(*This, ReadString(FileID))
    EndIf
    
    ProcedureReturn decoded
  EndProcedure
  
  Procedure.b RE_ReadByte(*This.Private_Members, FileID.i)
    
    ProcedureReturn Val(RE_ReadString(*This, FileID))
  EndProcedure
  
  Procedure.a RE_ReadAsciiCharacter(*This.Private_Members, FileID.i)
    
    ProcedureReturn Val(RE_ReadString(*This, FileID))
  EndProcedure
  
  Procedure.w RE_ReadWord(*This.Private_Members, FileID.i)
    
    ProcedureReturn Val(RE_ReadString(*This, FileID))
  EndProcedure
  
  Procedure.u RE_ReadUnicodeCharacter(*This.Private_Members, FileID.i)
    
    ProcedureReturn Val(RE_ReadString(*This, FileID))
  EndProcedure
  
  Procedure.c RE_ReadCharacter(*This.Private_Members, FileID.i)
    
    ProcedureReturn Val(RE_ReadString(*This, FileID))
  EndProcedure
  
  Procedure.l RE_ReadLong(*This.Private_Members, FileID.i)
    
    ProcedureReturn Val(RE_ReadString(*This, FileID))
  EndProcedure
  
  Procedure.q RE_ReadQuad(*This.Private_Members, FileID.i)
    
    ProcedureReturn Val(RE_ReadString(*This, FileID))
  EndProcedure
  
  Procedure.i RE_ReadInteger(*This.Private_Members, FileID.i)
    
    ProcedureReturn Val(RE_ReadString(*This, FileID))
  EndProcedure
  
  Procedure.f RE_ReadFloat(*This.Private_Members, FileID.i)
    
    ProcedureReturn ValF(RE_ReadString(*This, FileID))
  EndProcedure
  
  Procedure.d RE_ReadDouble(*This.Private_Members, FileID.i)
    
    ProcedureReturn ValD(RE_ReadString(*This, FileID))
  EndProcedure
  
  ; <<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< The Destructor <<<<<

  Procedure Free(*This.Private_Members)
    
    FreeStructure(*This)
    
  EndProcedure
  
  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< The Constructor <<<<<

  Procedure.i New(P_Salt.u, P_Mode.b = #Mode_String)
    
    *This.Private_Members = AllocateStructure(Private_Members)
    *This\VirtualTable = ?START_METHODS
    
    SetSalt(*This, P_Salt)
    SetMode(*This, P_Mode)
    
    ProcedureReturn *This
  EndProcedure
  
  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; <<<<< The Virtual Table Entries <<<<<

  DataSection
    START_METHODS:
    Data.i @GetSalt()
    Data.i @GetMode()
    Data.i @SetSalt()
    Data.i @SetMode()
    Data.i @RE_WriteString()
    Data.i @RE_WriteByte()
    Data.i @RE_WriteAsciiCharacter()
    Data.i @RE_WriteWord()
    Data.i @RE_WriteUnicodeCharacter()
    Data.i @RE_WriteCharacter()
    Data.i @RE_WriteLong()
    Data.i @RE_WriteQuad()
    Data.i @RE_WriteInteger()
    Data.i @RE_WriteFloat()
    Data.i @RE_WriteDouble()
    Data.i @RE_ReadString()
    Data.i @RE_ReadByte()
    Data.i @RE_ReadAsciiCharacter()
    Data.i @RE_ReadWord()
    Data.i @RE_ReadUnicodeCharacter()
    Data.i @RE_ReadCharacter()
    Data.i @RE_ReadLong()
    Data.i @RE_ReadQuad()
    Data.i @RE_ReadInteger()
    Data.i @RE_ReadFloat()
    Data.i @RE_ReadDouble()
    Data.i @Free()
    END_METHODS:
  EndDataSection
  
EndModule

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Code generated in : 00.001 seconds (87000.00 lines/second) <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

CompilerIf #PB_Compiler_IsMainFile

  Varw.w = 32700
  Varl.l = -2147483645
  Varq.q = 9223372036854775800
  Varf.f = 2 * #PI
  Vard.d = 3 * #PI
  Text.s = "J'aime les déesses nordiques super sexy !"
  
  Debug "; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
  Debug "; Test on file"
  Debug ""
  Debug "The Original data : "
  
  Debug Varw
  Debug Varl
  Debug Varq
  Debug Varf
  Debug Vard
  Debug Text
  Debug ""
  
  If CreateFile(0, "Test.Enc")
    
    RE.RollingEncryption::RollingEncryption = RollingEncryption::New(0, RollingEncryption::#Mode_Binary)
    
    RE\SetSalt(65342) ; Here we change the "Salt" on the fly for the first 3 data
    RE\RE_WriteWord(0, Varw) 
    RE\RE_WriteLong(0, Varl)  
    RE\RE_WriteQuad(0, Varq)
    RE\SetSalt(32123) ; Here we change the "Salt" on the fly for the last 3 data
    RE\RE_WriteFloat(0, Varf)
    RE\RE_WriteDouble(0, Vard)
    RE\RE_WriteString(0, Text)
    
    CloseFile(0)
    RE\Free()
    
  EndIf 
  
  Debug "From the file : " 
  
  If ReadFile(1, "Test.Enc")
    
    RE.RollingEncryption::RollingEncryption = RollingEncryption::New(0, RollingEncryption::#Mode_Binary)
    
    RE\SetSalt(65342) ; Here we change the "Salt" on the fly for the first 3 data
    Debug RE\RE_ReadWord(1)
    Debug RE\RE_ReadLong(1)
    Debug RE\RE_ReadQuad(1)
    RE\SetSalt(32123) ; Here we change the "Salt" on the fly for the last 3 data
    Debug RE\RE_ReadFloat(1)
    Debug RE\RE_ReadDouble(1)
    Debug RE\RE_ReadString(1)
    
    CloseFile(1)
    DeleteFile("Test.Enc")
    RE\Free()
    
  EndIf 
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Last edited by StarBootics on Fri Oct 30, 2020 12:18 am, edited 1 time in total.
The Stone Age did not end due to a shortage of stones !
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Read Write RollingEncryption - OOP

Post by Saki »

Hi, no offense, but this is useless stuff. :wink:
地球上の平和
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Read Write RollingEncryption - OOP

Post by StarBootics »

Saki wrote:Hi, no offense, but this is useless stuff. :wink:
Hello, no offense, but I like to make useless stuff :wink:

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Read Write RollingEncryption - OOP

Post by Saki »

Nice,
but, make a look on AES, you can simple create a AES based random Generator.
The possibilities are enormous.

Best Regards Saki
地球上の平和
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Read Write RollingEncryption - OOP

Post by StarBootics »

Saki wrote:Nice,
but, make a look on AES, you can simple create a AES based random Generator.
The possibilities are enormous.

Best Regards Saki
I think you miss the point here, I want to Read and Write binary file little bit obscurely. For my needs I'm hesitating between "RollingEncryption", "RandomEncryption" and "XTEA". Simply because it will be enough.

That being said, I will take a look at the standard AES Encoder()/Decoder() and make something completely secure, but this will be way way overkill for my needs.

Be patient.

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
Post Reply