It is currently Tue May 23, 2017 9:44 am



Welcome
Welcome to rfobasic

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today. **You are not required to provide truthful information to any registration questions. Be whomever you wish to be.!


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Picture Based Encryption
Unread postPosted: Fri Apr 14, 2017 12:20 pm 
Offline

Joined: Sat Jul 04, 2015 7:28 am
Posts: 40
The following program will use a bitmap file to encode a message and then use the same bitmap to decode it. It would not be able to decode the encrypted file using a different bitmap.

For this work in progress, you need to put a "Sample.txt" file containing the original message and a "Sample.png" file containing the bitmap to use as a key. The output files are "Message.bin" for the coded message and "DecryptedMessage.txt" for the decoded message.

I need to add an interface that allows the user to choose the file to let the user chose:
1. The bitmap file to use as the key
2. The text file to encode
3. The name for the binary file for the coded message
4. The existing binary file to be decoded
5. The name for the text file with the decoded message

To prove that you need the same bitmap key that was used to encode the message in order to decode it, try changing the following lines from

gr.bitmap.load key, "Sample.png"
%gr.bitmap.load key, "cartman.png"

to

%gr.bitmap.load key, "Sample.png"
gr.bitmap.load key, "cartman.png"

Program:


Fn.Def Encrypt(character$, image, x, y)
Gr.Get.bmpixel image, x, y, alpha, red, green, blue
offset = int((red + green + blue) / 3) %average of color values
value = ascii(character$)
newvalue = value + offset
If newvalue > 255 then newvalue = newvalue - 255 %roll over to prevent out of range numbers
Fn.Rtn newvalue
Fn.End

Fn.Def Decrypt$(value, image, x, y)
Gr.Get.bmpixel image, x, y, alpha, red, green, blue
offset = int((red + green + blue) / 3) %average of color values
newvalue = value - offset %Undo the offset from the encryption function
If newvalue < 0 then newvalue = newvalue + 255 %roll over to prevent out of range numbers
If newvalue = 253 then newvalue = 32 %remove strange character that does not decode properly.
Fn.Rtn Chr$(newvalue)
Fn.End

text.open r, source, "Sample.txt"

list.create s, plaintext

Print
Print "The orginal message:"
Print

Do %Read in the original message
text.readln source, line$

if line$ <> "EOF" then
print line$
list.add plaintext, line$
EndIf

until line$ = "EOF"

text.close source

gr.open

gr.bitmap.load key, "Sample.png" %get the "key" to create and/or solve the code
gr.bitmap.size key, picx, picy

%Encode the message!

pointx = 0 %these deterine which pixel is being used to encode a given character
pointy = 0

byte.open w, outputcoded, "Message.bin"

list.size plaintext, numberlines

For thisline = 1 to numberlines
list.get plaintext, thisline, line$
numberchars = len(line$)

while right$(line$, 1) = " " %trim white spaces at the end of the line
numberchars = numberchars - 1
line$ = left$(line$, numberchars)
repeat

line$ = line$ + chr$(254) %Use this as an end of line symbol.

For character = 1 to len(line$) %encode each line in the message

Thischar$ = mid$(line$,character,1)
byte.write.byte outputcoded, Encrypt(Thischar$, key, pointx, pointy)
pointx = pointx + 1

if (pointx > picx - 1) then %test for end of image column and move to next column if needed
pointx = 0
pointy = pointy + 1
if pointy > (picy - 1) then pointy = 0 %test for end of row and move to begining if needed
EndIf

Next

Next

byte.close outputcoded

gr.bitmap.delete key


%Decode the message!

gr.bitmap.load key, "Sample.png"

%gr.bitmap.load key, "cartman.png"

gr.bitmap.size key, picx, picy

Text.open w, output, "DecryptedMessage.txt"
byte.open r, input, "Message.bin"

message$ = ""

byte.read.byte input, codedbyte %read the first byte


pointx = 0 %these deterine which pixel is being used to encode a given character
pointy = 0
Lastchar$ = "a" %dummy value for the EndOfLine function the first time in the loop

list.create s, outlines %hold the decoded message before saving

while (codedbyte > -1) %test for end of file

DecodedChar$ = Decrypt$(codedbyte, key, pointx, pointy)

%if EndOfLine(Lastchar$, DecodedChar$) = 1 then %Start a new line

If DecodedChar$ = Chr$(254) then
list.add outlines, message$
message$ = ""
Else
message$ = message$ + DecodedChar$
EndIf

%message$ = message$ + DecodedChar$
%Lastchar$ = DecodedChar$

pointx = pointx + 1

if (pointx > picx - 1) then %test for end of image column and move to next column if needed
pointx = 0
pointy = pointy + 1
if pointy > (picy - 1) then pointy = 0 %test for end of row and move to begining if needed
EndIf

byte.read.byte input, codedbyte

repeat

list.add outlines, message$

byte.close input

list.size outlines, lines

Print
Print "The decoded message:"
Print

For ThisLine = 1 to lines %save the decoded message
list.get outlines, Thisline, Line$
Print line$
text.writeln output, Line$
next

text.close output


end


Report this post
Top
 Profile  
 
 Post subject: Re: Picture Based Encryption
Unread postPosted: Fri Apr 14, 2017 9:50 pm 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5518
Location: Paris, France
is it steganography?

_________________
- Creator of the Android BASIC! Compiler


Report this post
Top
 Profile  
 
 Post subject: Re: Picture Based Encryption
Unread postPosted: Sat Apr 15, 2017 3:02 am 
Offline
User avatar

Joined: Wed Jul 10, 2013 8:11 am
Posts: 325
@ Nicolas
I do not think so.
The bitmap is used as a key.
@ Iladutke
Very interresting program.
Think about
newvalue = BXOR (value , offset)
instead of
newvalue = value + offset
.

So you can use several images regardless of their sequence.

See also Gr.clip with RO_nexp = 5

Using UCODE could be also nice.

/ Gregor


Report this post
Top
 Profile  
 
 Post subject: Re: Picture Based Encryption
Unread postPosted: Sat Apr 15, 2017 7:59 am 
Offline

Joined: Sat Jul 04, 2015 7:28 am
Posts: 40
I was originally thinking of modifying the original image to store the message and then extracting it by comparing the two images. Unfortunately, it takes a lot longer to modify the bitmap pixel by pixel.

How would you reverse BXOR?


Report this post
Top
 Profile  
 
 Post subject: Re: Picture Based Encryption
Unread postPosted: Sat Apr 15, 2017 9:48 am 
Offline
User avatar

Joined: Tue Jan 03, 2012 9:31 am
Posts: 5518
Location: Paris, France
reverse BXOR = BXOR :)
a BXOR b BXOR b = a

_________________
- Creator of the Android BASIC! Compiler


Report this post
Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
suspicion-preferred