Platforms to show: All Mac Windows Linux Cross-Platform
/Util/AES/AESMBS example
Feedback.
Function:
You find this example project in your Plugins Download as a Realbasic project file within the examples folder: /Util/AES/AESMBS example
This example is the version from Fri, 14th May 2009.
Notes: Last modified: Fri, 14th May 2009
Feedback.
Function:
You find this example project in your Plugins Download as a Realbasic project file within the examples folder: /Util/AES/AESMBS example
This example is the version from Fri, 14th May 2009.
Notes: Last modified: Fri, 14th May 2009
Class Window1
Inherits Window
// Controls
ControlInstance
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
End ControlInstance
ControlInstance
Sub Action() Handles Event
dim temp as string
temp=EditField2.text
temp=DefineEncoding(temp,Encodings.ASCII)
EditField3.text=encryptAES(temp, EditField1.text) // data, key
temp=decryptAES(EditField3.text, EditField1.text) // data, key
temp=DefineEncoding(temp,Encodings.UTF8)
EditField4.text=temp
End Sub
End ControlInstance
// Properties
Dim aESpreviousCipherText As string
// Methods
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=asc(mid(temp,j,1))
t2=asc(mid(AESpreviousCipherText,j,1))
T=bitwise.bitXor(t1,t2) //XOR
s=s+CHR(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
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=asc(mid(toEncrypt,j,1))
t2=asc(mid(AESpreviousCipherText,j,1))
T=bitwise.bitXor(t1,t2) //XOR
s=s+CHR(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
End Class
Class App
Inherits Application
End Class