Platforms to show: All Mac Windows Linux Cross-Platform

/Util/Executablefile Attachment with Blowfish


Required plugins for this example: MBS Encryption Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Util/Executablefile Attachment with Blowfish

This example is the version from Wed, 30th Jul 2019.

Project "Executablefile Attachment with Blowfish.xojo_binary_project"
FileTypes
Filetype text
End FileTypes
Class EditorWindow Inherits Window
Binding enablingBinder
Control EditField1 Inherits TextArea
ControlInstance EditField1 Inherits TextArea
End Control
Control CheckBox1 Inherits CheckBox
ControlInstance CheckBox1 Inherits CheckBox
End Control
Control EditField2 Inherits TextField
ControlInstance EditField2 Inherits TextField
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action() save End EventHandler
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Protected Sub save() dim s,p as string dim f,d as FolderItem d=GetSaveFolderItem("application/binary","document.exe") s=EditField1.text p=EditField2.text // password if CheckBox1.Value and p<>"" then // convert text to one encoding which is later restored p=ConvertEncoding(p,Encodings.UTF8) // add some magic word to check later s="WORD"+ConvertEncoding(s,Encodings.UTF8) s=Encrypt(s,p) s="PASS"+s // now the string begins with "PASS". If the password is later correct the "WORD" text will decrypt correctly. // of course XOR Encryption is extreme unsecure! end if f=app.ExecutableFile if ExecutableAttachment.CreateFile(d,f) then ExecutableAttachment.Write s ExecutableAttachment.Close end if End Sub
End Class
MenuBar MenuBar1
MenuItem UntitledMenu1 = ""
MenuItem FileMenu = "&File"
MenuItem FileQuit = "Quit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu0 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class App Inherits Application
EventHandler Sub Open() if not StartViewer then EditorWindow.Show end if End EventHandler
Private Function StartViewer() As boolean dim s as string dim p as string if ExecutableAttachment.OpenFile(app.ExecutableFile,false) then s=ExecutableAttachment.read if s<>"" then if left(s,4)="PASS" then PasswordDialog.ShowModal if PasswordDialog.ok then p=PasswordDialog.pass.text p=ConvertEncoding(p,Encodings.UTF8) s=midb(s,5) s=Decrypt(s,p) s=DefineEncoding(s,Encodings.UTF8) if StrComp(left(s,4),"WORD",0)=0 then s=mid(s,5) ViewerWindow.EditField1.text=s ViewerWindow.Show else MsgBox "Password was wrong." quit end if else quit end if else s=DefineEncoding(s,Encodings.UTF8) ViewerWindow.EditField1.text=s ViewerWindow.Show end if Return true end if end if End Function
End Class
Class ViewerWindow Inherits Window
Control EditField1 Inherits TextArea
ControlInstance EditField1 Inherits TextArea
End Control
End Class
Class PasswordDialog Inherits Window
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control Pass Inherits TextField
ControlInstance Pass Inherits TextField
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action() ok=true hide End EventHandler
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action() ok=false hide End EventHandler
End Control
Property ok As boolean
End Class
Module ExecutableAttachment
Protected Sub Close() // Closes stream to clean up stream=nil End Sub
Protected Function CreateFile(file as folderitem, template as folderitem) As boolean // Create a new file based on an exe file on disc. // file can be app.ExecutableFile dim bo as BinaryStream dim bi as BinaryStream if template<>nil and file<>Nil then bi=template.OpenAsBinaryFile(False) if bi<>nil then bo=file.CreateBinaryFile("application/binary") if bo<>nil then bo.Write bi.Read(bi.Length) return Init(bo,true) end if end if end if End Function
Protected Function CreateFile(file as folderitem, template as string) As boolean // creates file based on string // you can drop an exe file into the project as a binary data file // and use it for the template string dim bo as BinaryStream if template<>"" and file<>Nil then bo=file.CreateBinaryFile("application/binary") if bo<>nil then bo.Write template return Init(bo,true) end if end if End Function
Private Function Init(b as binaryStream, created as boolean) As boolean dim p as integer dim n as Integer b.Position=0 b.LittleEndian=true // All DOS applications have MZ on the beginning if b.ReadUInt8=77 then // M if b.ReadUInt8=90 then // Z if created then Position=b.Length stream=b Return true end if b.Position=b.Length-8 n=b.ReadInt32 if n=&h20975867 then // our magic value! p=b.ReadInt32 if p>100000 then position=p stream=b Return true end if else position=b.Length stream=b Return true end if end if end if End Function
Protected Function OpenFile(file as folderitem, Write as boolean) As boolean // opens a file // Write must be false if file is app.ExecutableFile dim b as BinaryStream if file<>Nil then b=file.OpenAsBinaryFile(write) if b<>nil then Return init(b,false) else System.DebugLog "OpenFile: failed to open "+file.name end if else System.DebugLog "OpenFile: file=nil" end if End Function
Protected Function Read() As String // Reads contents // Remember that text encoding is binary for this string! stream.Position=position return stream.Read(stream.Length-position-8) End Function
Protected Sub Write(s as string) // Writes stream.Position=position stream.Write s stream.WriteInt32 &h20975867 stream.WriteInt32 position stream.Length=stream.Position // cut away rest End Sub
Property Private position As integer
Property Private stream As binaryStream
End Module
Module Encryption
Function Decrypt(data as string, password as string) As string dim b as BlowfishMBS dim temp as MemoryBlock b=new BlowfishMBS b.SetKey password Return b.DecryptCFB64(data,temp) End Function
Function Encrypt(data as string, password as string) As string dim b as BlowfishMBS dim temp as MemoryBlock b=new BlowfishMBS b.SetKey password Return b.EncryptCFB64(data,temp) End Function
End Module
End Project

See also:

The items on this page are in the following plugins: MBS Util Plugin.


The biggest plugin in space...