Hello
I have done more on this
Now you can encrypt a file, which accepts a filename, amount of maxchunks to do various swapping (maximum 1024 bits), but the more you have, the more performance degrading game might
become.
What it does is.
- Loads a file into memory
- Goes through the file from memory according to the "maxchunks" you specify
---------swaps them around
---------XOR the chunk by the maxchunk you specify in the parameter
---------saves the chunk back into memory
---------Repeats this until processed all the file
---------Creates a new file (encrypted) into your media folder in the Appdata area.
// Function encrypt accepts -
// file$ = path to the media
// encfile$ = filename to the new encrypted file
// maxchunks = process the swap mechanism with the amount of byte
// per swap to take place.... max 1024 (but speed will deter
// the higher this is.
// typ = the type of file it is, 1= pic/sprite, 2=sound, 3=music
//
// Returns the encrypted memblock as a sprite/sound/music
function encrypt(file$ as string, encfile$ as string, maxchunks as integer,typ as integer)
//Up to 1024 bit swapping mechanism
chunks as integer[1024]
swappedchunks as integer[1024]
maxchunks=maxchunks*4
if typ=1//Picture
img=LoadImage(file$)
mem=CreateMemblockFromImage(img)
//image=CreateImageFromMemblock(mem)
imgwid=GetMemblockInt(mem,0)
imghei=GetMemblockInt(mem,4)
imgsiz=GetMemblocksize(mem)
start=0
for a=12 to imgsiz-1 step 4// add bytes to the chunk
if start>maxchunks
// Read
for b=0 to maxchunks
if a+b+1 > imgsiz-1 then exit
chunks[b]=GetMemblockByte(mem,a+b+1)
next
//Swap full
top=maxchunks
for b=0 to maxchunks
swappedchunks[top]=chunks[b]
swappedchunks[top]=swappedchunks[top]~~maxchunks
top=top-1
next
//Write
for b=0 to maxchunks
if a+b+1 > imgsiz-1 then exit
setMemblockByte(mem,a+b+1,swappedchunks[b])
next
start=0
else
start=start+4
endif
// SetMemblockByte(mem,a,byte ~~ key)
// print(a)
//sync()
next
img=CreateImageFromMemblock(mem)
CreateFileFromMemblock(encfile$,mem)
result=createsprite(img)
DeleteMemblock(mem)
endif
endfunction result
Decrypt code for the encryption is
// Function decrypt accepts
// file$ = path to the media
// maxchunks = process the swap mechanism with the amount of byte
// per swap to take place.... max 1024 (but speed will deter
// the higher this is - This requires to exactly the chunk
// size as you encrypted a file with otherwise wont appear
// correctly
//
// typ = the type of file it is, 1= pic/sprite, 2=sound, 3=music
//
// Returns the encrypted memblock as a sprite/sound/music
function decrypt(file$, maxchunks as integer, typ as integer)
//1024 bit swapping mechanism
chunks as integer[1024]
swappedchunks as integer[1024]
print("Dec")
maxchunks=maxchunks*4
if typ=1 // Picture
mem=CreateMemblockFromFile(file$)
imgwid=GetMemblockInt(mem,0)
imghei=GetMemblockInt(mem,4)
imgsiz=GetMemblocksize(mem)
start=0
for a=12 to imgsiz-1 step 4// add bytes to the chunk
if start>maxchunks
// Read
for b=0 to maxchunks
if a+b+1 > imgsiz-1 then exit
chunks[b]=GetMemblockByte(mem,b+a+1)
next
//Swap
top=maxchunks
for b=0 to maxchunks
swappedchunks[top]=chunks[b]
swappedchunks[top]=swappedchunks[top]~~maxchunks
top=top-1
next
//Write
for b=0 to maxchunks
if a+b+1 > imgsiz-1 then exit
setMemblockByte(mem,a+b+1,swappedchunks[b])
next
start=0
else
start=start+4
endif
// SetMemblockByte(mem,a,byte ~~ key)
// print(a)
//sync()
next
img=CreateImageFromMemblock(mem)
result=createsprite(img)
endif
endfunction result
Here is a bit of code to see them in action, use at beginning of your main file
SetErrorMode(2)
width=1024:height=768
// set window properties
SetWindowTitle( "EncryptionApp" )
SetWindowSize( width, height, 0 )
// set display properties
SetVirtualResolution( width, height )
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 100, 0 ) // 30fps instead of 60 to save battery
t=CreateText("Loading.....")
SetTextSize(t,30)
SetTextPosition(t,width/2-100,height/2)
fn$="level1.png"
a=createsprite(loadimage(fn$))
SetSpritePosition(a,0,0)
print ("Original image. Press 'Space' to encrypt")
sync()
repeat
until GetRawKeyPressed(32)=1
ClearScreen()
sync()
encrypt(fn$,fn$+"enc",16,1)
print ("Encrypted, press 'Space' to decrypt")
sync()
repeat
until GetRawKeyPressed(32)=1
ClearScreen()
sync()
fn$=fn$+"enc"
decrypt(fn$,16,1)
print ("Decrypted, press 'Space' to exit")
sync()
repeat
until GetRawKeyPressed(32)=1
Once an image has been through the encrypted function, it places it into the project media folder in the Appdata area called "filename.pngenc"
This is when you can Exclude the original image files now from the main game project area and let the decrypt function do its work to decrypt
and load them normally as a sprite.
Good luck and hope it works out for some of you who wishes to have your assets encrypted someway
Any probs, give me a shout
Damo
Using Tier 1 AppGameKit V2
Started coding with AMOS