Tiny Encryption Algorithm - Module

Share your advanced PureBasic knowledge/code with the community.
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Tiny Encryption Algorithm - Module

Post by StarBootics »

Hello everyone,

A simple Tiny Encryption Algorithm module for light encryption duty. I'm also working on the XTEA or eXtended Tiny Encryption Algorithm version, I will post it here as soon as I can make it to work !

Have fun !

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : TinyEncryptionAlgorithm - Module
; File Name : TinyEncryptionAlgorithm - Module.pb
; File version: 1.1.0
; Programming : OK
; Programmed by : StarBootics
; Date : 24-05-2015
; Last Update : 25-05-2015
; PureBasic code : V5.31
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule TEA
  
  Declare.s Encrypt(Key.s, Message.s)
  Declare.s Decrypt(Key.s, Message.s)
  
EndDeclareModule

Module TEA
  
  Structure Chars
    
    CompilerIf #PB_Compiler_Unicode
      Values.u[4]
    CompilerElse
      Values.a[8]
    CompilerEndIf   
    
  EndStructure
  
  Structure Key
    
    Values.l[4]
    
  EndStructure  
  
  Structure Block
    
    Values.l[2]
    
  EndStructure
  
  #DELTA = 2654435769
  
  Procedure Private_Encrypt(*Key.Key, *Block.Block)
    
    Sum.l = 0
    
    For RoundID = 0 To 31
      
      Sum + #DELTA
      *Block\Values[0] + ((*Block\Values[1] << 4) + *Key\Values[0]) ! (*Block\Values[1] + Sum) ! ((*Block\Values[1] >> 5) + *Key\Values[1])
      *Block\Values[1] + ((*Block\Values[0] << 4) + *Key\Values[2]) ! (*Block\Values[0] + Sum) ! ((*Block\Values[0] >> 5) + *Key\Values[3])
      
    Next
    
  EndProcedure
  
  Procedure Private_Decrypt(*Key.Key, *Block.Block)
    
    Sum.l = $C6EF3720
    
    For RoundID = 0 To 31
      
      *Block\Values[1] - ((*Block\Values[0] << 4) + *Key\Values[2]) ! (*Block\Values[0] + Sum) ! ((*Block\Values[0] >> 5) + *Key\Values[3])
      *Block\Values[0] - ((*Block\Values[1] << 4) + *Key\Values[0]) ! (*Block\Values[1] + Sum) ! ((*Block\Values[1] >> 5) + *Key\Values[1])
      Sum - #DELTA
      
    Next
    
  EndProcedure
  
  Procedure.s Encrypt(Key.s, Message.s)
    
    Protected Block.Block, Keys.Key
    
    Keys\Values[0] = Asc(Mid(Key, 1, 1))
    Keys\Values[1] = Asc(Mid(Key, 2, 1))
    Keys\Values[2] = Asc(Mid(Key, 3, 1))
    Keys\Values[3] = Asc(Mid(Key, 4, 1))
    
    *Ptr_Message.Chars = @Message 
    CharMax = Len(Message)
    
    CompilerIf #PB_Compiler_Unicode
      
      For CharID = 1 To CharMax Step 4
        
        Block\Values[0] = *Ptr_Message\Values[0] << 16 | *Ptr_Message\Values[1]
        Block\Values[1] = *Ptr_Message\Values[2] << 16 | *Ptr_Message\Values[3]
        *Ptr_Message + SizeOf(Chars)
        
        Private_Encrypt(Keys, Block)
        
        Output.s + Chr((Block\Values[0] >> 16) & $FFFF)
        Output.s + Chr(Block\Values[0] & $FFFF)
        Output.s + Chr((Block\Values[1] >> 16) & $FFFF)
        Output.s + Chr(Block\Values[1] & $FFFF)
        
      Next
      
    CompilerElse
      
      For CharID = 1 To CharMax Step 8
        
        Block\Values[0] = *Ptr_Message\Values[0] << 24 + *Ptr_Message\Values[1] << 16 + *Ptr_Message\Values[2] << 8 + *Ptr_Message\Values[3]
        Block\Values[1] = *Ptr_Message\Values[4] << 24 + *Ptr_Message\Values[5] << 16 + *Ptr_Message\Values[6] << 8 + *Ptr_Message\Values[7]
        *Ptr_Message + SizeOf(Chars)
        
        Private_Encrypt(Keys, Block)
        
        Output.s + Chr(Block\Values[0] >> 24 & $FF)
        Output.s + Chr(Block\Values[0] >> 16 & $FF)
        Output.s + Chr(Block\Values[0] >> 8 & $FF)
        Output.s + Chr(Block\Values[0] & $FF)
        Output.s + Chr(Block\Values[1] >> 24 & $FF)
        Output.s + Chr(Block\Values[1] >> 16 & $FF)
        Output.s + Chr(Block\Values[1] >> 8 & $FF)
        Output.s + Chr(Block\Values[1] & $FF)
        
      Next
      
    CompilerEndIf
    
    ProcedureReturn Output
  EndProcedure
  
  Procedure.s Decrypt(Key.s, Message.s)
    
    Protected Block.Block, Keys.Key
    
    Keys\Values[0] = Asc(Mid(Key, 1, 1))
    Keys\Values[1] = Asc(Mid(Key, 2, 1))
    Keys\Values[2] = Asc(Mid(Key, 3, 1))
    Keys\Values[3] = Asc(Mid(Key, 4, 1))
    
    *Ptr_Message.Chars = @Message 
    CharMax = Len(Message)
    
    CompilerIf #PB_Compiler_Unicode
      
      For CharID = 1 To CharMax Step 4
        
        Block\Values[0] = *Ptr_Message\Values[0] << 16 | *Ptr_Message\Values[1]
        Block\Values[1] = *Ptr_Message\Values[2] << 16 | *Ptr_Message\Values[3]
        *Ptr_Message + SizeOf(Chars)
        
        Private_Decrypt(Keys, Block)
        
        Output.s + Chr((Block\Values[0] >> 16) & $FFFF)
        Output.s + Chr(Block\Values[0] & $FFFF)
        Output.s + Chr((Block\Values[1] >> 16) & $FFFF)
        Output.s + Chr(Block\Values[1] & $FFFF)
        
      Next
      
    CompilerElse
      
      For CharID = 1 To CharMax Step 8
        
        Block\Values[0] = *Ptr_Message\Values[0] << 24 + *Ptr_Message\Values[1] << 16 + *Ptr_Message\Values[2] << 8 + *Ptr_Message\Values[3]
        Block\Values[1] = *Ptr_Message\Values[4] << 24 + *Ptr_Message\Values[5] << 16 + *Ptr_Message\Values[6] << 8 + *Ptr_Message\Values[7]
        *Ptr_Message + SizeOf(Chars)
        
        Private_Decrypt(Keys, Block)
        
        Output.s + Chr(Block\Values[0] >> 24 & $FF)
        Output.s + Chr(Block\Values[0] >> 16 & $FF)
        Output.s + Chr(Block\Values[0] >> 8 & $FF)
        Output.s + Chr(Block\Values[0] & $FF)
        Output.s + Chr(Block\Values[1] >> 24 & $FF)
        Output.s + Chr(Block\Values[1] >> 16 & $FF)
        Output.s + Chr(Block\Values[1] >> 8 & $FF)
        Output.s + Chr(Block\Values[1] & $FF)
        
      Next
      
    CompilerEndIf
    
    ProcedureReturn Output
  EndProcedure
  
EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  Encrypted.s = TEA::Encrypt("PURE", "PureBasic V5.31")
  Decrypted.s = TEA::Decrypt("PURE", Encrypted)
  ;   Debug Encrypted
  ;   Debug Decrypted
  MessageRequester("TEA Encrypt", Encrypted)
  MessageRequester("TEA Decrypt", Decrypted)
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
The Stone Age did not end due to a shortage of stones !
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Tiny Encryption Algorithm - Module

Post by StarBootics »

Hello again,

The eXtended Tiny Encryption Algorithm Module

have fun !

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : eXtended TinyEncryptionAlgorithm Module
; File Name : eXtended TinyEncryptionAlgorithm - Module.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 25-05-2015
; Last Update : 25-05-2015
; PureBasic code : V5.31
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule XTEA
  
  Declare.s Encrypt(Key.s, Message.s)
  Declare.s Decrypt(Key.s, Message.s)
  
EndDeclareModule

Module XTEA
  
  Structure Chars
    
    CompilerIf #PB_Compiler_Unicode
      Values.u[4]
    CompilerElse
      Values.a[8]
    CompilerEndIf   
    
  EndStructure
  
  Structure Key
    
    Values.l[4]
    
  EndStructure  
  
  Structure Block
    
    Values.l[2]
    
  EndStructure
  
  #DELTA = 2654435769
  
  Procedure Private_Encrypt(*Key.Key, *Block.Block)
    
    Sum.q = 0
    
    For RoundID = 0 To 31

      *Block\Values[0] + ((*Block\Values[1] << 4 ! *Block\Values[1] >> 5) + *Block\Values[1]) ! (Sum + *Key\Values[Sum & 3])
      Sum + #DELTA
      *Block\Values[1] + ((*Block\Values[0] << 4 ! *Block\Values[0] >> 5) + *Block\Values[0]) ! (Sum + *Key\Values[(Sum>>11) & 3])

    Next

  EndProcedure
  
  Procedure Private_Decrypt(*Key.Key, *Block.Block)
    
    Sum.q = $C6EF3720
    
    For RoundID = 0 To 31

      *Block\Values[1] - ((*Block\Values[0] << 4 ! *Block\Values[0] >> 5) + *Block\Values[0]) ! (Sum + *Key\Values[(Sum>>11) & 3])
      Sum = Sum - #DELTA
      *Block\Values[0] - ((*Block\Values[1] << 4 ! *Block\Values[1] >> 5) + *Block\Values[1]) ! (Sum + *Key\Values[Sum & 3])
      
    Next
 
  EndProcedure

  Procedure.s Encrypt(Key.s, Message.s)
    
    Protected Block.Block, Keys.Key
    
    Keys\Values[0] = Asc(Mid(Key, 1, 1))
    Keys\Values[1] = Asc(Mid(Key, 2, 1))
    Keys\Values[2] = Asc(Mid(Key, 3, 1))
    Keys\Values[3] = Asc(Mid(Key, 4, 1))
    
    *Ptr_Message.Chars = @Message 
    CharMax = Len(Message)
    
    CompilerIf #PB_Compiler_Unicode
      
      For CharID = 1 To CharMax Step 4
        
        Block\Values[0] = *Ptr_Message\Values[0] << 16 | *Ptr_Message\Values[1]
        Block\Values[1] = *Ptr_Message\Values[2] << 16 | *Ptr_Message\Values[3]
        *Ptr_Message + SizeOf(Chars)

        Private_Encrypt(Keys, Block)

        Output.s + Chr((Block\Values[0] >> 16) & $FFFF)
        Output.s + Chr(Block\Values[0] & $FFFF)
        Output.s + Chr((Block\Values[1] >> 16) & $FFFF)
        Output.s + Chr(Block\Values[1] & $FFFF)
        
      Next
      
    CompilerElse
      
      For CharID = 1 To CharMax Step 8
        
        Block\Values[0] = *Ptr_Message\Values[0] << 24 + *Ptr_Message\Values[1] << 16 + *Ptr_Message\Values[2] << 8 + *Ptr_Message\Values[3]
        Block\Values[1] = *Ptr_Message\Values[4] << 24 + *Ptr_Message\Values[5] << 16 + *Ptr_Message\Values[6] << 8 + *Ptr_Message\Values[7]
        *Ptr_Message + SizeOf(Chars)

        Private_Encrypt(Keys, Block)
        
        Output.s + Chr(Block\Values[0] >> 24 & $FF)
        Output.s + Chr(Block\Values[0] >> 16 & $FF)
        Output.s + Chr(Block\Values[0] >> 8 & $FF)
        Output.s + Chr(Block\Values[0] & $FF)
        Output.s + Chr(Block\Values[1] >> 24 & $FF)
        Output.s + Chr(Block\Values[1] >> 16 & $FF)
        Output.s + Chr(Block\Values[1] >> 8 & $FF)
        Output.s + Chr(Block\Values[1] & $FF)
        
      Next
      
    CompilerEndIf
    
    ProcedureReturn Output
  EndProcedure
  
  Procedure.s Decrypt(Key.s, Message.s)
    
    Protected Block.Block, Keys.Key
    
    Keys\Values[0] = Asc(Mid(Key, 1, 1))
    Keys\Values[1] = Asc(Mid(Key, 2, 1))
    Keys\Values[2] = Asc(Mid(Key, 3, 1))
    Keys\Values[3] = Asc(Mid(Key, 4, 1))
    
    *Ptr_Message.Chars = @Message 
    CharMax = Len(Message)
    
    CompilerIf #PB_Compiler_Unicode
      
      For CharID = 1 To CharMax Step 4
        
        Block\Values[0] = *Ptr_Message\Values[0] << 16 | *Ptr_Message\Values[1]
        Block\Values[1] = *Ptr_Message\Values[2] << 16 | *Ptr_Message\Values[3]
        *Ptr_Message + SizeOf(Chars)
        
        Private_Decrypt(Keys, Block)

        Output.s + Chr((Block\Values[0] >> 16) & $FFFF)
        Output.s + Chr(Block\Values[0] & $FFFF)
        Output.s + Chr((Block\Values[1] >> 16) & $FFFF)
        Output.s + Chr(Block\Values[1] & $FFFF)
        
      Next

    CompilerElse
      
      For CharID = 1 To CharMax Step 8
        
        Block\Values[0] = *Ptr_Message\Values[0] << 24 + *Ptr_Message\Values[1] << 16 + *Ptr_Message\Values[2] << 8 + *Ptr_Message\Values[3]
        Block\Values[1] = *Ptr_Message\Values[4] << 24 + *Ptr_Message\Values[5] << 16 + *Ptr_Message\Values[6] << 8 + *Ptr_Message\Values[7]
        *Ptr_Message + SizeOf(Chars)
        
        Private_Decrypt(Keys, Block)
        
        Output.s + Chr(Block\Values[0] >> 24 & $FF)
        Output.s + Chr(Block\Values[0] >> 16 & $FF)
        Output.s + Chr(Block\Values[0] >> 8 & $FF)
        Output.s + Chr(Block\Values[0] & $FF)
        Output.s + Chr(Block\Values[1] >> 24 & $FF)
        Output.s + Chr(Block\Values[1] >> 16 & $FF)
        Output.s + Chr(Block\Values[1] >> 8 & $FF)
        Output.s + Chr(Block\Values[1] & $FF)
        
      Next
      
    CompilerEndIf
    
    ProcedureReturn Output
  EndProcedure
  
