Image and Texture crypter - simplest use + AES
Posted: Sat Jun 26, 2021 5:17 pm
Since there are always inquiries about this, here two simplest to use solutions.
This solutions works 'on place', it can encrypt images and textures very fast in itself,
without the need of temporary images or buffers.
On all you can set a password or not.
You can only compress encrypted images lossless, e.g. save as PNG, not as JPEG,
otherwise the content of the encrypted images will be destroyed.
Hints:
You can also use the BaseU_BF and all Data_Codes_BF for this purpose.
Have fun
AES based
Here is a code example that shows how to make worthless the images.
You can still see the images but they are worthless for others.
Diffusor
Diffusor with AES
This solutions works 'on place', it can encrypt images and textures very fast in itself,
without the need of temporary images or buffers.
On all you can set a password or not.
You can only compress encrypted images lossless, e.g. save as PNG, not as JPEG,
otherwise the content of the encrypted images will be destroyed.
Hints:
You can also use the BaseU_BF and all Data_Codes_BF for this purpose.
Have fun
Code: Select all
EnableExplicit
; Hidde your images and textures - Simple 'on place' image crypter
Procedure EncryptImageSimple_BF(image_ID, seed=0)
Protected i : RandomSeed(seed) ; By Saki
If Not IsImage(image_ID) Or Not StartDrawing(ImageOutput(image_ID)) : ProcedureReturn 0 : EndIf
Protected image_length=DrawingBufferPitch()*ImageHeight(image_ID)-1
Protected *buffer=DrawingBuffer() : StopDrawing()
For i=0 To image_length : PokeA(*buffer+i, PeekA(*buffer+i)+Random(255)) : Next : ProcedureReturn 1
EndProcedure
Procedure DecryptImageSimple_BF(image_ID, seed=0)
Protected i : RandomSeed(seed) ; By Saki
If Not IsImage(image_ID) Or Not StartDrawing(ImageOutput(image_ID)) : ProcedureReturn 0 : EndIf
Protected image_length=DrawingBufferPitch()*ImageHeight(image_ID)-1
Protected *buffer=DrawingBuffer() : StopDrawing()
For i=0 To image_length : PokeA(*buffer+i, PeekA(*buffer+i)-Random(255)) : Next : ProcedureReturn 1
EndProcedure
; ######## Get the result ########
UsePNGImageDecoder() : UseJPEGImageDecoder() : UseTIFFImageDecoder()
Define path$=OpenFileRequester("Select a image", "", "", 0) : If path$="" : End : EndIf
Define image_ID=LoadImage(#PB_Any, path$)
ResizeImage(image_ID, 300, 300)
Define window_ID=OpenWindow(#PB_Any, 0, 0, 660, 340, "Left=encrypted - Right=decrypted", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
; #### Encrypt a image ####
Define seed_as_password=1000
EncryptImageSimple_BF(image_ID, seed_as_password)
Define image_gadget_ID=ImageGadget(#PB_Any, 20, 20, 0, 0, ImageID(image_ID))
; #### Decrypt a image ####
Define seed_as_password=1000
DecryptImageSimple_BF(image_ID, seed_as_password)
Define image_gadget_1_ID=ImageGadget(#PB_Any, 340, 20, 0, 0, ImageID(image_ID))
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
AES based
Code: Select all
EnableExplicit
; Hidde your images and textures - Simple 'on place' AES based image crypter
Procedure EncryptImageSimple_BF(image_ID, password$="")
Protected i, ii, i1, depth : UseMD5Fingerprint() ; By Saki
Protected fixed$=StringFingerprint(password$+"%$(s4DäÖÄö", #PB_Cipher_MD5) : Dim register.q(1)
Repeat ; 16 Bytes
PokeA(@register(0)+i, Val("$"+PeekS(@fixed$+ii, 2))) : ii+SizeOf(character)<<1 : i+1 ; Create a key
Until ii=StringByteLength(fixed$) : i=0 : ii=0
If Not IsImage(image_ID) Or Not StartDrawing(ImageOutput(image_ID)) : ProcedureReturn 0 : EndIf
Protected image_length=DrawingBufferPitch()*ImageHeight(image_ID)-1
Protected *buffer=DrawingBuffer() : StopDrawing()
For i=0 To image_length
Repeat
If Not i1 : AESEncoder(@register(0), @register(0), 16, @register(0), 128, 0, #PB_Cipher_ECB) : EndIf
depth=PeekA(@register(0)+i1) : i1+1 : If i1>15 : i1=0 : EndIf
Until depth
PokeA(*buffer+i, PeekA(*buffer+i)+depth) : Next : ProcedureReturn 1
EndProcedure
Procedure DecryptImageSimple_BF(image_ID, password$="")
Protected i, ii, i1, depth : UseMD5Fingerprint() ; By Saki
Protected fixed$=StringFingerprint(password$+"%$(s4DäÖÄö", #PB_Cipher_MD5) : Dim register.q(1)
Repeat ; 16 Bytes
PokeA(@register(0)+i, Val("$"+PeekS(@fixed$+ii, 2))) : ii+SizeOf(character)<<1 : i+1 ; Create a key
Until ii=StringByteLength(fixed$) : i=0 : ii=0
If Not IsImage(image_ID) Or Not StartDrawing(ImageOutput(image_ID)) : ProcedureReturn 0 : EndIf
Protected image_length=DrawingBufferPitch()*ImageHeight(image_ID)-1
Protected *buffer=DrawingBuffer() : StopDrawing()
For i=0 To image_length
Repeat
If Not i1 : AESEncoder(@register(0), @register(0), 16, @register(0), 128, 0, #PB_Cipher_ECB) : EndIf
depth=PeekA(@register(0)+i1) : i1+1 : If i1>15 : i1=0 : EndIf
Until depth
PokeA(*buffer+i, PeekA(*buffer+i)-depth) : Next : ProcedureReturn 1
EndProcedure
; ######## Get the result ########
UsePNGImageDecoder() : UseJPEGImageDecoder() : UseTIFFImageDecoder()
Define path$=OpenFileRequester("Select a image", "", "", 0) : If path$="" : End : EndIf
Define image_ID=LoadImage(#PB_Any, path$)
ResizeImage(image_ID, 300, 300)
Define window_ID=OpenWindow(#PB_Any, 0, 0, 660, 340, "Left=encrypted - Right=decrypted", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
; #### Encrypt a image ####
Define password$="Hello Password"
EncryptImageSimple_BF(image_ID, password$)
Define image_gadget_ID=ImageGadget(#PB_Any, 20, 20, 0, 0, ImageID(image_ID))
; #### Decrypt a image ####
Define password$="Hello Password"
DecryptImageSimple_BF(image_ID, password$)
Define image_gadget_1_ID=ImageGadget(#PB_Any, 340, 20, 0, 0, ImageID(image_ID))
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
Here is a code example that shows how to make worthless the images.
You can still see the images but they are worthless for others.
Diffusor
Code: Select all
EnableExplicit
; Hidde your images and textures - Simple 'on place' image crypter
Procedure EncryptImageSimple_BF(image_ID, seed=0)
Protected i : RandomSeed(seed) ; By Saki
If Not IsImage(image_ID) Or Not StartDrawing(ImageOutput(image_ID)) : ProcedureReturn 0 : EndIf
Protected image_length=DrawingBufferPitch()*ImageHeight(image_ID)-1
Protected *buffer=DrawingBuffer() : StopDrawing()
For i=0 To image_length : If Random(2)>1 : PokeA(*buffer+i, PeekA(*buffer+i)+Random(255)) : EndIf : Next : ProcedureReturn 1
EndProcedure
Procedure DecryptImageSimple_BF(image_ID, seed=0)
Protected i : RandomSeed(seed) ; By Saki
If Not IsImage(image_ID) Or Not StartDrawing(ImageOutput(image_ID)) : ProcedureReturn 0 : EndIf
Protected image_length=DrawingBufferPitch()*ImageHeight(image_ID)-1
Protected *buffer=DrawingBuffer() : StopDrawing()
For i=0 To image_length : If Random(2)>1 : PokeA(*buffer+i, PeekA(*buffer+i)-Random(255)) : EndIf : Next : ProcedureReturn 1
EndProcedure
; ######## Get the result ########
UsePNGImageDecoder() : UseJPEGImageDecoder() : UseTIFFImageDecoder()
Define path$=OpenFileRequester("Select a image", "", "", 0) : If path$="" : End : EndIf
Define image_ID=LoadImage(#PB_Any, path$)
ResizeImage(image_ID, 300, 300)
Define window_ID=OpenWindow(#PB_Any, 0, 0, 660, 340, "Left=encrypted - Right=decrypted", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
; #### Encrypt a image ####
Define seed_as_password=1000
EncryptImageSimple_BF(image_ID, seed_as_password)
Define image_gadget_ID=ImageGadget(#PB_Any, 20, 20, 0, 0, ImageID(image_ID))
; #### Decrypt a image ####
Define seed_as_password=1000
DecryptImageSimple_BF(image_ID, seed_as_password)
Define image_gadget_1_ID=ImageGadget(#PB_Any, 340, 20, 0, 0, ImageID(image_ID))
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
Diffusor with AES
Code: Select all
EnableExplicit
; Hidde your images and textures - Simple 'on place' AES based image crypter
Procedure EncryptImageSimple_BF(image_ID, password$="")
Protected i, ii, i1, depth : UseMD5Fingerprint() ; By Saki
Protected fixed$=StringFingerprint(password$+"%$(s4DäÖÄö", #PB_Cipher_MD5) : Dim register.q(1)
Repeat ; 16 Bytes
PokeA(@register(0)+i, Val("$"+PeekS(@fixed$+ii, 2))) : ii+SizeOf(character)<<1 : i+1 ; Create a key
Until ii=StringByteLength(fixed$) : i=0 : ii=0
If Not IsImage(image_ID) Or Not StartDrawing(ImageOutput(image_ID)) : ProcedureReturn 0 : EndIf
Protected image_length=DrawingBufferPitch()*ImageHeight(image_ID)-1
Protected *buffer=DrawingBuffer() : StopDrawing()
RandomSeed(1)
For i=0 To image_length
Repeat
If Not i1 : AESEncoder(@register(0), @register(0), 16, @register(0), 128, 0, #PB_Cipher_ECB) : EndIf
depth=PeekA(@register(0)+i1) : i1+1 : If i1>15 : i1=0 : EndIf
Until depth
If Random(2)>1
PokeA(*buffer+i, PeekA(*buffer+i)+depth)
EndIf
Next
ProcedureReturn 1
EndProcedure
Procedure DecryptImageSimple_BF(image_ID, password$="")
Protected i, ii, i1, depth : UseMD5Fingerprint() ; By Saki
Protected fixed$=StringFingerprint(password$+"%$(s4DäÖÄö", #PB_Cipher_MD5) : Dim register.q(1)
Repeat ; 16 Bytes
PokeA(@register(0)+i, Val("$"+PeekS(@fixed$+ii, 2))) : ii+SizeOf(character)<<1 : i+1 ; Create a key
Until ii=StringByteLength(fixed$) : i=0 : ii=0
If Not IsImage(image_ID) Or Not StartDrawing(ImageOutput(image_ID)) : ProcedureReturn 0 : EndIf
Protected image_length=DrawingBufferPitch()*ImageHeight(image_ID)-1
Protected *buffer=DrawingBuffer() : StopDrawing()
RandomSeed(1)
For i=0 To image_length
Repeat
If Not i1 : AESEncoder(@register(0), @register(0), 16, @register(0), 128, 0, #PB_Cipher_ECB) : EndIf
depth=PeekA(@register(0)+i1) : i1+1 : If i1>15 : i1=0 : EndIf
Until depth
If Random(2)>1
PokeA(*buffer+i, PeekA(*buffer+i)-depth)
EndIf
Next
ProcedureReturn 1
EndProcedure
; ######## Get the result ########
UsePNGImageDecoder() : UseJPEGImageDecoder() : UseTIFFImageDecoder()
Define path$=OpenFileRequester("Select a image", "", "", 0) : If path$="" : End : EndIf
Define image_ID=LoadImage(#PB_Any, path$)
ResizeImage(image_ID, 300, 300)
Define window_ID=OpenWindow(#PB_Any, 0, 0, 660, 340, "Left=encrypted - Right=decrypted", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
; #### Encrypt a image ####
Define password$="Hello Password"
EncryptImageSimple_BF(image_ID, password$)
Define image_gadget_ID=ImageGadget(#PB_Any, 20, 20, 0, 0, ImageID(image_ID))
; #### Decrypt a image ####
Define password$="Hello Password"
DecryptImageSimple_BF(image_ID, password$)
Define image_gadget_1_ID=ImageGadget(#PB_Any, 340, 20, 0, 0, ImageID(image_ID))
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow