Here's a very basic level for just a data file where you might store game variables (user name, lives, score, etc)
gMEMEBLOCK_ID is global so it can be accessed during the game.
GAME_KEY[8]=[1,2,38,44,250,119,246,117,111] is the XOR encryption key (must be the same when saving or loading).
function SaveFileEncrypted()
dim GAME_KEY[8]=[1,2,38,44,250,119,246,117,111]
size = GetMemblockSize(gMEMBLOCK_ID)
fileID = OpenToWrite(sDATA_FILE , 0)
keyIndex = 1
for i = 1 to size
b = GetMemblockByte(gMEMBLOCK_ID , i-1)
keyByte = GAME_KEY[keyIndex]
eByte = keyByte ~~ b
WriteByte(fileID , eByte)
inc keyIndex
if keyIndex = 9 then keyIndex = 1
next i
fileSize = GetFileSize(fileID)
CloseFile(fileID)
undim GAME_KEY[]
if fileSize <> size
DeleteFile(sDATA_FILE)
endif
endfunction
function LoadFileEncrypted()
pass = 0
if GetFileExists(sDATA_FILE) = 1
dim GAME_KEY[8]=[1,2,38,44,250,119,246,117,111]
fileID = OpenToRead(sDATA_FILE)
size = GetFileSize(fileID)
gMEMBLOCK_ID = CreateMemblock(size)
`null out the memblock
for i = 1 to size
SetMemblockByte(gMEMBLOCK_ID , i-1 , 0)
next i
keyIndex = 1
for i = 1 to size
b = ReadByte(fileID)
keyByte = GAME_KEY[keyIndex]
dByte = keyByte ~~ b
SetMemblockByte(gMEMBLOCK_ID , i-1 , dByte)
inc keyIndex
if keyIndex = 9 then keyIndex = 1
next i
CloseFile(fileID)
undim GAME_KEY[]
pass = 1
else
pass = 0
endif
endfunction pass
Here's my library for loading encrypted media:
function AGCDecryptFile(sFileName$)
gosub _init_keys
fileID = OpenToRead(sFilename$)
// Find memArray index
memArrayIndex = 0
for i = 1 to val(fileNameKey$[0])
if sFileName$ = fileNameKey$[i]
memArrayIndex = i
EXIT
endif
next i
if memArrayIndex = 0
Message("FATAL ERROR Could not find memArrayindex for file " + sFilename$ , 1 , 1 , 1 , 1)
EXITFUNCTION ""
endif
//Determine the file's extension
memblockIndex = 1 // byte 0 is the total array length
repeat
b = GetMemblockByte(memKeyArray[memArrayIndex] , memBlockIndex)
if b <> 0
ext$ = ext$ + Chr(b)
endif
inc memBlockIndex
until b = 0
extByteLen = memBlockIndex - 1 // subtract 1 for the null separator byte
// Determine the decrypted file's name
origExtLen = len(GetStringToken(sFileName$ , "." , CountStringTokens(sFilename$ , ".") ))
fileRoot$ = Left(sFileName$ , Len(sFileName$) - origExtLen)
decFileName$ = fileRoot$ + ext$
fileIDDec = OpenToWrite(decFileName$ , 0)
fileSize = GetFileSize(fileID)
keyLenStart = GetMemblockByte(memKeyArray[memArrayIndex],memblockIndex)
inc memblockIndex
keyLenEnd = GetMemblockByte(memKeyArray[memArrayIndex],memblockIndex)
inc memblockIndex
for i = 1 to fileSize //- 1
encByte = ReadByte(fileID)
if i <= keyLenStart or i >= fileSize - keyLenEnd + 1
keyByte = GetMemblockByte(memKeyArray[memArrayIndex],memblockIndex)
inc memblockIndex
decByte = keyByte ~~ encByte
WriteByte(fileIDDec , decByte)
else
WriteByte(fileIDDec , encByte)
endif
next i
CloseFile(fileID) : CloseFile(fileIDDec)
gosub _kill_keys
endfunction decFileName$
function AGCDecryptFile2(sFileName$ , outputFile$)
gosub _init_keys
fileID = OpenToRead(sFilename$)
fileIDTemp = OpenToWrite(outputFile$ , 0)
// Find memArray index
memArrayIndex = 0
for i = 1 to val(fileNameKey$[0])
if sFileName$ = fileNameKey$[i]
memArrayIndex = i
EXIT
endif
next i
if memArrayIndex = 0
Message("FATAL ERROR Could not find memArrayindex for file " + sFilename$ , 1 , 1 , 1 , 1)
EXITFUNCTION ""
endif
fileSize = GetFileSize(fileID)
keyLenStart = GetMemblockByte(memKeyArray[memArrayIndex],0)
discard = ReadByte(fileID)
for i = 1 to fileSize - 1 // start at one since we read the first byte already
encByte = ReadByte(fileID)
if i <= keyLenStart
keyByte = GetMemblockByte(memKeyArray[memArrayIndex],i)
decByte = keyByte ~~ encByte
WriteByte(fileIDTemp , decByte)
else
WriteByte(fileIDTemp , encByte)
endif
next i
CloseFile(fileID) : CloseFile(fileIDTemp)
gosub _kill_keys
endfunction outputFile$
function LoadImageEnc(sFileName$)
decFile$ = AGCDecryptFile(sFileName$)
imageID = LoadImage(decFile$)
DeleteFile(decFile$)
endfunction imageID
gosub _init_keys is a sub that contains declarations for all of the key arrays, file names, etc
_init_keys:
memArrayCount = 1
dim memKeyArray[memArrayCount]
memKeyArray[0] = memArrayCount
dim fileNameKey$[memArrayCount]
fileNameKey$[0] = Str(memArrayCount)
fileNameKey$[memArrayCount] = "option_menu_button.jet"
dim _option_menu_button_KEY[262]=[262,112,110,103,0,128,128,161,215,246,7,119,112,19,49,180,195,61,137,107,166,194,182,244,30,94,17,145,119,2,92,81,148,211,177,53,131,118,184,146,84,139,29,182,44,119,65,206,149,103,48,8,24,146,212,244,14,190,104,48,179,5,46,68,124,217,101,203,171,70,118,212,32,119,110,135,114,124,176,12,62,74,236,168,191,3,75,41,159,120,236,133,13,237,148,190,16,28,161,8,27,124,64,46,225,194,224,50,106,168,231,52,103,238,128,148,195,159,131,91,122,150,135,215,174,64,245,134,248,164,78,54,252,160,128,105,64,117,93,254,62,134,76,14,183,92,62,21,75,238,228,82,71,31,32,112,190,137,185,101,219,121,82,155,117,58,157,77,228,151,204,53,19,155,72,100,215,155,166,113,127,119,215,213,173,28,158,150,100,77,71,35,97,164,151,127,175,211,255,162,118,48,20,29,20,113,68,252,110,179,193,240,136,194,194,174,84,148,235,146,200,22,9,136,51,44,3,44,209,242,14,48,157,73,31,254,102,143,166,159,37,173,121,220,142,87,14,163,229,143,238,172,19,107,102,91,249,25,128,233,4,32,50]
memKeyArray[memArrayCount] = CreateMemblock(_option_menu_button_KEY[0]+1)
for i = 0 to _option_menu_button_KEY[0]
SetMemblockByte(memKeyArray[memArrayCount] , i , _option_menu_button_KEY[i])
next i
gosub _kill_keys is a sub to undim all of the keys created in _init_keys to free up memory.
I've attached the tool I used for encrypting the files. It's not really intended for public use so isn't well commented and you'll need to actually read it to figure out how to use it. But it is short. Basically what it does is it takes a file you put in the media folder, generates a random key to encrypt it with, encodes the file with that key. The first byte is the overall key size, then next 3 bytes are the file's true extension. I can't recall if this encodes the head and tail of the files or not. It's at least the head. It actually looks like the entire file is encoded with the key. That's likely overkill, but it doesn't matter much because once you load the file into a memblock the most intensive part is done, the actual iteration through the memblock and XOR is extremely fast. The loading the file and saving the files is the slow part. But now I don't think you even need to save the file if it is an image since there's CreateImageFromMemblock now you could actually skip the saving part, but you'll need to learn the structure of your file types (PNG / JPG / etc) to understand how to load them into a memblock for use as an image. PNG isn't hard to do this with. No clue about doing this with JPG though.
The attached program parses out the arrays and everything for the keys (keys.agc) and undimming the keys (keysundim.agc) so you can just toss your media into this program's media folder, run the program to encode all. Then copy and include the two key files into your own program. Then make the functions for decoding them in your program too. It might be useful to you to go through a tool-building exercise for this kind of program on your own. It's not too hard, just a bit time consuming. Also it's really good logic, algorithm, and tool-building practice.