EndModule

CompilerIf #PB_Compiler_IsMainFile
	
  Encrypted.s = XTEA::Encrypt("PURE", "PureBasic V5.31")
  Decrypted.s = XTEA::Decrypt("PURE", Encrypted)
  Debug Encrypted
  Debug Decrypted
  MessageRequester("TEA Encrypt", Encrypted)
  MessageRequester("TEA Decrypt", Decrypted)
	
CompilerEndIf
; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
The Stone Age did not end due to a shortage of stones !
kvitaliy
Enthusiast
Enthusiast
Posts: 162
Joined: Mon May 10, 2010 4:02 pm

Re: Tiny Encryption Algorithm - Module

Post by kvitaliy »

TinyEncryptionAlgorithm - Ok!

eXtended TinyEncryptionAlgorithm - Error in line 52 ( PureBasic V5.31, x86 winXP SP3)
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Tiny Encryption Algorithm - Module

Post by StarBootics »

kvitaliy wrote:TinyEncryptionAlgorithm - Ok!

eXtended TinyEncryptionAlgorithm - Error in line 52 ( PureBasic V5.31, x86 winXP SP3)
:shock: I have double check with my original code and the one above and both work just fine. Is this problem also happen to others ?

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Tiny Encryption Algorithm - Module

Post by wilbert »

StarBootics wrote: :shock: I have double check with my original code and the one above and both work just fine. Is this problem also happen to others ?
Seems to work fine here on OSX.
Windows (x64)
Raspberry Pi OS (Arm64)
kvitaliy
Enthusiast
Enthusiast
Posts: 162
Joined: Mon May 10, 2010 4:02 pm

Re: Tiny Encryption Algorithm - Module

Post by kvitaliy »

StarBootics wrote:
:shock: I have double check with my original code and the one above and both work just fine.
Image
juror
Enthusiast
Enthusiast
Posts: 232
Joined: Mon Jul 09, 2007 4:47 pm
Location: Courthouse

Re: Tiny Encryption Algorithm - Module

Post by juror »

Line 52 - Invalid memory access

PB 5.31 x86
win 10 10074
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Tiny Encryption Algorithm - Module

Post by Tenaja »

There have been a few TEA and XTEA versions posted already, but I have saved yours as well, due to the Unicode option.

This one is set up for files:
http://www.purebasic.fr/english/viewtopic.php?t=37020


...and the x86 can probably be sped up by implementing this:
http://www.nayuki.io/page/tiny-encrypti ... 6-assembly
(And maybe even more after it gets Wilburized!)
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Tiny Encryption Algorithm - Module

Post by StarBootics »

kvitaliy wrote:
StarBootics wrote:
:shock: I have double check with my original code and the one above and both work just fine.
Image
What happen when you compile the code with Unicode disabled ?
The Stone Age did not end due to a shortage of stones !
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Tiny Encryption Algorithm - Module

Post by wilbert »

Tenaja wrote:...and the x86 can probably be sped up by implementing this:
http://www.nayuki.io/page/tiny-encrypti ... 6-assembly
(And maybe even more after it gets Wilburized!)
Too much credit Tenaja :wink:
It actually already is a very optimized asm routine.
I see only room for further optimization if extra registers are available (x86 with MMX/SSE or x64).
StarBootics wrote:What happen when you compile the code with Unicode disabled ?
The problem only seems to occur on x86, not on x64 and disappears if sum is changed from a quad to a long.
The only thing is if the results of both routines are compatible with other implementations in this case. A PureBasic right shift (>>) is always an arithmetic shift.
This is a problem because the specification of the algorithm mentions unsigned longs instead of signed ones.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Tiny Encryption Algorithm - Module

