Page 1 of 1

how can i crypt game assets and load ?

Posted: Sun Jan 10, 2021 6:32 am
by skinkairewalker
hello everyone !

i am trying to crypt game assets 3d, sounds and texture .

is there any way to encrypt all assets and import into my game in pb?

Re: how can i crypt game assets and load ?

Posted: Sun Jan 10, 2021 8:20 am
by Mijikai
U could use the AES Cipher

Here is some code i wrote (Unicode):

Code: Select all


EnableExplicit

Procedure.i CryptBuffer(*Buffer,Length.i,Key.s,Decrypt.i = #False)
  Protected *key
  Protected *block
  If Key And *Buffer And Length > 15
    *key = Ascii(Key + Space(8))
    If *key
      *block = AllocateMemory(Length)
      If *block
        If Decrypt
          If AESDecoder(*Buffer,*block,Length,*key,128,#Null,#PB_Cipher_ECB)
            FreeMemory(*key)
            ProcedureReturn *block
          EndIf
        Else
          If AESEncoder(*Buffer,*block,Length,*key,128,#Null,#PB_Cipher_ECB)
            FreeMemory(*key)
            ProcedureReturn *block
          EndIf
        EndIf
        FreeMemory(*block)
      EndIf
      FreeMemory(*key)
    EndIf
  EndIf
  ProcedureReturn #Null
EndProcedure

Procedure.i Main()
  Protected *encrypt
  Protected *decrypt
  Protected dummy.s
  dummy = "Hello World!"
  *encrypt = CryptBuffer(@dummy,StringByteLength(dummy) + 2,"Password")
  If *encrypt
    *decrypt = CryptBuffer(*encrypt,MemorySize(*encrypt),"Password",#True)
    If *decrypt
      ShowMemoryViewer(*decrypt ,MemorySize(*decrypt))
      FreeMemory(*decrypt)
    EndIf
    FreeMemory(*encrypt)
  EndIf
  ProcedureReturn
EndProcedure

Main()

End
Edit:
I dont know about the loading part.
Ogre is unable to load stuff from memory iirc.

Re: how can i crypt game assets and load ?

Posted: Sun Jan 10, 2021 12:11 pm
by NicTheQuick
Why do you want to encrypt it? The key to decrypt the data would be in the app anyway.

Re: how can i crypt game assets and load ?

Posted: Sun Jan 10, 2021 6:18 pm
by Saki
Well, for textures, as sam ple, you can just XOR randomize the image in memory.
For this you set RandomSeed() to a starting value and create as many randoms as you need.
Then you save it and load it the other way around.
Otherwise it is like Nick says.
The only way to get a handle this, is to dig very deep into the bag of tricks.
A Hint.
With AES you don't create a plain text password.
Also an Asccii password shortens the key with AES128 to 8 bytes.
But I think for a few textures or other this is little relevant.

Re: how can i crypt game assets and load ?

Posted: Sun Jan 10, 2021 6:41 pm
by NicTheQuick
Saki wrote:Well, for textures, as sam ple, you can just XOR randomize the image in memory.
For this you set RandomSeed() to a starting value and create as many randoms as you need.
Using a random seed is not encryption!
Saki wrote:Then you save it and load it the other way around.
Also randomizing the image's content and then saving it will result in a very bad image quality if you save it in a compressed format like JPG. That's because JPG is optimized for a reasonable image.

Re: how can i crypt game assets and load ?

Posted: Sun Jan 10, 2021 7:20 pm
by Saki
Pre encrypted textures are never saved as JPEGs.
An encrypted or XOR-ed texture also successfully resists any compression.
A lossy compression destroys every encrypted image.
And if he puts the key in the code, he has no working encryption anyway.
Then he has no more than with the XOR, no matter how he does it.

Everything else you put in a structure, encrypt it using the address
of the first entry and sizeof and save it.
Included strings create as FixedString.