It is currently Tue Jun 27, 2017 10:29 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  [ 1 post ] 
Author Message
 Post subject: Improved Picture-Based Encryption Program
Unread postPosted: Sun Apr 16, 2017 6:25 pm 
Offline

Joined: Sat Jul 04, 2015 7:28 am
Posts: 51
I have added the interface to choose files.

INCLUDE "GW.bas"

Fn.Def Extension (FileName$, Type$)
Correct = 1
Extension$ = Upper$ (Right$ (FileName$, 3))
If Extension$ <> "JPG" & Extension$ <> "PNG" & Type$ = "Graphics" Then Correct = 0
If Extension$ <> "TXT" & Type$ = "Text" Then Correct = 0
If Extension$ <> "BIN" & Type$ = "Binary" Then Correct = 0
Fn.Rtn Correct
Fn.End

Fn.Def PickFile$(Type$)
flag = 0
file.dir "", Existing$[]
Array.length filecount, Existing$[]
list.create s, allfiles
for count = 1 to filecount
if Extension(Existing$[count], Type$) = 1 then list.add allfiles, Existing$[count]
next
list.size allfiles, filecount
list.toarray allfiles, FilesofType$[]
Array.length filecount, Filesoftype$[]
if filecount > 0 then Filesoftype$[1] = ">" + Filesoftype$[1]
FileSelect = GW_NEW_PAGE()
Message$ = "Select a " + Type$ + " File:"
GW_ADD_TITLEBAR (FileSelect,Message$)
myselbx = GW_ADD_SELECTBOX(FileSelect, "" ,FilesofType$[])
GW_ADD_LISTENER (FileSelect, myselbx, "Submit", "Submit")
inpaxn3 = GW_ADD_SUBMIT (FileSelect, "Select")
GW_RENDER (FileSelect)
r$=GW_WAIT_ACTION$()
if is_in (GW_ID$(inpaxn3), r$) then
File = GW_GET_VALUE(myselbx)
If File < 1 then File = 1
File$ = FilesofType$[File]
EndIf
GW_CLOSE_PAGE (FileSelect)
Fn.Rtn File$
FN.End

Fn.Def Encrypt(character$, image, x, y)
Gr.Get.bmpixel image, x, y, alpha, red, green, blue
color = mod(x, 3)
if color = 0 then
offset = red
ElseIf color = 1 then
offset = green
Else
offset = blue
endif
%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
color = mod(x, 3)
if color = 0 then
offset = red
ElseIf color = 1 then
offset = green
Else
offset = blue
endif
%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

MainPage = GW_NEW_PAGE()
GW_USE_THEME_CUSTO_ONCE ("fit-screen")
GW_ADD_TITLEBAR (Mainpage, "Picture-Based Encryption")
GW_ADD_BUTTON (MainPage, "Encrypt your message", "Encrypt File")
GW_ADD_BUTTON (MainPage, "Decrypt your message", "Decrypt File")
GW_ADD_BUTTON (MainPage, "About", "About")
GW_ADD_BUTTON (MainPage, "Exit", "Exit")

MainLoop:

GW_RENDER (MainPage)
r$ = GW_WAIT_ACTION$()

If r$ = "Encrypt File" then
GW_CLOSE_PAGE (MainPage)
Goto EncryptFile
EndIf

If r$ = "Decrypt File" then
GW_CLOSE_PAGE (MainPage)
Goto DecryptFile
EndIf

If r$ = "About" then
GW_CLOSE_PAGE (MainPage)
Goto About
EndIf

If r$ = "Exit" then
GW_CLOSE_PAGE (MainPage)
Goto Leave
EndIf

Goto MainLoop

EncryptFile:

KeyFile$ = Pickfile$("Graphics")
InFile$ = Pickfile$("Text")


Do
Input "Enter file name for encrypted message.", OutFile$, "Message.bin", cancel
If Extension (OutFile$, "B") = False then Outfile$ = Outfile$ + ".bin"
File.exists taken, OutFile$
if taken <> 0 then
Message$ = OutFile$ + " exists. Do you want to replace it?"
Dialog.message Message$, "", decision, "Yes", "No"
if decision = 1 then taken = 0
endif
Until taken = 0 & OutFile$ <> ""