Post by StarBootics »

wilbert wrote:The problem only seems to occur on x86, not on x64 and disappears if sum is changed from a quad to a long.
The only thing is if the results of both routines are compatible with other implementations in this case. A PureBasic right shift (>>) is always an arithmetic shift.
This is a problem because the specification of the algorithm mentions unsigned longs instead of signed ones.
It's the main reason why I have use quad instead of long, the #Delta is 2654435769 and the maximum value for a signed long is 2147483647 :!: :?
I didn't try with a different value for the #DELTA.
But Quad are not supported on x86 computers ?

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Tiny Encryption Algorithm - Module

Post by wilbert »

StarBootics wrote:But Quad are not supported on x86 computers ?
Quads are supported on x86.
I'm not exactly sure what is causing the problem in this case; you would have to look at the assembler source PureBasic generates.
I do know right shifts are a problem. If one of your *Block\Values has the most significant bit set, result are different from the original algorithm.
For example a long $C6EF3720 as an unsigned value shifted 5 bits to the right is $63779B9 while the PB shift operation >> 5 results in $FE3779B9.

There's a few problems with your code.

The loop ...

Code: Select all

For CharID = 1 To CharMax Step 4
Assuming CharMax = 5 and unicode is off, it will loop twice.
This means it will encrypt 8 characters while there are only 5 available.

The returning of a string.
You can't return a string. There's the possibility that one of the encrypted bytes is a 0 and that will terminate the string resulting in data loss.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Tiny Encryption Algorithm - Module

Post by StarBootics »

wilbert wrote:There's a few problems with your code.

The loop ...

Code: Select all

For CharID = 1 To CharMax Step 4
Assuming CharMax = 5 and unicode is off, it will loop twice.
This means it will encrypt 8 characters while there are only 5 available.
How can I complete fixed data block size required by the algorithm if I don't set them to 0 in this case ?
Anyway, so far I didn't lost any information yet. But I have discovered if the strings came from linked list some time I have ended up with extra character at the end of the original strings. And the only way to avoid this is to extract character one by one using Asc(Mid("String", 0, 1)) which is not really fast.
wilbert wrote:The returning of a string.
You can't return a string. There's the possibility that one of the encrypted bytes is a 0 and that will terminate the string resulting in data loss.
Possible but the main reason I have work on this TEA module was to create something like this :

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Read Write TEA - Module
; File Name : Read Write TEA - Module.pb
; File version: 1.1.0
; Programming : OK
; Programmed by : StarBootics
; Date : 24-05-2015
; Last Update : 25-05-2015
; PureBasic code : V5.31
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule TEA
  
  Declare TEA_WriteString(FileID.l, P_Key.s, String.s)
  Declare TEA_WriteByte(FileID.l, P_Key.s, Value.b)
  Declare TEA_WriteAscii(FileID.l, P_Key.s, Value.a)
  Declare TEA_WriteCharacter(FileID.l, P_Key.s, Value.c)
  Declare TEA_WriteUnicode(FileID.l, P_Key.s, Value.u)
  Declare TEA_WriteWord(FileID.l, P_Key.s, Value.w)
  Declare TEA_WriteLong(FileID.l, P_Key.s, Value.l)
  Declare TEA_WriteInteger(FileID.l, P_Key.s, Value.i)
  Declare TEA_WriteQuad(FileID.l, P_Key.s, Value.q)
  Declare TEA_WriteFloat(FileID.l, P_Key.s, Value.f)
  Declare TEA_WriteDouble(FileID.l, P_Key.s, Value.d)
  
  Declare.s TEA_ReadString(FileID.l, P_Key.s)
  Declare.b TEA_ReadByte(FileID.l, P_Key.s)
  Declare.a TEA_ReadAscii(FileID.l, P_Key.s)
  Declare.c TEA_ReadCharacter(FileID.l, P_Key.s)
  Declare.u TEA_ReadUnicode(FileID.l, P_Key.s)
  Declare.w TEA_ReadWord(FileID.l, P_Key.s)
  Declare.l TEA_ReadLong(FileID.l, P_Key.s)
  Declare.i TEA_ReadInteger(FileID.l, P_Key.s)
  Declare.q TEA_ReadQuad(FileID.l, P_Key.s)
  Declare.f TEA_ReadFloat(FileID.l, P_Key.s)
  Declare.d TEA_ReadDouble(FileID.l, P_Key.s)
  
EndDeclareModule

