March 05, 2021, 06:43:36 AM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email
?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
Like stats
Home
Forum
Help
Search
Gallery
Login
Register
SyntaxBomb - Indie Coders
»
Languages & Coding
»
Blitz Code Archives
»
Graphics
»
[bb] Simple Image Encryption by Matty [ 1+ years ago ]
« previous
next »
Print
Pages: [
1
]
Go Down
Author
Topic: [bb] Simple Image Encryption by Matty [ 1+ years ago ] (Read 412 times)
BlitzBot
Jr. Member
Posts: 1
Total likes: 0
[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 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.
Code :
Code: BlitzBasic
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...
Logged
Print
Pages: [
1
]
Go Up
« previous
next »
SyntaxBomb - Indie Coders
»
Languages & Coding
»
Blitz Code Archives
»
Graphics
»
[bb] Simple Image Encryption by Matty [ 1+ years ago ]
SimplePortal 2.3.6 © 2008-2014, SimplePortal