home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / vbasic / Data / Utils / XZipComp.exe / XceedEncryption.Cab / F112966_Signer.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-07-04  |  19.1 KB  |  462 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form frmSigner 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "Signer"
  6.    ClientHeight    =   5655
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   8580
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   5655
  14.    ScaleWidth      =   8580
  15.    StartUpPosition =   2  'CenterScreen
  16.    Begin VB.CommandButton cmdOption 
  17.       Caption         =   "Option"
  18.       Height          =   315
  19.       Left            =   150
  20.       TabIndex        =   8
  21.       Top             =   5250
  22.       Width           =   1140
  23.    End
  24.    Begin VB.Frame fraMessageSigning 
  25.       Caption         =   "Frame2"
  26.       Height          =   2265
  27.       Left            =   150
  28.       TabIndex        =   11
  29.       Top             =   900
  30.       Width           =   8340
  31.       Begin VB.OptionButton optMessageSigning 
  32.          Caption         =   "Message signing"
  33.          Height          =   240
  34.          Left            =   75
  35.          TabIndex        =   3
  36.          Top             =   0
  37.          Width           =   1515
  38.       End
  39.       Begin VB.TextBox txtMessageToSign 
  40.          Height          =   1635
  41.          Left            =   150
  42.          MultiLine       =   -1  'True
  43.          OLEDropMode     =   1  'Manual
  44.          ScrollBars      =   2  'Vertical
  45.          TabIndex        =   4
  46.          Top             =   525
  47.          Width           =   8115
  48.       End
  49.       Begin VB.Label lbl 
  50.          Caption         =   "Message to sign"
  51.          Height          =   240
  52.          Index           =   2
  53.          Left            =   150
  54.          TabIndex        =   13
  55.          Top             =   300
  56.          Width           =   1290
  57.       End
  58.    End
  59.    Begin VB.Frame fraFileSigning 
  60.       Height          =   690
  61.       Left            =   150
  62.       TabIndex        =   10
  63.       Top             =   75
  64.       Width           =   8340
  65.       Begin VB.TextBox txtFileToSign 
  66.          Height          =   285
  67.          Left            =   1500
  68.          OLEDropMode     =   1  'Manual
  69.          TabIndex        =   1
  70.          Top             =   300
  71.          Width           =   6390
  72.       End
  73.       Begin VB.CommandButton cmdSelectFile 
  74.          Caption         =   "..."
  75.          Height          =   285
  76.          Left            =   7950
  77.          TabIndex        =   2
  78.          Top             =   300
  79.          Width           =   285
  80.       End
  81.       Begin VB.OptionButton optFileSigning 
  82.          Caption         =   "File signing"
  83.          Height          =   240
  84.          Left            =   75
  85.          TabIndex        =   0
  86.          Top             =   0
  87.          Width           =   1140
  88.       End
  89.       Begin VB.Label lbl 
  90.          Caption         =   "File to sign"
  91.          Height          =   240
  92.          Index           =   0
  93.          Left            =   150
  94.          TabIndex        =   14
  95.          Top             =   300
  96.          Width           =   1290
  97.       End
  98.    End
  99.    Begin VB.Frame fraSignature 
  100.       Caption         =   "Signature"
  101.       Height          =   1440
  102.       Left            =   150
  103.       TabIndex        =   12
  104.       Top             =   3750
  105.       Width           =   8340
  106.       Begin VB.TextBox txtSignature 
  107.          Height          =   1065
  108.          Left            =   150
  109.          MultiLine       =   -1  'True
  110.          ScrollBars      =   2  'Vertical
  111.          TabIndex        =   7
  112.          Top             =   300
  113.          Width           =   8115
  114.       End
  115.    End
  116.    Begin VB.CommandButton cmdQuit 
  117.       Cancel          =   -1  'True
  118.       Caption         =   "Quit"
  119.       Height          =   315
  120.       Left            =   7350
  121.       TabIndex        =   9
  122.       Top             =   5250
  123.       Width           =   1140
  124.    End
  125.    Begin VB.CommandButton cmdVerify 
  126.       Caption         =   "Verify"
  127.       Height          =   315
  128.       Left            =   7350
  129.       TabIndex        =   6
  130.       Top             =   3300
  131.       Width           =   1140
  132.    End
  133.    Begin VB.CommandButton cmdSign 
  134.       Caption         =   "Sign"
  135.       Height          =   315
  136.       Left            =   6150
  137.       TabIndex        =   5
  138.       Top             =   3300
  139.       Width           =   1140
  140.    End
  141.    Begin MSComDlg.CommonDialog dlgCommon 
  142.       Left            =   1875
  143.       Top             =   5175
  144.       _ExtentX        =   847
  145.       _ExtentY        =   847
  146.       _Version        =   393216
  147.    End
  148. Attribute VB_Name = "frmSigner"
  149. Attribute VB_GlobalNameSpace = False
  150. Attribute VB_Creatable = False
  151. Attribute VB_PredeclaredId = True
  152. Attribute VB_Exposed = False
  153. ' Xceed Encryption Library - Signer sample
  154. ' Copyright (c) 2001 Xceed Software Inc.
  155. ' [Signer.frm]
  156. ' This form module contains the main form's code. It demonstrates how to
  157. ' Sign a chunk of memory data or a file and Verify a file or memory data against
  158. ' a signature. It specifically uses:
  159. '  - The Sign, Verify, ReadFile and SetRandomKeyPair methods.
  160. '  - The PrivateKey, PublicKey, Signature and SigningMethod properties.
  161. ' This file is part of the Xceed Encryption Library sample applications.
  162. ' The source code in this file is only intended as a supplement to Xceed
  163. ' Encryption Library's documentation, and is provided "as is", without
  164. ' warranty of any kind, either expressed or implied.
  165. Option Explicit
  166. 'The values chosen by the user in the Option form
  167. Private m_sPrivateKeyFile As String
  168. Private m_sPublicKeyFile As String
  169. '====================================================================================
  170. ' EVENTS - triggered by the form and its controls
  171. '====================================================================================
  172. '------------------------------------------------------------------------------------
  173. 'Do the Signing of the message or file
  174. '------------------------------------------------------------------------------------
  175. Private Sub cmdSign_Click()
  176.     Dim xSigner As XceedSigning
  177.     Dim vaMessageToSign As Variant
  178.     Dim lErrorNumber As Long
  179.     Dim sSignature As String
  180.     'Create an instance of the Xceed Signing
  181.     Set xSigner = New XceedSigning
  182.     lErrorNumber = 0
  183.     On Error Resume Next
  184.         'Set the private key that will be used to calculate the signature.
  185.         'We don't have to set the public key for signing.
  186.         xSigner.SigningMethod.PrivateKey = ReadKeyFile(m_sPrivateKeyFile)
  187.         If Err.Number <> 0 Then
  188.             lErrorNumber = Err.Number
  189.             Call MsgBox("Error during PrivateKey initialization." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
  190.         End If
  191.     On Error GoTo 0
  192.     If lErrorNumber = 0 Then
  193.         If optFileSigning.Value Then
  194.             'Calculate the signature of the specified file
  195.             On Error Resume Next
  196.                 'Sign the file using the ReadFile method
  197.                 Call xSigner.ReadFile(txtFileToSign.Text, 0, 0, efpSign, True)
  198.                 If Err.Number <> 0 Then
  199.                     lErrorNumber = Err.Number
  200.                     Call MsgBox("Error during ReadFile." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
  201.                 End If
  202.             On Error GoTo 0
  203.         Else
  204.             'Calculate the signature of the specified message
  205.             
  206.             'Convert unicode text to an ASCII string as the message will,
  207.             'most probably, be sent in ASCII format.
  208.             vaMessageToSign = StrConv(txtMessageToSign.Text, vbFromUnicode)
  209.             
  210.             On Error Resume Next
  211.                 'Sign the ascii string, specifying that (True parameter)
  212.                 'this is the end of data (there will be no more calls to Sign).
  213.                 Call xSigner.Sign(vaMessageToSign, True)
  214.                 
  215.                 If lErrorNumber <> 0 Then
  216.                     lErrorNumber = Err.Number
  217.                     Call MsgBox("Error during Sign." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
  218.                 End If
  219.             On Error GoTo 0
  220.         End If
  221.     End If
  222.     On Error Resume Next
  223.         'Convert the resulting signature to an hexadecimal representation that
  224.         'can be shown in a text box.
  225.         sSignature = BinaryToHex(xSigner.SigningMethod.Signature)
  226.         If Err.Number <> 0 Then
  227.             lErrorNumber = Err.Number
  228.             Call MsgBox("Error in signature." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
  229.         End If
  230.     On Error GoTo 0
  231.     If lErrorNumber = 0 Then
  232.         If Len(sSignature) = 0 Then
  233.             'No output was produced
  234.             txtSignature.Text = ""
  235.         Else
  236.             'Display the signature in the text box
  237.             'The BinaryToHex method returning a BSTR (Unicode), we
  238.             'don't have to convert the encrypted string. We can affect
  239.             'it directly to the textbox
  240.             txtSignature.Text = sSignature
  241.         End If
  242.     End If
  243.     'Deallocate the Signing object.
  244.     Set xSigner = Nothing
  245. End Sub
  246. '------------------------------------------------------------------------------------
  247. 'Do the Verification of the message or file against the signature
  248. '------------------------------------------------------------------------------------
  249. Private Sub cmdVerify_Click()
  250.     Dim xSigner As XceedSigning
  251.     Dim vaMessageToVerify As Variant
  252.     Dim lErrorNumber As Long
  253.     Dim bVerifiedPassed As Boolean
  254.     'Create an instance of the Xceed Signing
  255.     Set xSigner = New XceedSigning
  256.     lErrorNumber = 0
  257.     bVerifiedPassed = True
  258.     On Error Resume Next
  259.         'Set the public key that will be used to verify the signature.
  260.         'We don't have to set the private key for verifying.
  261.         xSigner.SigningMethod.PublicKey = ReadKeyFile(m_sPublicKeyFile)
  262.         If Err.Number <> 0 Then
  263.             lErrorNumber = Err.Number
  264.             Call MsgBox("Error during PublicKey initialization." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
  265.         End If
  266.     On Error GoTo 0
  267.     On Error Resume Next
  268.         'Convert the hexadecimal signature to a binary representation that can
  269.         'be affected directly to the Signature property which will be used to
  270.         'verify that the data was signed with the proper (author) private key.
  271.         xSigner.SigningMethod.Signature = HexToBinary(txtSignature.Text)
  272.         If Err.Number <> 0 Then
  273.             lErrorNumber = Err.Number
  274.             Call MsgBox("Error in signature." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
  275.         End If
  276.     On Error GoTo 0
  277.     If lErrorNumber = 0 Then
  278.         If optFileSigning.Value Then
  279.             'Verify the signature of the specified file
  280.             
  281.             On Error Resume Next
  282.                 'Verify the file using the ReadFile method. The ReadFile method
  283.                 'returns an error code if the verification fails.
  284.                 Call xSigner.ReadFile(txtFileToSign.Text, 0, 0, efpVerify, True)
  285.                 If Err.Number = 0 Then
  286.                     Call MsgBox("Verify passed")
  287.                 Else
  288.                     lErrorNumber = Err.Number
  289.                     If lErrorNumber = eerVerifyFailed Then
  290.                         Call MsgBox("Verify failed")
  291.                     Else
  292.                         Call MsgBox("Error during ReadFile." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
  293.                     End If
  294.                 End If
  295.             On Error GoTo 0
  296.         Else
  297.             'Verify the signature of the specified message
  298.             
  299.             'Convert unicode text to an ASCII string as the message is,
  300.             'most probably, received in ASCII format.
  301.             vaMessageToVerify = StrConv(txtMessageToSign.Text, vbFromUnicode)
  302.             
  303.             On Error Resume Next
  304.                 'Verify the ascii string, specifying that (True parameter)
  305.                 'this is the end of data (there will be no more calls to Verify).
  306.                 'Verify returns False if the verification fails.
  307.                 bVerifiedPassed = xSigner.Verify(vaMessageToVerify, True)
  308.                 
  309.                 If lErrorNumber = 0 Then
  310.                     If bVerifiedPassed Then
  311.                         Call MsgBox("Verify passed")
  312.                     Else
  313.                         Call MsgBox("Verify failed")
  314.                     End If
  315.                 Else
  316.                     lErrorNumber = Err.Number
  317.                     Call MsgBox("Error during Verify." & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
  318.                 End If
  319.             On Error GoTo 0
  320.         End If
  321.     End If
  322.     'Deallocate the Signing object.
  323.     Set xSigner = Nothing
  324. End Sub
  325. '------------------------------------------------------------------------------------
  326. 'Display the options form, saving them if the user click OK
  327. '------------------------------------------------------------------------------------
  328. Private Sub cmdOption_Click()
  329.     Dim xFrmOption As frmOption
  330.     Set xFrmOption = New frmOption
  331.     If xFrmOption.ShowForm(m_sPrivateKeyFile, m_sPublicKeyFile) Then
  332.         Call SaveOption
  333.     End If
  334.     Set xFrmOption = Nothing
  335. End Sub
  336. '------------------------------------------------------------------------------------
  337. 'Quit the sample application
  338. '------------------------------------------------------------------------------------
  339. Private Sub cmdQuit_Click()
  340.     End
  341. End Sub
  342. '------------------------------------------------------------------------------------
  343. 'Select the file name that contain (or will contain) the private key.
  344. '------------------------------------------------------------------------------------
  345. Private Sub cmdSelectFile_Click()
  346.     With dlgCommon
  347.         .FileName = ""
  348.         .DialogTitle = "File to sign"
  349.         .Filter = "All type (*.*)|*.*"
  350.         .FilterIndex = 0
  351.         .Flags = cdlOFNExplorer
  352.         On Error Resume Next
  353.             Call .ShowOpen
  354.             If Len(.FileName) > 0 Then
  355.                 txtFileToSign.Text = .FileName
  356.             End If
  357.         On Error GoTo 0
  358.     End With
  359. End Sub
  360. '------------------------------------------------------------------------------------
  361. 'Initialize the default and last saved options
  362. '------------------------------------------------------------------------------------
  363. Private Sub Form_Load()
  364.     Call LoadOption
  365.     Call EnableControls
  366. End Sub
  367. '------------------------------------------------------------------------------------
  368. 'The user selected file signing, disable controls associated with message
  369. 'signing
  370. '------------------------------------------------------------------------------------
  371. Private Sub optFileSigning_Click()
  372.     If Me.Visible Then
  373.         optMessageSigning.Value = False
  374.         Call EnableControls
  375.         Call SaveObjectToSign
  376.     End If
  377. End Sub
  378. '------------------------------------------------------------------------------------
  379. 'The user selected message signing, disable controls associated with file
  380. 'signing
  381. '------------------------------------------------------------------------------------
  382. Private Sub optMessageSigning_Click()
  383.     If Me.Visible Then
  384.         optFileSigning.Value = False
  385.         Call EnableControls
  386.         Call SaveObjectToSign
  387.     End If
  388. End Sub
  389. '====================================================================================
  390. ' FUNCTIONS
  391. '====================================================================================
  392. '------------------------------------------------------------------------------------
  393. 'Enable the control associated with the selected object to sign (file or
  394. 'message). Disable the other controls.
  395. '------------------------------------------------------------------------------------
  396. Private Sub EnableControls()
  397.     Dim bFileSigning As Boolean
  398.     bFileSigning = optFileSigning.Value
  399.     txtFileToSign.Enabled = bFileSigning
  400.     cmdSelectFile.Enabled = bFileSigning
  401.     txtMessageToSign.Enabled = Not bFileSigning
  402. End Sub
  403. '------------------------------------------------------------------------------------
  404. 'Load in the member variables the options saved in the registry the last
  405. 'time this sample file manager was called.
  406. '------------------------------------------------------------------------------------
  407. Private Sub LoadOption()
  408.     m_sPrivateKeyFile = GetSetting("XceedSigner", "Signing", "PrivateKeyFile", "")
  409.     m_sPublicKeyFile = GetSetting("XceedSigner", "Signing", "PublicKeyFile", "")
  410.     optFileSigning.Value = Val(GetSetting("XceedSigner", "Signing", "FileSigning", CStr(CInt(True))))
  411.     optMessageSigning.Value = Val(GetSetting("XceedSigner", "Signing", "MessageSigning", CStr(CInt(False))))
  412. End Sub
  413. '------------------------------------------------------------------------------------
  414. 'Read the content of the specified file, allegedly containing a private or public
  415. 'key in hexadecimal representation.
  416. 'Return the key in the vaKey parameter. This function can return Empty.
  417. '------------------------------------------------------------------------------------
  418. Private Function ReadKeyFile(ByVal sKeyFile As String) As Variant
  419.     Dim nNoFic As Integer
  420.     Dim sBuffer As String
  421.     Dim sKey As String
  422.     Dim vaKey As Variant
  423.     vaKey = Empty
  424.     If Len(sKeyFile) > 0 Then
  425.         On Error Resume Next
  426.             nNoFic = FreeFile
  427.             Open sKeyFile For Input As #nNoFic
  428.             If Err.Number = 0 Then
  429.                 sKey = ""
  430.                 While Not EOF(nNoFic) And Err.Number = 0
  431.                     Line Input #nNoFic, sBuffer
  432.                     sKey = sKey & sBuffer
  433.                 Wend
  434.                 Close #nNoFic
  435.             End If
  436.             
  437.             If Err.Number = 0 Then
  438.                 'The file has been read successfully. Decode the hexadecimal key to binary form.
  439.                 vaKey = HexToBinary(sKey)
  440.             Else
  441.                 vaKey = Empty
  442.                 Call MsgBox("Error reading key file " & sKeyFile & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
  443.             End If
  444.         On Error GoTo 0
  445.     End If
  446.     ReadKeyFile = vaKey
  447. End Function
  448. '------------------------------------------------------------------------------------
  449. 'Save the current selected (and deselected) object to sign (file or message)
  450. '------------------------------------------------------------------------------------
  451. Private Sub SaveObjectToSign()
  452.     Call SaveSetting("XceedSigner", "Signing", "FileSigning", CStr(CInt(optFileSigning.Value)))
  453.     Call SaveSetting("XceedSigner", "Signing", "MessageSigning", CStr(CInt(optMessageSigning.Value)))
  454. End Sub
  455. '------------------------------------------------------------------------------------
  456. 'Save the current options from the member variables in the registry
  457. '------------------------------------------------------------------------------------
  458. Private Sub SaveOption()
  459.     Call SaveSetting("XceedSigner", "Signing", "PrivateKeyFile", m_sPrivateKeyFile)
  460.     Call SaveSetting("XceedSigner", "Signing", "PublicKeyFile", m_sPublicKeyFile)
  461. End Sub
  462.