Module TEA
    
  Structure Key
    
    Values.l[4]
    
  EndStructure  
  
  Structure Block
    
    Values.l[2]
    
  EndStructure
  
  #DELTA = 2654435769
  
  Procedure Private_Encrypt(*Key.Key, *Block.Block)
    
    Sum.l = 0
    
    For RoundID = 0 To 31
      
      Sum + #DELTA
      *Block\Values[0] + ((*Block\Values[1] << 4) + *Key\Values[0]) ! (*Block\Values[1] + Sum) ! ((*Block\Values[1] >> 5) + *Key\Values[1])
      *Block\Values[1] + ((*Block\Values[0] << 4) + *Key\Values[2]) ! (*Block\Values[0] + Sum) ! ((*Block\Values[0] >> 5) + *Key\Values[3])
      
    Next
    
  EndProcedure
  
  Procedure Private_Decrypt(*Key.Key, *Block.Block)
    
    Sum.l = $C6EF3720
    
    For RoundID = 0 To 31
      
      *Block\Values[1] - ((*Block\Values[0] << 4) + *Key\Values[2]) ! (*Block\Values[0] + Sum) ! ((*Block\Values[0] >> 5) + *Key\Values[3])
      *Block\Values[0] - ((*Block\Values[1] << 4) + *Key\Values[0]) ! (*Block\Values[1] + Sum) ! ((*Block\Values[1] >> 5) + *Key\Values[1])
      Sum - #DELTA
      
    Next
    
  EndProcedure
  
  Procedure TEA_WriteString(FileID.l, P_Key.s, String.s)
    
    Protected Block.Block, Keys.Key
    
    Keys\Values[0] = Asc(Mid(P_Key, 1, 1))
    Keys\Values[1] = Asc(Mid(P_Key, 2, 1))
    Keys\Values[2] = Asc(Mid(P_Key, 3, 1))
    Keys\Values[3] = Asc(Mid(P_Key, 4, 1))
    
    CharMax = Len(String)
    
    WriteLong(FileID, CharMax)
    
    CompilerIf #PB_Compiler_Unicode
      
      For CharID = 1 To CharMax Step 4
        
        Block\Values[0] = Asc(Mid(String, CharID + 0, 1)) << 16 | Asc(Mid(String, CharID + 1, 1))
        Block\Values[1] = Asc(Mid(String, CharID + 2, 1)) << 16 | Asc(Mid(String, CharID + 3, 1))
        
        Private_Encrypt(Keys, Block)
        WriteLong(FileID, Block\Values[0])
        WriteLong(FileID, Block\Values[1])
        
      Next
      
    CompilerElse
      
      For CharID = 1 To CharMax Step 8
        
        Block\Values[0] = Asc(Mid(String, CharID + 0, 1)) << 24 + Asc(Mid(String, CharID + 1, 1)) << 16 + Asc(Mid(String, CharID + 2, 1)) << 8 + Asc(Mid(String, CharID + 3, 1))
        Block\Values[1] = Asc(Mid(String, CharID + 4, 1)) << 24 + Asc(Mid(String, CharID + 5, 1)) << 16 + Asc(Mid(String, CharID + 6, 1)) << 8 + Asc(Mid(String, CharID + 7, 1))
 
        Private_Encrypt(Keys, Block)
        WriteLong(FileID, Block\Values[0])
        WriteLong(FileID, Block\Values[1])
        
      Next
      
    CompilerEndIf
    
  EndProcedure
  
  Procedure TEA_WriteByte(FileID.l, P_Key.s, P_Value.b)
    
    If P_Value < 0
      Sign.s = "-"
      P_Value = P_Value * -1
    EndIf
    
    TEA_WriteString(FileID, P_Key, Sign + RSet(Str(P_Value), 6, "0"))
    
  EndProcedure
  
  Procedure TEA_WriteAscii(FileID.l, P_Key.s, P_Value.a)
    
    TEA_WriteString(FileID, P_Key, RSet(Str(P_Value), 6, "0"))
    
  EndProcedure
  
  Procedure TEA_WriteWord(FileID.l, P_Key.s, P_Value.w)
    
    If P_Value < 0
      Sign.s = "-"
      P_Value = P_Value * -1
    EndIf
    
    TEA_WriteString(FileID, P_Key, Sign + RSet(Str(P_Value), 10, "0"))
    
  EndProcedure
  
  Procedure TEA_WriteCharacter(FileID.l, P_Key.s, P_Value.c)
    
    TEA_WriteString(FileID, P_Key, RSet(Str(P_Value), 10, "0"))
    
  EndProcedure
  
  Procedure TEA_WriteUnicode(FileID.l, P_Key.s, P_Value.u)
    
    TEA_WriteString(FileID, P_Key, RSet(Str(P_Value), 10, "0"))
    
  EndProcedure
  
  Procedure TEA_WriteLong(FileID.l, P_Key.s, P_Value.l)
    
    If P_Value < 0
      Sign.s = "-"
      P_Value = P_Value * -1
    EndIf
    
    TEA_WriteString(FileID, P_Key, Sign + RSet(Str(P_Value), 22, "0"))
    
  EndProcedure
  
  Procedure TEA_WriteQuad(FileID.l, P_Key.s, P_Value.q)
    
    If P_Value < 0
      Sign.s = "-"
      P_Value = P_Value * -1
    EndIf
    
    TEA_WriteString(FileID, P_Key, Sign + RSet(Str(P_Value), 42, "0"))
    
  EndProcedure
  
  Procedure TEA_WriteInteger(FileID.l, P_Key.s, P_Value.i)
    
    If P_Value < 0
      Sign.s = "-"
      P_Value = P_Value * -1
    EndIf
    
    TEA_WriteString(FileID, P_Key, Sign + RSet(Str(P_Value), 42, "0"))
    
  EndProcedure
  
  Procedure TEA_WriteFloat(FileID.l, P_Key.s, P_Value.f)
    
    TEA_WriteString(FileID, P_Key, StrF(P_Value, 14))
    
  EndProcedure
  
  Procedure TEA_WriteDouble(FileID.l, P_Key.s, P_Value.d)
    
    TEA_WriteString(FileID, P_Key, StrD(P_Value, 25))
    
  EndProcedure
  
  Procedure.s TEA_ReadString(FileID.l, P_Key.s)
    
    Protected Block.Block, Keys.Key
    
    Keys\Values[0] = Asc(Mid(P_Key, 1, 1))
    Keys\Values[1] = Asc(Mid(P_Key, 2, 1))
    Keys\Values[2] = Asc(Mid(P_Key, 3, 1))
    Keys\Values[3] = Asc(Mid(P_Key, 4, 1))
    
    CharMax = ReadLong(FileID)
    
    CompilerIf #PB_Compiler_Unicode
      
      For CharID = 1 To CharMax Step 4
        
        Block\Values[0] = ReadLong(FileID)
        Block\Values[1] = ReadLong(FileID)
        
        Private_Decrypt(Keys, Block)
        
        Output.s + Chr((Block\Values[0] >> 16) & $FFFF)
        Output.s + Chr(Block\Values[0] & $FFFF)
        Output.s + Chr((Block\Values[1] >> 16) & $FFFF)
        Output.s + Chr(Block\Values[1] & $FFFF)
        
      Next
      
    CompilerElse
      
      For CharID = 1 To CharMax Step 8
        
        Block\Values[0] = ReadLong(FileID)
        Block\Values[1] = ReadLong(FileID)
        
        Private_Decrypt(Keys, Block)
        
        Output.s + Chr(Block\Values[0] >> 24 & $FF)
        Output.s + Chr(Block\Values[0] >> 16 & $FF)
        Output.s + Chr(Block\Values[0] >> 8 & $FF)
        Output.s + Chr(Block\Values[0] & $FF)
        Output.s + Chr(Block\Values[1] >> 24 & $FF)
        Output.s + Chr(Block\Values[1] >> 16 & $FF)
        Output.s + Chr(Block\Values[1] >> 8 & $FF)
        Output.s + Chr(Block\Values[1] & $FF)
        
      Next
      
    CompilerEndIf
    
    ProcedureReturn Output
  EndProcedure
  
  Procedure.b TEA_ReadByte(FileID.l, P_Key.s)
    
    ProcedureReturn Val(TEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.a TEA_ReadAscii(FileID.l, P_Key.s)
    
    ProcedureReturn Val(TEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.w TEA_ReadWord(FileID.l, P_Key.s)
    
    ProcedureReturn Val(TEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.c TEA_ReadCharacter(FileID.l, P_Key.s)
    
    ProcedureReturn Val(TEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.u TEA_ReadUnicode(FileID.l, P_Key.s)
    
    ProcedureReturn Val(TEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.l TEA_ReadLong(FileID.l, P_Key.s)
    
    ProcedureReturn Val(TEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.q TEA_ReadQuad(FileID.l, P_Key.s)
    
    ProcedureReturn Val(TEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.i TEA_ReadInteger(FileID.l, P_Key.s)
    
    ProcedureReturn Val(TEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.f TEA_ReadFloat(FileID.l, P_Key.s)
    
    ProcedureReturn ValF(TEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.d TEA_ReadDouble(FileID.l, P_Key.s)
    
    ProcedureReturn ValD(TEA_ReadString(FileID, P_Key))
  EndProcedure
  
EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  Macro AddElementEx(ListName, Element)
    AddElement(ListName)
    ListName = Element
  EndMacro
  
  NewList Strings.s()
  
  AddElementEx(Strings(),"PureBasic V5.31")
  AddElementEx(Strings(), "#PB_Compiler_IsMainFile")
  AddElementEx(Strings(), "AddElementEx(ListName, Element)")
  AddElementEx(Strings(), "ListName = Element")
  
  Key.s = "PURE"
  
  Varw.w = 32760
  Varl.l = 2147483645
  Varq.q = 9223372036854775800
  Varf.f = 2 * #PI
  Vard.d = 3 * #PI

  Debug "; <<<<<<<<<<<<<<<<<<<<<<<<<<<"
  Debug "The Original"
  Debug ""
  Debug Varw
  Debug Varl
  Debug Varq
  Debug Varf
  Debug Vard
  
  If CreateFile(0, "Test.tea")
    
    TEA::TEA_WriteWord(0, Key, Varw) 
    TEA::TEA_WriteLong(0, Key, Varl)  
    TEA::TEA_WriteQuad(0, Key, Varq)
    TEA::TEA_WriteFloat(0, Key, Varf)
    TEA::TEA_WriteDouble(0, Key, Vard)

    TEA::TEA_WriteLong(0,Key, ListSize(Strings()))
    
    ForEach Strings()
      Debug Strings()
      TEA::TEA_WriteString(0, Key, Strings())
    Next
    
    CloseFile(0)
    
  EndIf
  
  If ReadFile(1, "Test.tea")
    
    Debug ""
    Debug "; <<<<<<<<<<<<<<<<<<<<<<<<<<<"
    Debug "From file"
    Debug ""
    
    Debug TEA::TEA_ReadWord(1, Key)
    Debug TEA::TEA_ReadLong(1, Key)
    Debug TEA::TEA_ReadQuad(1, Key)
    Debug TEA::TEA_ReadFloat(1, Key)
    Debug TEA::TEA_ReadDouble(1, Key)

    Max = TEA::TEA_ReadLong(1, Key) - 1
    
    For Index = 0 To Max
      Debug TEA::TEA_ReadString(1, Key)
    Next
    
    CloseFile(1)
    
  EndIf
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
If you check carefully the TEA_WriteString(FileID.l, P_Key.s, String.s) procedure, I write the two long encrypted values to the file directly and use TEA_ReadString(FileID.l, P_Key.s, String.s) to read encrypted values from the file.
I think this is safe because there is no conversion back to string after encryption.

For those who would like to use XTEA version there it is :

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Read Write XTEA - Module
; File Name : Read Write XTEA - Module.pb
; File version: 1.1.0
; Programming : OK
; Programmed by : StarBootics
; Date : 24-05-2015
; Last Update : 25-05-2015
; PureBasic code : V5.31
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

DeclareModule XTEA
  
  Declare XTEA_WriteString(FileID.l, P_Key.s, String.s)
  Declare XTEA_WriteByte(FileID.l, P_Key.s, Value.b)
  Declare XTEA_WriXTEAscii(FileID.l, P_Key.s, Value.a)
  Declare XTEA_WriteCharacter(FileID.l, P_Key.s, Value.c)
  Declare XTEA_WriteUnicode(FileID.l, P_Key.s, Value.u)
  Declare XTEA_WriteWord(FileID.l, P_Key.s, Value.w)
  Declare XTEA_WriteLong(FileID.l, P_Key.s, Value.l)
  Declare XTEA_WriteInteger(FileID.l, P_Key.s, Value.i)
  Declare XTEA_WriteQuad(FileID.l, P_Key.s, Value.q)
  Declare XTEA_WriteFloat(FileID.l, P_Key.s, Value.f)
  Declare XTEA_WriteDouble(FileID.l, P_Key.s, Value.d)
  
  Declare.s XTEA_ReadString(FileID.l, P_Key.s)
  Declare.b XTEA_ReadByte(FileID.l, P_Key.s)
  Declare.a XTEA_ReadAscii(FileID.l, P_Key.s)
  Declare.c XTEA_ReadCharacter(FileID.l, P_Key.s)
  Declare.u XTEA_ReadUnicode(FileID.l, P_Key.s)
  Declare.w XTEA_ReadWord(FileID.l, P_Key.s)
  Declare.l XTEA_ReadLong(FileID.l, P_Key.s)
  Declare.i XTEA_ReadInteger(FileID.l, P_Key.s)
  Declare.q XTEA_ReadQuad(FileID.l, P_Key.s)
  Declare.f XTEA_ReadFloat(FileID.l, P_Key.s)
  Declare.d XTEA_ReadDouble(FileID.l, P_Key.s)
  
EndDeclareModule

Module XTEA
  
  Structure Key
    
    Values.l[4]
    
  EndStructure  
  
  Structure Block
    
    Values.l[2]
    
  EndStructure
  
  #DELTA = 2654435769
  
  Procedure Private_Encrypt(*Key.Key, *Block.Block)
    
    Sum.q = 0
    
    For RoundID = 0 To 31

      *Block\Values[0] + ((*Block\Values[1] << 4 ! *Block\Values[1] >> 5) + *Block\Values[1]) ! (Sum + *Key\Values[Sum & 3])
      Sum + #DELTA
      *Block\Values[1] + ((*Block\Values[0] << 4 ! *Block\Values[0] >> 5) + *Block\Values[0]) ! (Sum + *Key\Values[(Sum>>11) & 3])

    Next

  EndProcedure
  
  Procedure Private_Decrypt(*Key.Key, *Block.Block)
    
    Sum.q = $C6EF3720
    
    For RoundID = 0 To 31

      *Block\Values[1] - ((*Block\Values[0] << 4 ! *Block\Values[0] >> 5) + *Block\Values[0]) ! (Sum + *Key\Values[(Sum>>11) & 3])
      Sum = Sum - #DELTA
      *Block\Values[0] - ((*Block\Values[1] << 4 ! *Block\Values[1] >> 5) + *Block\Values[1]) ! (Sum + *Key\Values[Sum & 3])
      
    Next
 
  EndProcedure
  
  Procedure XTEA_WriteString(FileID.l, P_Key.s, String.s)
    
    Protected Block.Block, Keys.Key
    
    Keys\Values[0] = Asc(Mid(P_Key, 1, 1))
    Keys\Values[1] = Asc(Mid(P_Key, 2, 1))
    Keys\Values[2] = Asc(Mid(P_Key, 3, 1))
    Keys\Values[3] = Asc(Mid(P_Key, 4, 1))
    
    CharMax = Len(String)
    
    WriteLong(FileID, CharMax)
    
    CompilerIf #PB_Compiler_Unicode
      
      For CharID = 1 To CharMax Step 4
        
        Block\Values[0] = Asc(Mid(String, CharID + 0, 1)) << 16 | Asc(Mid(String, CharID + 1, 1))
        Block\Values[1] = Asc(Mid(String, CharID + 2, 1)) << 16 | Asc(Mid(String, CharID + 3, 1))
        
        Private_Encrypt(Keys, Block)
        WriteLong(FileID, Block\Values[0])
        WriteLong(FileID, Block\Values[1])
        
      Next
      
    CompilerElse
      
      For CharID = 1 To CharMax Step 8
        
        Block\Values[0] = Asc(Mid(String, CharID + 0, 1)) << 24 + Asc(Mid(String, CharID + 1, 1)) << 16 + Asc(Mid(String, CharID + 2, 1)) << 8 + Asc(Mid(String, CharID + 3, 1))
        Block\Values[1] = Asc(Mid(String, CharID + 4, 1)) << 24 + Asc(Mid(String, CharID + 5, 1)) << 16 + Asc(Mid(String, CharID + 6, 1)) << 8 + Asc(Mid(String, CharID + 7, 1))
 
        Private_Encrypt(Keys, Block)
        WriteLong(FileID, Block\Values[0])
        WriteLong(FileID, Block\Values[1])
        
      Next
      
    CompilerEndIf
    
  EndProcedure
  
  Procedure XTEA_WriteByte(FileID.l, P_Key.s, P_Value.b)
    
    If P_Value < 0
      Sign.s = "-"
      P_Value = P_Value * -1
    EndIf
    
    XTEA_WriteString(FileID, P_Key, Sign + RSet(Str(P_Value), 6, "0"))
    
  EndProcedure
  
  Procedure XTEA_WriXTEAscii(FileID.l, P_Key.s, P_Value.a)
    
    XTEA_WriteString(FileID, P_Key, RSet(Str(P_Value), 6, "0"))
    
  EndProcedure
  
  Procedure XTEA_WriteWord(FileID.l, P_Key.s, P_Value.w)
    
    If P_Value < 0
      Sign.s = "-"
      P_Value = P_Value * -1
    EndIf
    
    XTEA_WriteString(FileID, P_Key, Sign + RSet(Str(P_Value), 10, "0"))
    
  EndProcedure
  
  Procedure XTEA_WriteCharacter(FileID.l, P_Key.s, P_Value.c)
    
    XTEA_WriteString(FileID, P_Key, RSet(Str(P_Value), 10, "0"))
    
  EndProcedure
  
  Procedure XTEA_WriteUnicode(FileID.l, P_Key.s, P_Value.u)
    
    XTEA_WriteString(FileID, P_Key, RSet(Str(P_Value), 10, "0"))
    
  EndProcedure
  
  Procedure XTEA_WriteLong(FileID.l, P_Key.s, P_Value.l)
    
    If P_Value < 0
      Sign.s = "-"
      P_Value = P_Value * -1
    EndIf
    
    XTEA_WriteString(FileID, P_Key, Sign + RSet(Str(P_Value), 22, "0"))
    
  EndProcedure
  
  Procedure XTEA_WriteQuad(FileID.l, P_Key.s, P_Value.q)
    
    If P_Value < 0
      Sign.s = "-"
      P_Value = P_Value * -1
    EndIf
    
    XTEA_WriteString(FileID, P_Key, Sign + RSet(Str(P_Value), 42, "0"))
    
  EndProcedure
  
  Procedure XTEA_WriteInteger(FileID.l, P_Key.s, P_Value.i)
    
    If P_Value < 0
      Sign.s = "-"
      P_Value = P_Value * -1
    EndIf
    
    XTEA_WriteString(FileID, P_Key, Sign + RSet(Str(P_Value), 42, "0"))
    
  EndProcedure
  
  Procedure XTEA_WriteFloat(FileID.l, P_Key.s, P_Value.f)
    
    XTEA_WriteString(FileID, P_Key, StrF(P_Value, 14))
    
  EndProcedure
  
  Procedure XTEA_WriteDouble(FileID.l, P_Key.s, P_Value.d)
    
    XTEA_WriteString(FileID, P_Key, StrD(P_Value, 25))
    
  EndProcedure
  
  Procedure.s XTEA_ReadString(FileID.l, P_Key.s)
    
    Protected Block.Block, Keys.Key
    
    Keys\Values[0] = Asc(Mid(P_Key, 1, 1))
    Keys\Values[1] = Asc(Mid(P_Key, 2, 1))
    Keys\Values[2] = Asc(Mid(P_Key, 3, 1))
    Keys\Values[3] = Asc(Mid(P_Key, 4, 1))
    
    CharMax = ReadLong(FileID)
    
    CompilerIf #PB_Compiler_Unicode
      
      For CharID = 1 To CharMax Step 4
        
        Block\Values[0] = ReadLong(FileID)
        Block\Values[1] = ReadLong(FileID)
        
        Private_Decrypt(Keys, Block)
        
        Output.s + Chr((Block\Values[0] >> 16) & $FFFF)
        Output.s + Chr(Block\Values[0] & $FFFF)
        Output.s + Chr((Block\Values[1] >> 16) & $FFFF)
        Output.s + Chr(Block\Values[1] & $FFFF)
        
      Next
      
    CompilerElse
      
      For CharID = 1 To CharMax Step 8
        
        Block\Values[0] = ReadLong(FileID)
        Block\Values[1] = ReadLong(FileID)
        
        Private_Decrypt(Keys, Block)
        
        Output.s + Chr(Block\Values[0] >> 24 & $FF)
        Output.s + Chr(Block\Values[0] >> 16 & $FF)
        Output.s + Chr(Block\Values[0] >> 8 & $FF)
        Output.s + Chr(Block\Values[0] & $FF)
        Output.s + Chr(Block\Values[1] >> 24 & $FF)
        Output.s + Chr(Block\Values[1] >> 16 & $FF)
        Output.s + Chr(Block\Values[1] >> 8 & $FF)
        Output.s + Chr(Block\Values[1] & $FF)
        
      Next
      
    CompilerEndIf
    
    ProcedureReturn Output
  EndProcedure
  
  Procedure.b XTEA_ReadByte(FileID.l, P_Key.s)
    
    ProcedureReturn Val(XTEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.a XTEA_ReadAscii(FileID.l, P_Key.s)
    
    ProcedureReturn Val(XTEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.w XTEA_ReadWord(FileID.l, P_Key.s)
    
    ProcedureReturn Val(XTEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.c XTEA_ReadCharacter(FileID.l, P_Key.s)
    
    ProcedureReturn Val(XTEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.u XTEA_ReadUnicode(FileID.l, P_Key.s)
    
    ProcedureReturn Val(XTEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.l XTEA_ReadLong(FileID.l, P_Key.s)
    
    ProcedureReturn Val(XTEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.q XTEA_ReadQuad(FileID.l, P_Key.s)
    
    ProcedureReturn Val(XTEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.i XTEA_ReadInteger(FileID.l, P_Key.s)
    
    ProcedureReturn Val(XTEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.f XTEA_ReadFloat(FileID.l, P_Key.s)
    
    ProcedureReturn ValF(XTEA_ReadString(FileID, P_Key))
  EndProcedure
  
  Procedure.d XTEA_ReadDouble(FileID.l, P_Key.s)
    
    ProcedureReturn ValD(XTEA_ReadString(FileID, P_Key))
  EndProcedure
  
EndModule

CompilerIf #PB_Compiler_IsMainFile
  
  Macro AddElementEx(ListName, Element)
    AddElement(ListName)
    ListName = Element
  EndMacro
  
  NewList Strings.s()
  
  AddElementEx(Strings(),"PureBasic V5.31")
  AddElementEx(Strings(), "#PB_Compiler_IsMainFile")
  AddElementEx(Strings(), "AddElementEx(ListName, Element)")
  AddElementEx(Strings(), "ListName = Element")
  
  Key.s = "PURE"
  
  Varw.w = 32760
  Varl.l = 2147483645
  Varq.q = 9223372036854775800
  Varf.f = 2 * #PI
  Vard.d = 3 * #PI

  Debug "; <<<<<<<<<<<<<<<<<<<<<<<<<<<"
  Debug "The Original"
  Debug ""
  Debug Varw
  Debug Varl
  Debug Varq
  Debug Varf
  Debug Vard
  
  If CreateFile(0, "Test.tea")
    
    XTEA::XTEA_WriteWord(0, Key, Varw) 
    XTEA::XTEA_WriteLong(0, Key, Varl)  
    XTEA::XTEA_WriteQuad(0, Key, Varq)
    XTEA::XTEA_WriteFloat(0, Key, Varf)
    XTEA::XTEA_WriteDouble(0, Key, Vard)

    XTEA::XTEA_WriteLong(0,Key, ListSize(Strings()))
    
    ForEach Strings()
      Debug Strings()
      XTEA::XTEA_WriteString(0, Key, Strings())
    Next
    
    CloseFile(0)
    
  EndIf
  
  If ReadFile(1, "Test.tea")
    
    Debug ""
    Debug "; <<<<<<<<<<<<<<<<<<<<<<<<<<<"
    Debug "From file"
    Debug ""
    
    Debug XTEA::XTEA_ReadWord(1, Key)
    Debug XTEA::XTEA_ReadLong(1, Key)
    Debug XTEA::XTEA_ReadQuad(1, Key)
    Debug XTEA::XTEA_ReadFloat(1, Key)
    Debug XTEA::XTEA_ReadDouble(1, Key)

    Max = XTEA::XTEA_ReadLong(1, Key) - 1
    
    For Index = 0 To Max
      Debug XTEA::XTEA_ReadString(1, Key)
    Next
    
    CloseFile(1)
    
  EndIf
  
CompilerEndIf

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
Post Reply