home *** CD-ROM | disk | FTP | other *** search
- VERSION 5.00
- Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
- Begin VB.Form frmSigner
- BorderStyle = 3 'Fixed Dialog
- Caption = "Signer"
- ClientHeight = 5655
- ClientLeft = 45
- ClientTop = 330
- ClientWidth = 8580
- LinkTopic = "Form1"
- MaxButton = 0 'False
- MinButton = 0 'False
- ScaleHeight = 5655
- ScaleWidth = 8580
- StartUpPosition = 2 'CenterScreen
- Begin VB.CommandButton cmdOption
- Caption = "Option"
- Height = 315
- Left = 150
- TabIndex = 8
- Top = 5250
- Width = 1140
- End
- Begin VB.Frame fraMessageSigning
- Caption = "Frame2"
- Height = 2265
- Left = 150
- TabIndex = 11
- Top = 900
- Width = 8340
- Begin VB.OptionButton optMessageSigning
- Caption = "Message signing"
- Height = 240
- Left = 75
- TabIndex = 3
- Top = 0
- Width = 1515
- End
- Begin VB.TextBox txtMessageToSign
- Height = 1635
- Left = 150
- MultiLine = -1 'True
- OLEDropMode = 1 'Manual
- ScrollBars = 2 'Vertical
- TabIndex = 4
- Top = 525
- Width = 8115
- End
- Begin VB.Label lbl
- Caption = "Message to sign"
- Height = 240
- Index = 2
- Left = 150
- TabIndex = 13
- Top = 300
- Width = 1290
- End
- End
- Begin VB.Frame fraFileSigning
- Height = 690
- Left = 150
- TabIndex = 10
- Top = 75
- Width = 8340
- Begin VB.TextBox txtFileToSign
- Height = 285
- Left = 1500
- OLEDropMode = 1 'Manual
- TabIndex = 1
- Top = 300
- Width = 6390
- End
- Begin VB.CommandButton cmdSelectFile
- Caption = "..."
- Height = 285
- Left = 7950
- TabIndex = 2
- Top = 300
- Width = 285
- End
- Begin VB.OptionButton optFileSigning
- Caption = "File signing"
- Height = 240
- Left = 75
- TabIndex = 0
- Top = 0
- Width = 1140
- End
- Begin VB.Label lbl
- Caption = "File to sign"
- Height = 240
- Index = 0
- Left = 150
- TabIndex = 14
- Top = 300
- Width = 1290
- End
- End
- Begin VB.Frame fraSignature
- Caption = "Signature"
- Height = 1440
- Left = 150
- TabIndex = 12
- Top = 3750
- Width = 8340
- Begin VB.TextBox txtSignature
- Height = 1065
- Left = 150
- MultiLine = -1 'True
- ScrollBars = 2 'Vertical
- TabIndex = 7
- Top = 300
- Width = 8115
- End
- End
- Begin VB.CommandButton cmdQuit
- Cancel = -1 'True
- Caption = "Quit"
- Height = 315
- Left = 7350
- TabIndex = 9
- Top = 5250
- Width = 1140
- End
- Begin VB.CommandButton cmdVerify
- Caption = "Verify"
- Height = 315
- Left = 7350
- TabIndex = 6
- Top = 3300
- Width = 1140
- End
- Begin VB.CommandButton cmdSign
- Caption = "Sign"
- Height = 315
- Left = 6150
- TabIndex = 5
- Top = 3300
- Width = 1140
- End
- Begin MSComDlg.CommonDialog dlgCommon
- Left = 1875
- Top = 5175
- _ExtentX = 847
- _ExtentY = 847
- _Version = 393216
- End
- Attribute VB_Name = "frmSigner"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_PredeclaredId = True
- Attribute VB_Exposed = False
- ' Xceed Encryption Library - Signer sample
- ' Copyright (c) 2001 Xceed Software Inc.
- ' [Signer.frm]
- ' This form module contains the main form's code. It demonstrates how to
- ' Sign a chunk of memory data or a file and Verify a file or memory data against
- ' a signature. It specifically uses:
- ' - The Sign, Verify, ReadFile and SetRandomKeyPair methods.
- ' - The PrivateKey, PublicKey, Signature and SigningMethod properties.
- ' This file is part of the Xceed Encryption Library sample applications.
- ' The source code in this file is only intended as a supplement to Xceed
- ' Encryption Library's documentation, and is provided "as is", without
- ' warranty of any kind, either expressed or implied.
- Option Explicit
- 'The values chosen by the user in the Option form
- Private m_sPrivateKeyFile As String
- Private m_sPublicKeyFile As String
- '====================================================================================
- ' EVENTS - triggered by the form and its controls
- '====================================================================================
- '------------------------------------------------------------------------------------
- 'Do the Signing of the message or file
- '------------------------------------------------------------------------------------
- Private Sub cmdSign_Click()
- Dim xSigner As XceedSigning
- Dim vaMessageToSign As Variant
- Dim lErrorNumber As Long
- Dim sSignature As String
- 'Create an instance of the Xceed Signing
- Set xSigner = New XceedSigning
- lErrorNumber = 0
- On Error Resume Next
- 'Set the private key that will be used to calculate the signature.
- 'We don't have to set the public key for signing.
- xSigner.SigningMethod.PrivateKey = ReadKeyFile(m_sPrivateKeyFile)
- If Err.Number <> 0 Then
- lErrorNumber = Err.Number
- Call MsgBox("Error during PrivateKey initialization." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
- End If
- On Error GoTo 0
- If lErrorNumber = 0 Then
- If optFileSigning.Value Then
- 'Calculate the signature of the specified file
- On Error Resume Next
- 'Sign the file using the ReadFile method
- Call xSigner.ReadFile(txtFileToSign.Text, 0, 0, efpSign, True)
- If Err.Number <> 0 Then
- lErrorNumber = Err.Number
- Call MsgBox("Error during ReadFile." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
- End If
- On Error GoTo 0
- Else
- 'Calculate the signature of the specified message
-
- 'Convert unicode text to an ASCII string as the message will,
- 'most probably, be sent in ASCII format.
- vaMessageToSign = StrConv(txtMessageToSign.Text, vbFromUnicode)
-
- On Error Resume Next
- 'Sign the ascii string, specifying that (True parameter)
- 'this is the end of data (there will be no more calls to Sign).
- Call xSigner.Sign(vaMessageToSign, True)
-
- If lErrorNumber <> 0 Then
- lErrorNumber = Err.Number
- Call MsgBox("Error during Sign." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
- End If
- On Error GoTo 0
- End If
- End If
- On Error Resume Next
- 'Convert the resulting signature to an hexadecimal representation that
- 'can be shown in a text box.
- sSignature = BinaryToHex(xSigner.SigningMethod.Signature)
- If Err.Number <> 0 Then
- lErrorNumber = Err.Number
- Call MsgBox("Error in signature." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
- End If
- On Error GoTo 0
- If lErrorNumber = 0 Then
- If Len(sSignature) = 0 Then
- 'No output was produced
- txtSignature.Text = ""
- Else
- 'Display the signature in the text box
- 'The BinaryToHex method returning a BSTR (Unicode), we
- 'don't have to convert the encrypted string. We can affect
- 'it directly to the textbox
- txtSignature.Text = sSignature
- End If
- End If
- 'Deallocate the Signing object.
- Set xSigner = Nothing
- End Sub
- '------------------------------------------------------------------------------------
- 'Do the Verification of the message or file against the signature
- '------------------------------------------------------------------------------------
- Private Sub cmdVerify_Click()
- Dim xSigner As XceedSigning
- Dim vaMessageToVerify As Variant
- Dim lErrorNumber As Long
- Dim bVerifiedPassed As Boolean
- 'Create an instance of the Xceed Signing
- Set xSigner = New XceedSigning
- lErrorNumber = 0
- bVerifiedPassed = True
- On Error Resume Next
- 'Set the public key that will be used to verify the signature.
- 'We don't have to set the private key for verifying.
- xSigner.SigningMethod.PublicKey = ReadKeyFile(m_sPublicKeyFile)
- If Err.Number <> 0 Then
- lErrorNumber = Err.Number
- Call MsgBox("Error during PublicKey initialization." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
- End If
- On Error GoTo 0
- On Error Resume Next
- 'Convert the hexadecimal signature to a binary representation that can
- 'be affected directly to the Signature property which will be used to
- 'verify that the data was signed with the proper (author) private key.
- xSigner.SigningMethod.Signature = HexToBinary(txtSignature.Text)
- If Err.Number <> 0 Then
- lErrorNumber = Err.Number
- Call MsgBox("Error in signature." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
- End If
- On Error GoTo 0
- If lErrorNumber = 0 Then
- If optFileSigning.Value Then
- 'Verify the signature of the specified file
-
- On Error Resume Next
- 'Verify the file using the ReadFile method. The ReadFile method
- 'returns an error code if the verification fails.
- Call xSigner.ReadFile(txtFileToSign.Text, 0, 0, efpVerify, True)
- If Err.Number = 0 Then
- Call MsgBox("Verify passed")
- Else
- lErrorNumber = Err.Number
- If lErrorNumber = eerVerifyFailed Then
- Call MsgBox("Verify failed")
- Else
- Call MsgBox("Error during ReadFile." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
- End If
- End If
- On Error GoTo 0
- Else
- 'Verify the signature of the specified message
-
- 'Convert unicode text to an ASCII string as the message is,
- 'most probably, received in ASCII format.
- vaMessageToVerify = StrConv(txtMessageToSign.Text, vbFromUnicode)
-
- On Error Resume Next
- 'Verify the ascii string, specifying that (True parameter)
- 'this is the end of data (there will be no more calls to Verify).
- 'Verify returns False if the verification fails.
- bVerifiedPassed = xSigner.Verify(vaMessageToVerify, True)
-
- If lErrorNumber = 0 Then
- If bVerifiedPassed Then
- Call MsgBox("Verify passed")
- Else
- Call MsgBox("Verify failed")
- End If
- Else
- lErrorNumber = Err.Number
- Call MsgBox("Error during Verify." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
- End If
- On Error GoTo 0
- End If
- End If
- 'Deallocate the Signing object.
- Set xSigner = Nothing
- End Sub
- '------------------------------------------------------------------------------------
- 'Display the options form, saving them if the user click OK
- '------------------------------------------------------------------------------------
- Private Sub cmdOption_Click()
- Dim xFrmOption As frmOption
- Set xFrmOption = New frmOption
- If xFrmOption.ShowForm(m_sPrivateKeyFile, m_sPublicKeyFile) Then
- Call SaveOption
- End If
- Set xFrmOption = Nothing
- End Sub
- '------------------------------------------------------------------------------------
- 'Quit the sample application
- '------------------------------------------------------------------------------------
- Private Sub cmdQuit_Click()
- End
- End Sub
- '------------------------------------------------------------------------------------
- 'Select the file name that contain (or will contain) the private key.
- '------------------------------------------------------------------------------------
- Private Sub cmdSelectFile_Click()
- With dlgCommon
- .FileName = ""
- .DialogTitle = "File to sign"
- .Filter = "All type (*.*)|*.*"
- .FilterIndex = 0
- .Flags = cdlOFNExplorer
- On Error Resume Next
- Call .ShowOpen
- If Len(.FileName) > 0 Then
- txtFileToSign.Text = .FileName
- End If
- On Error GoTo 0
- End With
- End Sub
- '------------------------------------------------------------------------------------
- 'Initialize the default and last saved options
- '------------------------------------------------------------------------------------
- Private Sub Form_Load()
- Call LoadOption
- Call EnableControls
- End Sub
- '------------------------------------------------------------------------------------
- 'The user selected file signing, disable controls associated with message
- 'signing
- '------------------------------------------------------------------------------------
- Private Sub optFileSigning_Click()
- If Me.Visible Then
- optMessageSigning.Value = False
- Call EnableControls
- Call SaveObjectToSign
- End If
- End Sub
- '------------------------------------------------------------------------------------
- 'The user selected message signing, disable controls associated with file
- 'signing
- '------------------------------------------------------------------------------------
- Private Sub optMessageSigning_Click()
- If Me.Visible Then
- optFileSigning.Value = False
- Call EnableControls
- Call SaveObjectToSign
- End If
- End Sub
- '====================================================================================
- ' FUNCTIONS
- '====================================================================================
- '------------------------------------------------------------------------------------
- 'Enable the control associated with the selected object to sign (file or
- 'message). Disable the other controls.
- '------------------------------------------------------------------------------------
- Private Sub EnableControls()
- Dim bFileSigning As Boolean
- bFileSigning = optFileSigning.Value
- txtFileToSign.Enabled = bFileSigning
- cmdSelectFile.Enabled = bFileSigning
- txtMessageToSign.Enabled = Not bFileSigning
- End Sub
- '------------------------------------------------------------------------------------
- 'Load in the member variables the options saved in the registry the last
- 'time this sample file manager was called.
- '------------------------------------------------------------------------------------
- Private Sub LoadOption()
- m_sPrivateKeyFile = GetSetting("XceedSigner", "Signing", "PrivateKeyFile", "")
- m_sPublicKeyFile = GetSetting("XceedSigner", "Signing", "PublicKeyFile", "")
- optFileSigning.Value = Val(GetSetting("XceedSigner", "Signing", "FileSigning", CStr(CInt(True))))
- optMessageSigning.Value = Val(GetSetting("XceedSigner", "Signing", "MessageSigning", CStr(CInt(False))))
- End Sub
- '------------------------------------------------------------------------------------
- 'Read the content of the specified file, allegedly containing a private or public
- 'key in hexadecimal representation.
- 'Return the key in the vaKey parameter. This function can return Empty.
- '------------------------------------------------------------------------------------
- Private Function ReadKeyFile(ByVal sKeyFile As String) As Variant
- Dim nNoFic As Integer
- Dim sBuffer As String
- Dim sKey As String
- Dim vaKey As Variant
- vaKey = Empty
- If Len(sKeyFile) > 0 Then
- On Error Resume Next
- nNoFic = FreeFile
- Open sKeyFile For Input As #nNoFic
- If Err.Number = 0 Then
- sKey = ""
- While Not EOF(nNoFic) And Err.Number = 0
- Line Input #nNoFic, sBuffer
- sKey = sKey & sBuffer
- Wend
- Close #nNoFic
- End If
-
- If Err.Number = 0 Then
- 'The file has been read successfully. Decode the hexadecimal key to binary form.
- vaKey = HexToBinary(sKey)
- Else
- vaKey = Empty
- Call MsgBox("Error reading key file " & sKeyFile & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
- End If
- On Error GoTo 0
- End If
- ReadKeyFile = vaKey
- End Function
- '------------------------------------------------------------------------------------
- 'Save the current selected (and deselected) object to sign (file or message)
- '------------------------------------------------------------------------------------
- Private Sub SaveObjectToSign()
- Call SaveSetting("XceedSigner", "Signing", "FileSigning", CStr(CInt(optFileSigning.Value)))
- Call SaveSetting("XceedSigner", "Signing", "MessageSigning", CStr(CInt(optMessageSigning.Value)))
- End Sub
- '------------------------------------------------------------------------------------
- 'Save the current options from the member variables in the registry
- '------------------------------------------------------------------------------------
- Private Sub SaveOption()
- Call SaveSetting("XceedSigner", "Signing", "PrivateKeyFile", m_sPrivateKeyFile)
- Call SaveSetting("XceedSigner", "Signing", "PublicKeyFile", m_sPublicKeyFile)
- End Sub
-