Platforms to show: All Mac Windows Linux Cross-Platform

/Encryption/JWT RS 256


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/JWT RS 256

This example is the version from Wed, 14th Jul 2020.

Project "JWT RS 256.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control PrivateKey Inherits TextArea
ControlInstance PrivateKey Inherits TextArea
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control Password Inherits TextField
ControlInstance Password Inherits TextField
End Control
Control Label3 Inherits Label
ControlInstance Label3 Inherits Label
End Control
Control Header Inherits TextArea
ControlInstance Header Inherits TextArea
End Control
Control Label4 Inherits Label
ControlInstance Label4 Inherits Label
End Control
Control Payload Inherits TextArea
ControlInstance Payload Inherits TextArea
End Control
Control Label5 Inherits Label
ControlInstance Label5 Inherits Label
End Control
Control output Inherits TextArea
ControlInstance output Inherits TextArea
End Control
Control CalculateButton Inherits PushButton
ControlInstance CalculateButton Inherits PushButton
EventHandler Sub Action() // take values from fields and make sure encoding is right and line endings Dim Header As String = Self.Header.Text.ConvertEncoding(encodings.UTF8) Dim Payload As String = Self.Payload.Text.ConvertEncoding(encodings.UTF8) Dim PrivateKey As String = ReplaceLineEndings(Self.PrivateKey.Text, EndOfLine.UNIX).ConvertEncoding(encodings.UTF8) Dim Password As String = Self.Password.Text.ConvertEncoding(encodings.UTF8) // now prepare string to sign Dim EncodedString As String = EncodeBase64URLMBS(header) + "." + EncodeBase64URLMBS(Payload) // sign it with RSA key and SHA 256 hash Dim Signature As String = OpenSSLMBS.SignData(EncodedString, PrivateKey, Password, OpenSSLMBS.kAlgorithmSHA256) // encode signature Dim SignatureEncoded As String = EncodeBase64URLMBS(Signature) // and build JWT RS 256 signature Dim result As String = EncodedString + "." + SignatureEncoded output.Text = result End EventHandler
End Control
Control VerifyButton Inherits PushButton
ControlInstance VerifyButton Inherits PushButton
EventHandler Sub Action() // take values from fields and make sure encoding is right and line endings Dim PrivateKey As String = ReplaceLineEndings(Self.PrivateKey.Text, EndOfLine.UNIX).ConvertEncoding(encodings.UTF8) Dim Password As String = Self.Password.Text.ConvertEncoding(encodings.UTF8) // check if we have a dot in the input. Dim t As String = output.Text.ConvertEncoding(encodings.UTF8) Dim p As Integer = InStr(t, ".") If p = 0 Then MsgBox "invalid text?" Return End If // split JWT into three parts: Dim Header As String = NthField(t, ".", 1) Dim Payload As String = NthField(t, ".", 2) Dim Signature As String = NthField(t, ".", 3) // define the signed portion Dim EncodedString As String = Header + "." + Payload // decode all three parts Signature = DecodeBase64URLMBS(Signature) Header = DecodeBase64URLMBS(Header) Payload = DecodeBase64URLMBS(Payload) // verify signature If OpenSSLMBS.VerifyData(EncodedString, signature, PrivateKey, Password, OpenSSLMBS.kAlgorithmSHA256) Then // show the valid data: MsgBox header MsgBox Payload Else MsgBox "Verification failed." End If End EventHandler
End Control
Note "Get Key"
You can create key in Terminal with commands like this: ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key
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.


The biggest plugin in space...