Platforms to show: All Mac Windows Linux Cross-Platform
/Encryption/AES/AESMBS example
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/AESMBS example
This example is the version from Mon, 1st Sep 2013.
Project "AESMBS example.xojo_binary_project"
FileTypes
Filetype text
End FileTypes
Class Window1 Inherits Window
Control EditField1 Inherits TextField
ControlInstance EditField1 Inherits TextField
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control StaticText2 Inherits Label
ControlInstance StaticText2 Inherits Label
End Control
Control StaticText3 Inherits Label
ControlInstance StaticText3 Inherits Label
End Control
Control StaticText4 Inherits Label
ControlInstance StaticText4 Inherits Label
End Control
Control EditField2 Inherits TextField
ControlInstance EditField2 Inherits TextField
End Control
Control EditField3 Inherits TextField
ControlInstance EditField3 Inherits TextField
End Control
Control EditField4 Inherits TextField
ControlInstance EditField4 Inherits TextField
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action()
dim temp as string
temp=EditField2.text
temp=DefineEncoding(temp,Encodings.ASCII)
dim result as string = encryptAES(temp, EditField1.text)
EditField3.text=result // data, key
temp=decryptAES(result, EditField1.text) // data, key
temp=DefineEncoding(temp,Encodings.ASCII)
EditField4.text=temp
End EventHandler
End Control
Function decryptAES(data as string, key as string) As String
dim temp as string
dim pad as integer
aESpreviousCipherText = ""
temp = decrypt_AES128(data, key)
//padding
pad=asc(right(temp,1))
if pad<16 then
temp=mid(temp,1,len(temp)-pad)
end if
//
return temp
End Function
Function decrypt_AES128(data as string, key as string) As String
dim d as string
dim temp as string
dim toDecrypt as string
dim output as string
dim t1, t2, t as integer
dim s as string
dim i, n,j as integer
d=decodeBase64MBS(data)
n=Ceil(lenB(d)/16)
for i=1 to n
toDecrypt = mid(d, 1+16*(i-1), 16)
temp=decrypt_AES128_16bytes(toDecrypt, key )
//In the cipher-block chaining (CBC) mode, each block of plaintext
// is XORed with the previous ciphertext block before being encrypted.
if i>1 then
s=""
for j=1 to 16
t1=ascb(midb(temp,j,1))
t2=ascb(midb(AESpreviousCipherText,j,1))
T=bitwise.bitXor(t1,t2) //XOR
s=s+CHRb(t)
next
temp=s
end if
//-----------------------------------------------------------------------------------
aESpreviousCipherText = toDecrypt
output=output+temp
next
return output
End Function
Function decrypt_AES128_16bytes(data as string, key as string) As String
dim a as AESMBS
dim mbKey as MemoryBlock
dim mbData as MemoryBlock
mbKey=NewMemoryBlock(20)
mbKey.CString(0)=key // 16 byte key for 128bit
a=new AESMBS
if a.SetKey(mbKey, 128) then
mbData = NewMemoryBlock(16)
mbData.StringValue(0,16) = data
a.Decrypt(mbData)
return mbData.StringValue(0,16)
end if
End Function
Function encryptAES(data as string, key as string) As String
// for new projects, better use AESMBS.EncryptString directly.
dim temp as string
aESpreviousCipherText = ""
temp = encrypt_AES128(data, key)
return temp
End Function
Function encrypt_AES128(data as string, key as string) As String
dim temp as string
dim output as string
dim toEncrypt as string
dim t1, t2, t as integer
dim s as string
dim i, n,j as integer
n=Ceil(lenB(data)/16)
for i=1 to n
toEncrypt = mid(data, 1+16*(i-1), 16)
//In the cipher-block chaining (CBC) mode, each block of plaintext
// is XORed with the previous ciphertext block before being encrypted.
if AESpreviousCipherText>"" then
s=""
for j=1 to 16
t1=ascb(midb(toEncrypt,j,1))
t2=ascb(midb(AESpreviousCipherText,j,1))
T=bitwise.bitXor(t1,t2) //XOR
s=s+CHRb(t)
next
toEncrypt=s
end if
//-----------------------------------------------------------------------------------
temp=encrypt_AES128_16bytes(toEncrypt, key )
AESpreviousCipherText = temp
output=output+temp
next
output=encodeBase64MBS(output,0,"")
return output
End Function
Function encrypt_AES128_16bytes(data as string, key as string) As String
dim a as AESMBS
dim mbKey as MemoryBlock
dim mbdata as MemoryBlock
dim l,ll as integer
dim toEncrypt as string
dim padding as integer
dim i as integer
l=lenB(data)
padding = l mod 16
//Pad the input with a padding string of between 1 and 15 bytes to make the total length an exact multiple of 16 bytes.
//The value of each byte of the padding string is set to the number of bytes added - i.e. 8 bytes of value 0x08,
//7 bytes of value 0x07, ..., 2 bytes of 0x02, or one byte of value 0x01.
padding = 16-padding //padding
if padding = 16 then
padding=0
end if
ll=l+padding
toEncrypt =data
if padding>0 then
for i=1 to padding
toEncrypt=toEncrypt+CHR(padding)
next
end if
mbKey=NewMemoryBlock(20)
mbKey.CString(0)=key // 16 byte key for 128bit
a=new AESMBS
if a.SetKey(mbKey, 128) then
mbdata=NewMemoryBlock(16)
mbData.StringValue(0,16)=toEncrypt
a.Encrypt(mbdata)
return mbData.StringValue(0,16)
end if
End Function
Property aESpreviousCipherText As string
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
End Class
End Project
The items on this page are in the following plugins: MBS Encryption Plugin.