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

  1. VERSION 5.00
  2. Begin VB.Form frmKeyPair 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "Key pair generator"
  5.    ClientHeight    =   1380
  6.    ClientLeft      =   0
  7.    ClientTop       =   285
  8.    ClientWidth     =   5355
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    ScaleHeight     =   1380
  13.    ScaleWidth      =   5355
  14.    ShowInTaskbar   =   0   'False
  15.    StartUpPosition =   1  'CenterOwner
  16.    Begin VB.TextBox txtRandomSeed 
  17.       Height          =   285
  18.       IMEMode         =   3  'DISABLE
  19.       Left            =   1275
  20.       OLEDropMode     =   1  'Manual
  21.       TabIndex        =   1
  22.       Top             =   525
  23.       Width           =   3990
  24.    End
  25.    Begin VB.TextBox txtKeySize 
  26.       Height          =   285
  27.       IMEMode         =   3  'DISABLE
  28.       Left            =   1275
  29.       MaxLength       =   4
  30.       OLEDropMode     =   1  'Manual
  31.       TabIndex        =   0
  32.       Top             =   150
  33.       Width           =   840
  34.    End
  35.    Begin VB.CommandButton cmdCancel 
  36.       Cancel          =   -1  'True
  37.       Caption         =   "Cancel"
  38.       Height          =   315
  39.       Left            =   4125
  40.       TabIndex        =   3
  41.       Top             =   975
  42.       Width           =   1140
  43.    End
  44.    Begin VB.CommandButton cmdOk 
  45.       Caption         =   "Ok"
  46.       Default         =   -1  'True
  47.       Height          =   315
  48.       Left            =   2925
  49.       TabIndex        =   2
  50.       Top             =   975
  51.       Width           =   1140
  52.    End
  53.    Begin VB.Label lbl 
  54.       Caption         =   "Random seed"
  55.       Height          =   240
  56.       Index           =   1
  57.       Left            =   150
  58.       TabIndex        =   5
  59.       Top             =   525
  60.       Width           =   1065
  61.    End
  62.    Begin VB.Label lbl 
  63.       Caption         =   "Key size"
  64.       Height          =   240
  65.       Index           =   0
  66.       Left            =   150
  67.       TabIndex        =   4
  68.       Top             =   150
  69.       Width           =   915
  70.    End
  71. Attribute VB_Name = "frmKeyPair"
  72. Attribute VB_GlobalNameSpace = False
  73. Attribute VB_Creatable = False
  74. Attribute VB_PredeclaredId = True
  75. Attribute VB_Exposed = False
  76. ' Xceed Encryption Library - Memory Encrypt sample
  77. ' Copyright (c) 2001 Xceed Software Inc.
  78. ' [KeyPair.frm]
  79. ' This form module allows the user to generate a new random key pair for RSA
  80. ' public key encryption.
  81. ' This file is part of the Xceed Encryption Library sample applications.
  82. ' The source code in this file is only intended as a supplement to Xceed
  83. ' Encryption Library's documentation, and is provided "as is", without
  84. ' warranty of any kind, either expressed or implied.
  85. Option Explicit
  86. Private m_bOk As Boolean
  87. Public Function ShowForm(ByVal sPrivateKeyFile As String, _
  88.                          ByVal sPublicKeyFile As String) As Boolean
  89.     'By default a key size of 1024 is proposed
  90.     txtKeySize.Text = "1024"
  91.     Call Me.Show(vbModal)
  92.     If m_bOk Then
  93.         'The user clicked the OK button, generate a new key pair
  94.         Call GenerateKeyPair(sPrivateKeyFile, sPublicKeyFile)
  95.     End If
  96.     ShowForm = m_bOk
  97.     Call Unload(Me)
  98. End Function
  99. Private Sub cmdOk_Click()
  100.     If Val(txtKeySize.Text) > 0 Then
  101.         m_bOk = True
  102.         Call Me.Hide
  103.     Else
  104.         Call MsgBox("Invalid key size", vbExclamation, "Error")
  105.     End If
  106. End Sub
  107. Private Sub cmdCancel_Click()
  108.     Call Me.Hide
  109. End Sub
  110. Private Sub Form_Load()
  111.     m_bOk = False
  112. End Sub
  113. Private Sub Form_Unload(Cancel As Integer)
  114.     Call cmdCancel_Click
  115. End Sub
  116. '------------------------------------------------------------------------------------
  117. 'Do the key pair generation
  118. '------------------------------------------------------------------------------------
  119. Private Sub GenerateKeyPair(ByVal sPrivateKeyFile As String, _
  120.                             ByVal sPublicKeyFile As String)
  121.     Dim xRSA As XceedRSAEncryptionMethod
  122.     Dim xEncryptor As XceedEncryption
  123.     Dim vaSeed As Variant
  124.     Dim nNoFic As Integer
  125.     On Error Resume Next
  126.         'Create an instance of the XceedRSA Encryption Method
  127.         Set xRSA = New XceedRSAEncryptionMethod
  128.         'Create an instance of the Xceed Encryption
  129.         Set xEncryptor = New XceedEncryption
  130.         If Err.Number = 0 Then
  131.             'We set the seed value to Empty by default.
  132.             vaSeed = Empty
  133.             
  134.             If Len(Trim$(txtRandomSeed.Text)) > 0 Then
  135.                 'The user specified a seed value for the pseudo-random generator
  136.                 'used in the key pair generation. We use it.
  137.                 vaSeed = txtRandomSeed.Text
  138.             End If
  139.             
  140.             'Generate a new key pair Specifying:
  141.             '   - The wanted key size entered by the user.
  142.             '   - The seed to the pseudo-random generator. If the seed is Empty,
  143.             '     the RSA object will use its own random seed generator.
  144.             Call xRSA.SetRandomKeyPair(Val(txtKeySize.Text), vaSeed)
  145.             
  146.             If Err.Number = 0 Then
  147.                 'Save the hexadecimal representation of the Private key
  148.                 'generated above, in the file specified.
  149.                 nNoFic = FreeFile
  150.                 Open sPrivateKeyFile For Output As #nNoFic
  151.                 If Err.Number = 0 Then
  152.                     Print #nNoFic, BinaryToHex(xRSA.PrivateKey)
  153.                     Close #nNoFic
  154.                 End If
  155.             
  156.                 'Save the hexadecimal representation of the Public key
  157.                 'generated above, in the file specified.
  158.                 nNoFic = FreeFile
  159.                 Open sPublicKeyFile For Output As #nNoFic
  160.                 If Err.Number = 0 Then
  161.                     Print #nNoFic, BinaryToHex(xRSA.PublicKey)
  162.                     Close #nNoFic
  163.                 End If
  164.             End If
  165.             
  166.         End If
  167.         Set xEncryptor = Nothing
  168.         Set xRSA = Nothing
  169.         
  170.         If Err.Number <> 0 Then
  171.             Call MsgBox("Error generating key pair " & vbCrLf & Err.Description & " (" & Hex(Err.Number) & ")")
  172.         End If
  173.     On Error GoTo 0
  174. End Sub
  175.