Platforms to show: All Mac Windows Linux Cross-Platform
/Encryption/AES/Encrypt File with AES
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: /Encryption/AES/Encrypt File with AES
This example is the version from Mon, 6th Oct 2013.
Project "Encrypt File with AES.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
EventHandler Sub Open()
dim key as string = "Hello World"
dim sourceFile as FolderItem = SpecialFolder.Desktop.Child("test.tiff")
dim encryptedFile as FolderItem = SpecialFolder.Desktop.Child("test.encrypted")
dim decryptedFile as FolderItem = SpecialFolder.Desktop.Child("test2.tiff")
if sourceFile.Exists = false then
MsgBox "Please change file name in project for source file or add test.tiff to your desktop."
break
end if
if EncryptAES(key, sourceFile, encryptedFile) then
if DecryptAES(key, encryptedFile, decryptedFile) then
MsgBox "OK"
decryptedFile.Launch
else
MsgBox "Failed to decrypt."
end if
else
MsgBox "Failed to encrypt."
end if
End EventHandler
Function DecryptAES(key as string, sourcefile as FolderItem, destfile as FolderItem) As Boolean
// decrypt file with AES and CFB
dim a as AESMBS = NewAES(key)
if a = nil then Return false
dim bi as BinaryStream = sourcefile.OpenAsBinaryFile(false)
if bi = nil then Return false
dim bo as BinaryStream = destfile.CreateBinaryFile("")
if bo = nil then Return false
dim IVectorOffset as integer = 0
dim IVector as new MemoryBlock(16)
dim data as string = bi.Read(1000000)
while lenb(data)>0
dim idata as MemoryBlock = data
a.DecryptCFB128(idata, lenb(data), IVectorOffset, IVector, nil, 0, 0)
bo.Write idata
data = bi.Read(1000000)
wend
bo.Close
bi.Close
Return true
// exception handler for newer Real Studio version
'Exception io as ioexception
if bo<>nil then
destfile.Delete
end if
End Function
Function EncryptAES(key as string, sourcefile as FolderItem, destfile as FolderItem) As Boolean
// encrypt file with AES and CFB
dim a as AESMBS = NewAES(key)
if a = nil then Return false
dim bi as BinaryStream = sourcefile.OpenAsBinaryFile(false)
if bi = nil then Return false
dim bo as BinaryStream = destfile.CreateBinaryFile("")
if bo = nil then Return false
dim IVectorOffset as integer = 0
dim IVector as new MemoryBlock(16)
dim data as string = bi.Read(1000000)
while lenb(data)>0
dim idata as MemoryBlock = data
a.EncryptCFB128(idata, lenb(data), IVectorOffset, IVector, nil, 0, 0)
bo.Write idata
data = bi.Read(1000000)
wend
bo.Close
bi.Close
Return true
// exception handler for newer Real Studio version
'Exception io as ioexception
if bo<>nil then
destfile.Delete
end if
End Function
Function NewAES(key as string) As AESMBS
dim a as new AESMBS
// all keys should be UTF-8
key = ConvertEncoding(key, encodings.UTF8)
dim bits as integer = 0
dim l as integer = lenb(key)
// pad key to required length for 16, 24 or 32 bit key
if l>32 then
key = leftb(key,32)
elseif l>24 then
while lenb(key)<32
key = key + " "
wend
elseif l>16 then
while lenb(key)<24
key = key + " "
wend
else
while lenb(key)<16
key = key + " "
wend
end if
if a.SetKey(key) then
Return a
end if
End Function
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
End Project
The items on this page are in the following plugins: MBS Encryption Plugin.