CodePage = GW_NEW_PAGE()
GW_ADD_TITLEBAR (CodePage, "Picture-Based Encryption")
Message$ = "The following message has been encrypted as " +chr$(34) + OutFile$ + chr$(34) +"."
GW_ADD_TEXT (Codepage, Message$)
GW_ADD_BUTTON (CodePage, "Main Menu", "Main Menu")



text.open r, source, InFile$

list.create s, plaintext



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

if line$ <> "EOF" then
GW_ADD_TEXT (CodePage, Line$)
list.add plaintext, line$
EndIf

until line$ = "EOF"

text.close source

gr.open
Gr.orientation 1
GR.COLOR ,0,0,255,2
GR.TEXT.SIZE 50
GR.TEXT.ALIGN 2
Gr.screen width, height
Middle = int(width / 2)
gr.text.draw dummy, Middle, 100, "Encrypting the message."
gr.text.draw dummy, Middle, 200, "Please wait."

gr.bitmap.load key, KeyFile$ %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, OutFile$

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
gr.close


GW_RENDER (CodePage)
r$=GW_WAIT_ACTION$()
GW_CLOSE_PAGE (CodePage)
Goto MainLoop


DecryptFile:
KeyFile$ = PickFile$("Graphics")
InFile$ = PickFile$("Binary")
Do
Input "Enter file name for decrypted message.", OutFile$, "DecryptedMessage.txt", cancel
If Extension (OutFile$, "T") = False then Outfile$ = Outfile$ + ".txt"
File.exists taken, OutFile$
if taken <> 0 then
Message$ = OutFile$ + " exists. Do you want to replace it?"
Dialog.message Message$, "", decision, "Yes", "No"
if decision = 1 then taken = 0
endif
Until taken = 0 & OutFile$ <> ""


DecodePage = GW_NEW_PAGE()
GW_ADD_TITLEBAR (DecodePage, "Picture-Based Encryption")
Message$ = "The following message has been decrypted as " + Chr$(34) + OutFile$ + Chr$(34) +"."
GW_ADD_TEXT (Decodepage, Message$)
GW_ADD_BUTTON (DecodePage, "Main Menu", "Main Menu")

%Decode the message!

gr.open
Gr.orientation 1
GR.COLOR ,0,0,255,2
GR.TEXT.SIZE 50
GR.TEXT.ALIGN 2
Gr.screen width, height
Middle = int(width / 2)
gr.text.draw dummy, Middle, 100, "Decrypting the message."
gr.text.draw dummy, Middle, 200, "Please wait."
gr.bitmap.load key, KeyFile$
gr.bitmap.size key, picx, picy
Text.open w, output, OutFile$
byte.open r, input, InFile$

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 DecodedChar$ = Chr$(254) then
list.add outlines, message$
message$ = ""
Else
message$ = message$ + DecodedChar$
EndIf

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

GW_ADD_TEXT (DecodePage, "Decrypted Message:")


For ThisLine = 1 to lines %save the decoded message
list.get outlines, Thisline, Line$
GW_ADD_TEXT (DecodePage, Line$)
text.writeln output, Line$
next

text.close output
gr.bitmap.delete key
gr.close


GW_RENDER (DecodePage)
r$=GW_WAIT_ACTION$()
GW_CLOSE_PAGE (DecodePage)
Goto MainLoop

About:
Info = GW_NEW_PAGE()
GW_ADD_TITLEBAR (Info, "Picture-Based Encryption")
GW_ADD_BUTTON (Info, "Main Menu", "Main Menu")
GW_ADD_TEXT(Info,"by Larry Ladutke")
GW_ADD_TEXT(Info,"This program uses a graphics file as a key to encrypt a text message as a binary file. The exact same graphics file must be used to decrypt the binary file back into the original text message.")
GW_ADD_TEXT(Info,"The encrypted file is saved as a binary file. The decrypted message is saved as text. The original message and the key must be in the default directory. The encrypted and decrypted messages will be saved in the default directory.")
GW_RENDER (Info)
r$=GW_WAIT_ACTION$()
GW_CLOSE_PAGE (Info)
Goto MainLoop

Leave:
End


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


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