Code archives/Graphics/Simple Image Encryption

This code has been declared by its author to be Public Domain code.

Download source code

Simple Image Encryption by Matty2009
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 this...no 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.
Graphics 800,600,0,2
image=LoadImage("your image file goes here")
SetBuffer ImageBuffer(image)
iw=ImageWidth(image)-1
ih=ImageHeight(image)-1
SeedRnd 1
LockBuffer
For x=0 To iw
	For y=0 To ih
		WritePixelFast x,y,ReadPixelFast(x,y) Xor Rand(16777215)
	Next
Next
UnlockBuffer
SetBuffer BackBuffer()
DrawImage image,0,0
Flip
WaitKey
SeedRnd 1
For x=0 To iw
	LockBuffer
	For y=0 To ih
		Col=Rand(16777215)
		If y<GraphicsHeight() And x<GraphicsWidth() Then 
			WritePixelFast x,y,ReadPixelFast(x,y) Xor Col
		EndIf 
	Next
	UnlockBuffer
	Flip False
Next
End

Comments

None.

Code Archives Forum