Platforms to show: All Mac Windows Linux Cross-Platform
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/AES CBC Test
This example is the version from Thu, 4th Sep 2013.
Project "AES CBC Test.xojo_binary_project"
Class AES_Test Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
Class frmMain Inherits Window
Control btnDecrypt Inherits BevelButton
ControlInstance btnDecrypt Inherits BevelButton
EventHandler Sub Action()
DecryptFile
End EventHandler
End Control
Control btnRoundTrip Inherits BevelButton
ControlInstance btnRoundTrip Inherits BevelButton
EventHandler Sub Action()
EncryptFile
DecryptFile
End EventHandler
End Control
Control txBlockSize Inherits TextField
ControlInstance txBlockSize Inherits TextField
End Control
Control lbBlockSize Inherits Label
ControlInstance lbBlockSize Inherits Label
End Control
Control lbFileName Inherits Label
ControlInstance lbFileName Inherits Label
End Control
Control txFileName Inherits TextField
ControlInstance txFileName Inherits TextField
End Control
Control btnEncrypt Inherits BevelButton
ControlInstance btnEncrypt Inherits BevelButton
EventHandler Sub Action()
EncryptFile
End EventHandler
End Control
Control txExtension Inherits TextField
ControlInstance txExtension Inherits TextField
End Control
EventHandler Sub Close()
quit
End EventHandler
Function DecryptAES(key as string, iv as string, SourceFile as FolderItem, DestFile as FolderItem, optional Blocksize as Integer = 160000) As Boolean
// decrypt file with AES and CBC
BlockSize = BlockSize - (BlockSize mod 16)
if BlockSize <=0 then
BlockSize = 16
end
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 MemoryBlock = iv
dim newIV as new MemoryBlock(16)
dim FileLength as int64 = SourceFile.Length
dim BytesToProcess as int64 = 0
dim aktPos as int64 = 0
dim aktBlockLength as int64
dim data as string =""
do
aktPos = bi.Position
BytesToProcess = FileLength - aktPos
data = bi.Read(BlockSize)
dim idata as MemoryBlock = data
// pick last 16 bytes of encrypted data as initialization vector of next block
newIV = idata.RightB(16)
aktBlockLength = LenB(idata)
if aktBlockLength > 0 then
a.DecryptCBC(idata, lenb(data), IVector, nil, 0, 0)
// assign initialization vector for next block
IVector = newIV
if BytesToProcess <= BlockSize then
// Last block
// check padding byte
dim wegDamit as integer = asc(idata.RightB(1))
// copy bytes without padding
if BytesToProcess - wegDamit > 0 then
dim RestData as string = idata.LeftB(BytesToProcess - wegDamit)
bo.Write RestData
end if
else
// write decrypted bytes
bo.Write idata
end if
end if
loop until aktBlockLength = 0
bo.Close
bi.Close
Return true
End Function
Sub DecryptFile()
dim key as string = "1234567890123456ABCDEFGHIJKLMNOP"
dim iv as string = "6543210987654321"
dim Filename as string = txFileName.Text
dim Extension as string = "." + txExtension.Text
dim sourceFile as FolderItem = SpecialFolder.Desktop.Child(Filename +".aes")
dim decryptedFile as FolderItem = SpecialFolder.Desktop.Child(Filename +"_decrypted" + Extension)
if sourceFile.Exists = false then
MsgBox "File not found: " + sourceFile.Name
return
end if
if DecryptAES(key, iv, sourceFile, decryptedFile) then
MsgBox "Fertig !"
end if
End Sub
Function EncryptAES(key as string, iv as string, sourcefile as FolderItem, destfile as FolderItem, optional BlockSize as integer = 160000) As Boolean
// encrypt file with AES and CBC
BlockSize = BlockSize - (BlockSize mod 16)
if BlockSize <=0 then
BlockSize = 16
end
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 MemoryBlock = iv
dim newIV as new MemoryBlock(16)
dim FileLength as int64 = SourceFile.Length
dim BytesToProcess as int64 = 0
dim aktPos as int64 = 0
dim aktBlockLength as int64
dim data as string =""
do
aktPos = bi.Position
BytesToProcess = FileLength - aktPos
data = bi.Read(BlockSize)
dim idata as MemoryBlock = data
dim PaddingValue as integer = 16 - (BytesToProcess Mod 16)
aktBlockLength = LenB(idata)
if aktBlockLength > 0 then
if BytesToProcess <= BlockSize then
// add padding for last block
dim RestData as new MemoryBlock(BytesToProcess + PaddingValue)
RestData = idata.LeftB(BytesToProcess) + new MemoryBlock(PaddingValue)
for i as integer = BytesToProcess to BytesToProcess + PaddingValue -1
RestData.byte(i) = PaddingValue
next i
a.EncryptCBC(RestData, BytesToProcess + PaddingValue, IVector, nil, 0, 0)
bo.Write RestData
else
a.EncryptCBC(idata, lenb(data), IVector, nil, 0, 0)
bo.Write idata
// Take last bytes as initialization vector of next block
IVector = idata.RightB(16)
end if
end if
loop until aktBlockLength = 0
bo.Close
bi.Close
Return true
End Function
Sub EncryptFile()
dim key as string = "1234567890123456ABCDEFGHIJKLMNOP"
dim iv as string = "6543210987654321"
dim Filename as string = txFileName.Text
dim Extension as string = "." + txExtension.Text
dim sourceFile as FolderItem = SpecialFolder.Desktop.Child(Filename+Extension)
dim encryptedFile as FolderItem = SpecialFolder.Desktop.Child(Filename+".aes")
if sourceFile.Exists = false then
MsgBox "File not found: " + Filename + extension
return
end if
if EncryptAES(key, iv, sourceFile, encryptedFile) then
MsgBox "Fertig!"
end if
End Sub
Function NewAES(key as string) As AESMBS
dim a as new AESMBS
// all keys should be UTF-8
key = ConvertEncoding(key, encodings.ascii)
//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
End MenuBar
End Project
See also:
The items on this page are in the following plugins: MBS Encryption Plugin.