Example: /Encryption/Twofish

Online Documentation   -   Statistics   -   FAQ   -   Plugin Parts (All, Dependencies)   -   Class hierarchy

New in Version 22.2 22.3 22.4 22.5 23.0 23.1 23.2 23.3 23.4 23.5 24.0 24.1

The list of the   topics,   classes,   interfaces,   controls,   modules,   global methods by category,   global methods by name,   screenshots,   licenses   and   examples.

Platforms to show: All Mac Windows Linux Cross-Platform

/Encryption/Twofish


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/Twofish

This example is the version from Thu, 14th Dec 2016.

Project "Twofish.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
EventHandler Sub Open() TestCBC TestECB End EventHandler
Function DecryptECB(key as string, input as string) As string dim k as string = DecodeHex(key) dim i as string = DecodeHex(input) dim e as string = TwofishMBS.DecryptECB(k, i) dim r as string = EncodeHex(e) return r End Function
Function EncryptECB(key as string, input as string) As string dim k as string = DecodeHex(key) dim i as string = DecodeHex(input) #if DebugBuild then dim ilen as integer = i.Len dim klen as integer = k.len #endif dim e as string = TwofishMBS.EncryptECB(k, i) #if DebugBuild then dim elen as integer = e.Len #endif dim r as string = EncodeHex(e) return r End Function
Sub TestCBC() dim ss as string = "00000000000000000000000000000000" dim s as string = DecodeHex(ss) dim e as string = TwofishMBS.EncryptCBC(s, s, s) dim d as string = TwofishMBS.DecryptCBC(s, e, s) dim es as string = EncodeHex(e) dim ds as string = EncodeHex(d) if ds <> ss then Break MsgBox "CBC failed" Return end if testCBC "8GSgXLqVr5fuRSAAGC2I6oI3CCeAR7vW", "Hello cruel World!", "0000000000000000", "e6QqlT2ewFnHQYazxRKdBZzGb0xJAOL9C1n6V+81fKKfgQqtY3pZa1pxaEUr7rid" testCBC "8GSgXLqVr5fuRSAAGC2I6oI3CCeAR7vW", "Hello cruel World!", "0000000000000001", "pSm5oC+FP5MWAwHq/xPn14dAPyUAbKJUzEqGs5ZhsYflVrdsQHHTsbPd3Ojo8QdU" testCBC "8GSgXLqVr5fuRSAAGC2I6oI3CCeAR7vW", "Hello cruel World!", "1000000000000000", "KjmHjeqq8RM5Ogpa+6xT7vLwbvJcgPgH+uTQ7lGcAo3FZnOj/z6j5KBbFC51NjLg" testCBC "G8SgXLqVr5fuRSAAGC2I6oI3CCeAR7vW", "Hello cruel World!", "0000000000000000", "BzB/zSxNBWfbVgg2VBQP1SpbM7NWfBYBuS62JRAQQPXtlDbqSwfRlBcs4/+7Y99z" testCBC "G8SgXLqVr5fuRSAAGC2I6oI3CCeAR7vW", "Hello cruel World!", "0000000000000001", "PYuIRks1LUBrAaDvqnaqGm0x78VthKQRW6bYLTS0X82Minj9s8ro8SxlaeW7uCRM" testCBC "G8SgXLqVr5fuRSAAGC2I6oI3CCeAR7vW", "Hello cruel World!", "1000000000000000", "X9BsmQDsO2GOdj7f6du3/fxNo5Tdwh+5NxNs3076ttmkQLxHPEdvSItlMnIY3DkN" End Sub
Sub TestECB() // test vectors from website: // https://www.schneier.com/code/ecb_ival.txt dim key, soll, input, encrypted, decrypted, lastInput, lastKey as string key = "00000000000000000000000000000000" input = "00000000000000000000000000000000" soll = "9F589F5CF6122C32B6BFEC2F2AE8C35A" encrypted = EncryptECB(key, input) decrypted = DecryptECB(key, encrypted) if encrypted = soll and decrypted = input then // ok else break MsgBox "ECB failed" Return end if key = "0123456789ABCDEFFEDCBA98765432100011223344556677" input = "00000000000000000000000000000000" soll = "CFD1D2E5A9BE9CDF501F13B892BD2248" encrypted = EncryptECB(key, input) decrypted = DecryptECB(key, encrypted) if encrypted = soll and decrypted = input then // ok else break MsgBox "ECB failed" Return end if key = "0123456789ABCDEFFEDCBA987654321000112233445566778899AABBCCDDEEFF" input = "00000000000000000000000000000000" soll = "37527BE0052334B89F0CFCCAE87CFA20" encrypted = EncryptECB(key, input) decrypted = DecryptECB(key, encrypted) if encrypted = soll and decrypted = input then // ok else break MsgBox "ECB failed" Return end if // test 49 rounds, 128 bits key = "00000000000000000000000000000000" input = "00000000000000000000000000000000" for i as integer = 1 to 49 lastInput = input lastkey = key encrypted = EncryptECB(key, input) decrypted = DecryptECB(key, encrypted) if decrypted = input then // ok else break // failed MsgBox "ECB failed" Return end if key = lastInput input = encrypted next if encrypted = "5D9D4EEFFA9151575524F115815A12E0" and lastkey = "BCA724A54533C6987E14AA827952F921" then // ok else break MsgBox "ECB failed" Return end if // test 49 rounds, 192 bits key = "000000000000000000000000000000000000000000000000" input = "00000000000000000000000000000000" for i as integer = 1 to 49 lastInput = input lastkey = key encrypted = EncryptECB(key, input) decrypted = DecryptECB(key, encrypted) if decrypted = input then // ok else break // failed MsgBox "ECB failed" Return end if key = lastInput + left(key, 16) input = encrypted next if encrypted = "E75449212BEEF9F4A390BD860A640941" and lastkey = "FB66522C332FCC4C042ABE32FA9E902FDEA4F3DA75EC7A8E" then // ok else break MsgBox "ECB failed" Return end if // test 49 rounds, 256 bits key = "0000000000000000000000000000000000000000000000000000000000000000" input = "00000000000000000000000000000000" for i as integer = 1 to 49 lastInput = input lastkey = key encrypted = EncryptECB(key, input) decrypted = DecryptECB(key, encrypted) if decrypted = input then // ok else break // failed MsgBox "ECB failed" Return end if key = lastInput + left(key, 32) input = encrypted next if encrypted = "37FE26FF1CF66175F5DDF4C33B97A205" and lastkey = "248A7F3528B168ACFDD1386E3F51E30C2E2158BC3E5FC714C1EEECA0EA696D48" then // ok else break MsgBox "ECB failed" Return end if // all okay MsgBox "All okay, if you came here without errors." End Sub
Sub testCBC(Key as string, Input as String, IV as String, EncryptedBase64 as string) dim prefix as string = "0000000000000000" // salt // add padding to 16 byte block sizes dim ModResult as integer = 16 - input.len mod 16 dim Padded as string = prefix + input if ModResult = 0 then ModResult = 16 end if for i as integer = 1 to ModResult padded = padded + chrb(ModResult) next // encrypt dim Encrypted as String = TwofishMBS.EncryptCBC(key, padded, IV) // decrypt dim Decrypted as string = TwofishMBS.DecryptCBC(key, Encrypted, IV) // remove zeros Decrypted = ReplaceAllb(Decrypted, chr(0), "") dim n as integer = asc(Rightb(Decrypted,1)) // remove passing Decrypted = Decrypted.leftb(Decrypted.lenb-n) // remove salt prefix Decrypted = midb(Decrypted, lenb(prefix)+1) // set encodings Decrypted = DefineEncoding(Decrypted, encodings.UTF8) // match? dim Base64 as string = EncodeBase64(Encrypted) if Base64 <> EncryptedBase64 then break elseif Decrypted = input then // ok else Break MsgBox "CBC failed" Return end if End Sub
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
End Project

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


💬 Ask a question or report a problem