home *** CD-ROM | disk | FTP | other *** search
- VERSION 5.00
- Begin VB.Form frmKeyPair
- BorderStyle = 3 'Fixed Dialog
- Caption = "Key pair generator"
- ClientHeight = 1380
- ClientLeft = 0
- ClientTop = 285
- ClientWidth = 5355
- LinkTopic = "Form1"
- MaxButton = 0 'False
- MinButton = 0 'False
- ScaleHeight = 1380
- ScaleWidth = 5355
- ShowInTaskbar = 0 'False
- StartUpPosition = 1 'CenterOwner
- Begin VB.TextBox txtRandomSeed
- Height = 285
- IMEMode = 3 'DISABLE
- Left = 1275
- OLEDropMode = 1 'Manual
- TabIndex = 1
- Top = 525
- Width = 3990
- End
- Begin VB.TextBox txtKeySize
- Height = 285
- IMEMode = 3 'DISABLE
- Left = 1275
- MaxLength = 4
- OLEDropMode = 1 'Manual
- TabIndex = 0
- Top = 150
- Width = 840
- End
- Begin VB.CommandButton cmdCancel
- Cancel = -1 'True
- Caption = "Cancel"
- Height = 315
- Left = 4125
- TabIndex = 3
- Top = 975
- Width = 1140
- End
- Begin VB.CommandButton cmdOk
- Caption = "Ok"
- Default = -1 'True
- Height = 315
- Left = 2925
- TabIndex = 2
- Top = 975
- Width = 1140
- End
- Begin VB.Label lbl
- Caption = "Random seed"
- Height = 240
- Index = 1
- Left = 150
- TabIndex = 5
- Top = 525
- Width = 1065
- End
- Begin VB.Label lbl
- Caption = "Key size"
- Height = 240
- Index = 0
- Left = 150
- TabIndex = 4
- Top = 150
- Width = 915
- End
- Attribute VB_Name = "frmKeyPair"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_PredeclaredId = True
- Attribute VB_Exposed = False
- ' Xceed Encryption Library - Memory Encrypt sample
- ' Copyright (c) 2001 Xceed Software Inc.
- ' [KeyPair.frm]
- ' This form module allows the user to generate a new random key pair for RSA
- ' public key encryption.
- ' 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
- Private m_bOk As Boolean
- Public Function ShowForm(ByVal sPrivateKeyFile As String, _
- ByVal sPublicKeyFile As String) As Boolean
- 'By default a key size of 1024 is proposed
- txtKeySize.Text = "1024"
- Call Me.Show(vbModal)
- If m_bOk Then
- 'The user clicked the OK button, generate a new key pair
- Call GenerateKeyPair(sPrivateKeyFile, sPublicKeyFile)
- End If
- ShowForm = m_bOk
- Call Unload(Me)
- End Function
- Private Sub cmdOk_Click()
- If Val(txtKeySize.Text) > 0 Then
- m_bOk = True
- Call Me.Hide
- Else
- Call MsgBox("Invalid key size", vbExclamation, "Error")
- End If
- End Sub
- Private Sub cmdCancel_Click()
- Call Me.Hide
- End Sub
- Private Sub Form_Load()
- m_bOk = False
- End Sub
- Private Sub Form_Unload(Cancel As Integer)
- Call cmdCancel_Click
- End Sub
- '------------------------------------------------------------------------------------
- 'Do the key pair generation
- '------------------------------------------------------------------------------------
- Private Sub GenerateKeyPair(ByVal sPrivateKeyFile As String, _
- ByVal sPublicKeyFile As String)
- Dim xRSA As XceedRSAEncryptionMethod
- Dim xEncryptor As XceedEncryption
- Dim vaSeed As Variant
- Dim nNoFic As Integer
- On Error Resume Next
- 'Create an instance of the XceedRSA Encryption Method
- Set xRSA = New XceedRSAEncryptionMethod
- 'Create an instance of the Xceed Encryption
- Set xEncryptor = New XceedEncryption
- If Err.Number = 0 Then
- 'We set the seed value to Empty by default.
- vaSeed = Empty
-
- If Len(Trim$(txtRandomSeed.Text)) > 0 Then
- 'The user specified a seed value for the pseudo-random generator
- 'used in the key pair generation. We use it.
- vaSeed = txtRandomSeed.Text
- End If
-
- 'Generate a new key pair Specifying:
- ' - The wanted key size entered by the user.
- ' - The seed to the pseudo-random generator. If the seed is Empty,
- ' the RSA object will use its own random seed generator.
- Call xRSA.SetRandomKeyPair(Val(txtKeySize.Text), vaSeed)
-
- If Err.Number = 0 Then
- 'Save the hexadecimal representation of the Private key
- 'generated above, in the file specified.
- nNoFic = FreeFile
- Open sPrivateKeyFile For Output As #nNoFic
- If Err.Number = 0 Then
- Print #nNoFic, BinaryToHex(xRSA.PrivateKey)
- Close #nNoFic
- End If
-
- 'Save the hexadecimal representation of the Public key
- 'generated above, in the file specified.
- nNoFic = FreeFile
- Open sPublicKeyFile For Output As #nNoFic
- If Err.Number = 0 Then
- Print #nNoFic, BinaryToHex(xRSA.PublicKey)
- Close #nNoFic
- End If
- End If
-
- End If
- Set xEncryptor = Nothing
- Set xRSA = Nothing
-
- If Err.Number <> 0 Then
- Call MsgBox("Error generating key pair " & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
- End If
- On Error GoTo 0
- End Sub
-