Platforms to show: All Mac Windows Linux Cross-Platform

/Encryption/AES/AES256 Text encryption


Required plugins for this example: MBS Util Plugin, MBS Encryption Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Encryption/AES/AES256 Text encryption

This example is the version from Mon, 16th Jul 2017.

Project "AES256 Text encryption.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control TextArea1 Inherits TextArea
ControlInstance TextArea1 Inherits TextArea
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control TextArea2 Inherits TextArea
ControlInstance TextArea2 Inherits TextArea
End Control
Control Label3 Inherits Label
ControlInstance Label3 Inherits Label
End Control
Control TextArea3 Inherits TextArea
ControlInstance TextArea3 Inherits TextArea
End Control
Control Label4 Inherits Label
ControlInstance Label4 Inherits Label
End Control
Control Password1 Inherits TextField
ControlInstance Password1 Inherits TextField
End Control
Control Label5 Inherits Label
ControlInstance Label5 Inherits Label
End Control
Control Password2 Inherits TextField
ControlInstance Password2 Inherits TextField
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action() dim r as string = EncodeHex(RandomBytesStringMBS(16)) Password1.text = r Password2.text = r End EventHandler
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action() TextArea2.text = EncryptionModule.Encrypt(Password1.text, TextArea1.text) End EventHandler
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action() TextArea3.text = EncryptionModule.Decrypt(Password2.text, TextArea2.text) End EventHandler
End Control
Control Label6 Inherits Label
ControlInstance Label6 Inherits Label
End Control
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
Module EncryptionModule
Protected Function Decrypt(Key as String, Text as String) As string // AES 256 plus good key generation // returns Base64, so you can store in text field in database dim Input as String = DecodeBase64(text) if lenb(input) < 8 then // no salt? Return "" end if key = ConvertEncoding(key, encodings.UTF8) dim iKey as MemoryBlock = key dim salt as MemoryBlock = leftb(input, 8) const RoundNumbers = 1000 dim CKey as MemoryBlock dim CIV as MemoryBlock if CipherMBS.BytesToKey(CipherMBS.aes_256_cfb128, DigestMBS.SHA512, salt, key, RoundNumbers, CKey, CIV) then // last 64 bytes are hash dim CheckHash as string = input.RightB(64) Input = Input.midb(9, lenb(input) - 64 - 8) dim c as CipherMBS = CipherMBS.aes_256_cfb128 call c.DecryptInit Ckey, CIV dim output as string = c.ProcessString(Input) + c.FinalizeAsString dim outputHash as string = SHA512MBS.Hash(output) if StrCompBytesMBS(outputHash, CheckHash) = 0 then // ok, bytes are same in hash if encodings.UTF8.IsValidData(output) then // ok dim content as string = DefineEncoding(output, encodings.UTF8) Return content else // text encoding error? Maybe wrong key? Break end if else // hash doesn't work, so wrong key! break end if else // failed to make key break end if End Function
Protected Function Encrypt(Key as String, Text as String) As string // AES 256 plus good key generation // returns Base64, so you can store in text field in database Text = ConvertEncoding(Text, encodings.UTF8) key = ConvertEncoding(key, encodings.UTF8) // quick hash so we can later check if data is correct dim Hash as string = SHA512MBS.Hash(text) dim iKey as MemoryBlock = key dim salt as MemoryBlock = RandomBytesStringMBS(8) const RoundNumbers = 1000 dim CKey as MemoryBlock dim CIV as MemoryBlock if CipherMBS.BytesToKey(CipherMBS.aes_256_cfb128, DigestMBS.SHA512, salt, key, RoundNumbers, CKey, CIV) then dim c as CipherMBS = CipherMBS.aes_256_cfb128 call c.EncryptInit Ckey, CIV dim output as string = salt + c.ProcessString(Text) + c.FinalizeAsString + Hash Return EncodeBase64(output) else // failed to make key break end if End Function
End Module
End Project

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


The biggest plugin in space...