also be used in this form in professional applications or games.
This crypter works with AES-256 in CBC mode with a crypt randomized IV.
The special thing about it is that no temporary unencrypted images are created.
Everything is done directly in memory, which provides a very high security.
If you encrypt the same image a million times with this crypter, each of these encrypted images is different down to the bit level.
With this you can solve the problem of encrypted images comprehensively and completely.
All image formats can be encrypted, for example also Gif.
The Crypter is extremely fast.
The images and textures can also be easily read in a batch process and allocated in a flash.
Have fun with it
Encrypter
Code: Select all
EnableExplicit
Procedure LoadImage_Encrypt_and_Save_BF(source_path_image$, destination_path_image$, password$, crypt_extender$=" [encrypted]")
UseSHA3Fingerprint() ; By Saki - Advanced AES CBC mode image Encrypter with crypt randomized IV
Protected fixed$=StringFingerprint(password$+"%$(s4DäÖÄö", #PB_Cipher_SHA3), i, ii, *buffer : Dim register.q(5)
Protected extension$="."+GetExtensionPart(source_path_image$)
destination_path_image$=GetPathPart(source_path_image$)+GetFilePart(source_path_image$)
destination_path_image$=RemoveString(destination_path_image$, extension$) : destination_path_image$+crypt_extender$+extension$
Repeat ; 32 Bytes
PokeA(@register(0)+i, Val("$"+PeekS(@fixed$+ii, 2))) : ii+SizeOf(character)<<1 : i+1 ; Create a key
Until ii=StringByteLength(fixed$)
Protected file=ReadFile(#PB_Any, source_path_image$) : If Not file : ProcedureReturn -21 : EndIf
If OpenCryptRandom() : CryptRandomData(@register.q(4), 16) : Else : RandomData(@register.q(4), 16) : EndIf
Protected length=Lof(file) : *buffer=AllocateMemory(length+16) : If Not *buffer : CloseFile(file) : ProcedureReturn -9 : EndIf
If Not ReadData(file, *buffer, length) : FreeMemory(*buffer) : CloseFile(file) : ProcedureReturn -21 : EndIf
CloseFile(file)
Protected *buffer_1=AllocateMemory(length+16) : If Not *buffer_1 : FreeMemory(*buffer) : ProcedureReturn -9 : EndIf
CopyMemory(@register(4), *buffer_1+length, 16)
If Not AESEncoder(*buffer, *buffer_1, length, @register(0), 256, @register(4), #PB_Cipher_CBC)
FreeMemory(*buffer) : FreeMemory(*buffer_1) : ProcedureReturn -9
EndIf
FreeMemory(*buffer) : file=CreateFile(#PB_Any, destination_path_image$)
If Not file : FreeMemory(*buffer_1) : ProcedureReturn -22 : EndIf
If WriteData(file, *buffer_1, length+16)<>length+16 : FreeMemory(*buffer_1) : CloseFile(file) : ProcedureReturn -22 : EndIf
CloseFile(file)
FreeMemory(*buffer_1)
ProcedureReturn 1
EndProcedure
; #### Encrypt a image ####
Define source_path_image$=OpenFileRequester("Select a image", "", "", 0) : If source_path_image$="" : End : EndIf
Define destination_path_image$=source_path_image$
Define password$="Hello Password"
LoadImage_Encrypt_and_Save_BF(source_path_image$, destination_path_image$, password$)
Decrypter
Code: Select all
EnableExplicit
Procedure LoadImage_and_Decrypt_BF(source_path_image$, password$, recreate_file=0, crypt_extender$=" [encrypted]")
UseSHA3Fingerprint() ; By Saki - Advanced AES CBC mode image Decrypter with crypt randomized IV
Protected fixed$=StringFingerprint(password$+"%$(s4DäÖÄö", #PB_Cipher_SHA3), i, ii : Dim register.q(5)
Repeat ; 32 Bytes
PokeA(@register(0)+i, Val("$"+PeekS(@fixed$+ii, 2))) : ii+SizeOf(character)<<1 : i+1 ; Create a key
Until ii=StringByteLength(fixed$)
Protected file=ReadFile(#PB_Any, source_path_image$)
If Not file : ProcedureReturn -21 : EndIf : Protected length=Lof(file), *buffer=AllocateMemory(length)
If Not *buffer : CloseFile(file) : ProcedureReturn -9 : EndIf : length-16 : FileSeek(file, length)
If ReadData(file, @register.q(4), 16)<>16 : CloseFile(file) : ProcedureReturn -21 : EndIf : FileSeek(file, 0)
If ReadData(file, *buffer, length)<>length : FreeMemory(*buffer) : CloseFile(file) : ProcedureReturn -21 : EndIf
CloseFile(file)
Protected *buffer_1=AllocateMemory(length) : If Not *buffer_1 : FreeMemory(*buffer) : ProcedureReturn -9 : EndIf
If Not AESDecoder(*buffer, *buffer_1, length, @register(0), 256, @register(4), #PB_Cipher_CBC)
FreeMemory(*buffer) : FreeMemory(*buffer_1) : CloseFile(file) : ProcedureReturn -9
EndIf
FreeMemory(*buffer)
If recreate_file
source_path_image$=RemoveString(source_path_image$, crypt_extender$, #PB_String_NoCase)
file=CreateFile(#PB_Any, source_path_image$)
If Not file : ProcedureReturn -21 : EndIf
If WriteData(file, *buffer_1, length)<>length : CloseFile(file) : FreeMemory(*buffer_1) : ProcedureReturn -22 : EndIf
CloseFile(file) : FreeMemory(*buffer_1)
ProcedureReturn 1
Else
Protected image_ID=CatchImage(#PB_Any, *buffer_1, length) : FreeMemory(*buffer_1)
If Not image_ID : ProcedureReturn -6 : EndIf : ProcedureReturn image_ID
EndIf
EndProcedure
; ######## Decrypt a image ########
UsePNGImageDecoder() : UseJPEGImageDecoder() : UseTIFFImageDecoder() : UseGIFImageDecoder()
Define source_path_image$=OpenFileRequester("Select a image", "", "", 0) : If source_path_image$="" : End : EndIf
Define recreate_image_file=0 ; Recreate a decrypted image from a encrypted image
Define password$="Hello Password"
Define image_ID=LoadImage_and_Decrypt_BF(source_path_image$, password$, recreate_image_file)
If recreate_image_file : If Not image_ID : MessageRequester("Hint", "Can not create a decrypted image") : EndIf : End : EndIf
Define window_ID=OpenWindow(#PB_Any, 0, 0, 650, 400, "Decrypted Image", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
If IsImage(image_ID)
If UCase(GetExtensionPart(source_path_image$))<>"GIF" : ResizeImage(image_ID, 300, 300) : EndIf
Define image_gadget_ID=ImageGadget(#PB_Any,
WindowWidth(window_ID)/2-ImageWidth(image_ID)/2,
WindowHeight(window_ID)/2-ImageHeight(image_ID)/2,
0,
0,
ImageID(image_ID))
Else
CloseWindow(window_ID)
MessageRequester("Hint", "Decrypted Image not usable")
End
EndIf
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow