These functions are specifically designed for storing information which the player cannot change to make them better at something in game.
eg.
If you made a gambling game, and needed to store each players money, when using write integer, anybody can open your file in a hex editor, and change that to a much higher value. When using these functions, this is almost impossible.
It works by storing two lots of your file, seemingly randomly mixed together. You must specify a numeric code (in a string) for the password. (Letters are not secure), and also an Xbyte value. The Xbyte value needs to be kept most secret, but although there are only 256 options, it is impossible to guess, unless you have the password as well.
The Xbyte is added on to every byte in the second copy of the data, and is to stop people pairing up the two file by using the byte values.
As most media types can be easily converted to a memblock in program, that is what the program reads.
If anybody attempts to tamper with the saved file, or the password/Xbyte is incorrect, then the Sec_Read_Memblock function will return 1
If in your save file, you only need to save a few bytes, it is good to pad it out with some extra useless bytes to make it more secure.
To securely store a memblock in a file, use Sec_Store_Memblock
To securely read a file to a memblock, use Sec_Read_Memblock
fsize is the size of the memblock to be created (if one does not exist with the specified number) and must be the size of the data, or an error will occur.
I hope somebody finds it useful
Here is the code:
function Sec_Store_Memblock(Memnum as dword, filename$ as string, overwrite as boolean, password$ as string, Xbyte as byte)
local ex as boolean
local mp as dword
local ic as integer
local rpt$ as string
local pp as dword
fnum = find free file()
if file exist(filename$)
ex = 1
if overwrite
delete file filename$
else
exitfunction ex
endif
endif
open to write fnum,filename$
inc pp
for mp = 0 to get memblock size(Memnum)-1
nbyte = memblock byte(Memnum,mp)
write byte fnum,nbyte
rpt$ = rpt$+chr$(nbyte)
if ic >= val(mid$(password$,pp))
ic = -1
inc pp
if pp > len(password$) then pp = 1
while len(rpt$) > 0
write byte fnum,wrapbyte(asc(rpt$)+Xbyte)
rpt$ = right$(rpt$,len(rpt$)-1)
endwhile
endif
inc ic
next mp
while len(rpt$) > 0
write byte fnum,wrapbyte(asc(rpt$)+Xbyte)
rpt$ = right$(rpt$,len(rpt$)-1)
endwhile
close file fnum
endfunction ex
function Sec_Read_Memblock(Memnum as dword, filename$ as string, fsize as dword, password$ as string, Xbyte as byte)
local ex as boolean
local mp as dword
local ic as integer
local rpt$ as string
local pp as dword
if memblock exist(Memnum) = 0
make memblock Memnum,fsize
endif
fnum = find free file()
ex = 0
open to read fnum,filename$
inc pp
for mp = 0 to get memblock size(Memnum)-1
read byte fnum,nbyte
write memblock byte Memnum,mp,nbyte
rpt$ = rpt$+chr$(nbyte)
if ic >= val(mid$(password$,pp))
ic = -1
inc pp
if pp > len(password$) then pp = 1
while len(rpt$) > 0
read byte fnum,cbyte1
cbyte2 = wrapbyte(asc(rpt$)+Xbyte)
if cbyte1 <> cbyte2 then ex = 1
rpt$ = right$(rpt$,len(rpt$)-1)
endwhile
endif
inc ic
next mp
while len(rpt$) > 0
read byte fnum,cbyte1
cbyte2 = wrapbyte(asc(rpt$)+Xbyte)
if cbyte1 <> cbyte2 then ex = 1
rpt$ = right$(rpt$,len(rpt$)-1)
endwhile
close file fnum
endfunction ex
function wrapbyte(b as integer)
while b > 255 : dec b,256 : endwhile
while b < 0 : inc b,256 : endwhile
endfunction b