March 05, 2021, 06:43:36 AM

Author Topic: [bb] Simple Image Encryption by Matty [ 1+ years ago ]  (Read 412 times)

Offline BlitzBot

[bb] Simple Image Encryption by Matty [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Simple Image Encryption
Author : Matty
Posted : 1+ years ago

Description : This code provides a simple method for encrypting an image.  It should be fairly obvious what it does, but I'll explain anyway:

Each pixel's RGB value is XORed with a random number between 0 and 16777215 with a particular seed.  This creates an image that looks like random noise, most of the time.  The resulting image can then be XORed with the same sequence of random numbers to convert the image back.  In the example below I simply draw the converted images to the backbuffer and display them, but it is just as easy to use an imagebuffer and save the resulting image.

Note that because of the possibility of random number sequences being different on different machines for the same seed you should really use your own random number generator - which is not hard - look up the VBA random number generator method - it's good enough for something like need for crazy mersenne twisters etc.

An interesting other possibility:
You can encrypt the image multiple times with a different seed each time, and as long as you reverse the process with the same seeds the image will be decrypted fine.  

This could be used, I guess, to encrypt texture / image information in your games so that to the end user if they view the bitmaps/pngs in your resource files they will just see noise.

Code :
Code: BlitzBasic
  1. Graphics 800,600,0,2
  2. image=LoadImage("your image file goes here")
  3. SetBuffer ImageBuffer(image)
  4. iw=ImageWidth(image)-1
  5. ih=ImageHeight(image)-1
  6. SeedRnd 1
  7. LockBuffer
  8. For x=0 To iw
  9.         For y=0 To ih
  10.                 WritePixelFast x,y,ReadPixelFast(x,y) Xor Rand(16777215)
  11.         Next
  12. Next
  13. UnlockBuffer
  14. SetBuffer BackBuffer()
  15. DrawImage image,0,0
  16. Flip
  17. WaitKey
  18. SeedRnd 1
  19. For x=0 To iw
  20.         LockBuffer
  21.         For y=0 To ih
  22.                 Col=Rand(16777215)
  23.                 If y<GraphicsHeight() And x<GraphicsWidth() Then
  24.                         WritePixelFast x,y,ReadPixelFast(x,y) Xor Col
  25.                 EndIf
  26.         Next
  27.         UnlockBuffer
  28.         Flip False
  29. Next
  30. End

Comments : none...


SimplePortal 2.3.6 © 2008-2014, SimplePortal