⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 了解公钥密码算法的特点
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   8250
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   10185
   LinkTopic       =   "Form1"
   ScaleHeight     =   8250
   ScaleWidth      =   10185
   StartUpPosition =   3  '窗口缺省
   Begin VB.Frame Frame3 
      Caption         =   "解密"
      Height          =   4215
      Left            =   5160
      TabIndex        =   13
      Top             =   3120
      Width           =   4815
      Begin VB.CommandButton Command3 
         Caption         =   "生成明文"
         Height          =   495
         Left            =   2160
         TabIndex        =   23
         Top             =   3600
         Width           =   1215
      End
      Begin VB.TextBox Text9 
         Enabled         =   0   'False
         Height          =   1455
         Left            =   1320
         MultiLine       =   -1  'True
         TabIndex        =   22
         Top             =   2040
         Width           =   2655
      End
      Begin VB.TextBox Text8 
         Enabled         =   0   'False
         Height          =   1455
         Left            =   1320
         MultiLine       =   -1  'True
         TabIndex        =   20
         Top             =   240
         Width           =   2655
      End
      Begin VB.Label Label9 
         Caption         =   "明文 "
         Height          =   255
         Left            =   600
         TabIndex        =   21
         Top             =   2640
         Width           =   615
      End
      Begin VB.Label Label8 
         Caption         =   "密文 "
         Height          =   375
         Left            =   480
         TabIndex        =   19
         Top             =   960
         Width           =   855
      End
   End
   Begin VB.Frame Frame2 
      Caption         =   "加密"
      Height          =   4215
      Left            =   360
      TabIndex        =   12
      Top             =   3120
      Width           =   4455
      Begin VB.CommandButton Command2 
         Caption         =   "生成密文"
         Height          =   495
         Left            =   1560
         TabIndex        =   18
         Top             =   3480
         Width           =   1335
      End
      Begin VB.TextBox Text7 
         Enabled         =   0   'False
         Height          =   1455
         Left            =   1080
         MultiLine       =   -1  'True
         TabIndex        =   17
         Top             =   1920
         Width           =   2535
      End
      Begin VB.TextBox Text6 
         Enabled         =   0   'False
         Height          =   1335
         Left            =   1080
         MultiLine       =   -1  'True
         TabIndex        =   15
         Top             =   240
         Width           =   2535
      End
      Begin VB.Label Label7 
         Caption         =   "密文"
         Height          =   375
         Left            =   360
         TabIndex        =   16
         Top             =   2640
         Width           =   615
      End
      Begin VB.Label Label6 
         Caption         =   "明文 "
         Height          =   495
         Left            =   360
         TabIndex        =   14
         Top             =   840
         Width           =   1095
      End
   End
   Begin VB.Frame Frame1 
      Caption         =   "生成公钥及密钥"
      Height          =   2175
      Left            =   360
      TabIndex        =   0
      Top             =   480
      Width           =   9495
      Begin VB.TextBox Text5 
         Enabled         =   0   'False
         Height          =   375
         Left            =   6720
         TabIndex        =   11
         Top             =   360
         Width           =   1815
      End
      Begin VB.TextBox Text4 
         Enabled         =   0   'False
         Height          =   375
         Left            =   3720
         TabIndex        =   9
         Top             =   960
         Width           =   1575
      End
      Begin VB.TextBox Text3 
         Enabled         =   0   'False
         Height          =   375
         Left            =   600
         TabIndex        =   7
         Top             =   960
         Width           =   1575
      End
      Begin VB.TextBox Text2 
         Height          =   375
         Left            =   3720
         TabIndex        =   5
         Top             =   360
         Width           =   1575
      End
      Begin VB.TextBox Text1 
         Height          =   375
         Left            =   600
         TabIndex        =   3
         Top             =   360
         Width           =   1575
      End
      Begin VB.CommandButton Command1 
         Caption         =   "确定"
         Height          =   375
         Left            =   4200
         TabIndex        =   1
         Top             =   1680
         Width           =   735
      End
      Begin VB.Label Label5 
         Caption         =   "N=P*Q"
         Height          =   375
         Left            =   6000
         TabIndex        =   10
         Top             =   480
         Width           =   615
      End
      Begin VB.Label Label4 
         Caption         =   "密钥d"
         Height          =   255
         Left            =   3000
         TabIndex        =   8
         Top             =   1080
         Width           =   495
      End
      Begin VB.Label Label3 
         Caption         =   "公钥E"
         Height          =   255
         Left            =   120
         TabIndex        =   6
         Top             =   1080
         Width           =   495
      End
      Begin VB.Label Label2 
         Caption         =   "Q"
         Height          =   375
         Left            =   3240
         TabIndex        =   4
         Top             =   360
         Width           =   495
      End
      Begin VB.Label Label1 
         Caption         =   "P"
         Height          =   375
         Left            =   240
         TabIndex        =   2
         Top             =   480
         Width           =   615
      End
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Public n As Long
Public e As Long
Public s As Long
Public d As Long
Public r As Long
Public z As Long
Private Sub Command1_Click()
Text6.Enabled = True
Text7.Enabled = True
Text8.Enabled = True


'判断输入的P是否为素数m=0不是素数m=1时为素数及文本不能为空

If Text1.Text <> "" Then
If Int(Text1.Text) > 0 Then
Call prime.prime(Text1.Text)
If m = "0" Then
MsgBox "您输入的P不是素数", vbInformation, "提示"
Text1.Text = ""
Text1.SetFocus
Exit Sub
End If
Else
MsgBox "请输入一个大于0的质数", vbInformation, "提示"
Text1.SetFocus
Exit Sub
End If
Else
MsgBox "输入不能为空", vbInformation, "提示"
Text1.SetFocus
Exit Sub
End If

'判断输入的Q是否为素数及文本不能为空,当为素数时执行相应的程序

If Text2.Text <> "" Then
If Int(Text2.Text) > 0 Then
Call prime.prime(Text2.Text)
If m = "0" Then
MsgBox "您输入的Q不是素数", vbInformation, "提示"
Text2.Text = ""
Text2.SetFocus
Exit Sub
End If
If m = "1" Then 'Q、P都为素数时计算出公钥及密钥
n = Int(Text1.Text) * Int(Text2.Text) '计算n的值
Text5.Text = n
s = (Int(Text1.Text) - 1) * (Int(Text2.Text) - 1)
'计算公钥,
For e = 2 To s
s = (Int(Text1.Text) - 1) * (Int(Text2.Text) - 1)
Randomize
e = Rnd() * (s - 2) + 2
z = e
z = GCD(z, s)
If z = 1 Then
Text3.Text = e

'计算密钥
Dim a As Long
s = (Int(Text1.Text) - 1) * (Int(Text2.Text) - 1)
For d = 2 To s
s = (Int(Text1.Text) - 1) * (Int(Text2.Text) - 1)
a = (Int(Text3.Text) * d) Mod s
If a = 1 Then
Text4.Text = d
Exit For
End If
Next
Exit For
End If
Next
End If
Else
MsgBox "请输入一个大于0的质数", vbInformation, "提示"
Text2.SetFocus
Exit Sub
End If
Else
MsgBox "输入不能为空", vbInformation, "提示"
Text2.SetFocus
Exit Sub
End If

End Sub
Private Function GCD(x As Long, y As Long) As Long '判断两个数的公约数
Do Until y = 0
r = x Mod y
x = y
y = r
Loop
GCD = x
End Function

Private Sub Command2_Click()
Dim count As Long
Dim temp As Long
Dim m1 As Long
Dim i As Long
Dim l As Long
Dim b As Long
Text7.Text = ""
Text8.Text = ""
Text9.Text = ""
For l = 1 To Len(Text6.Text) '将输入的字符转换为ascll码
b = Asc(Mid(Text6.Text, l, 1))
temp = b Mod n

m1 = 1    '生成密文
For i = 1 To e
m1 = (m1 * temp) Mod n
Next

Text7.Text = Text7.Text + Str(m1)
Text7.Text = Replace(LTrim(Text7.Text), " ", "@") '生成密文中的空格用“@”代替
Next
End Sub

Private Sub Command3_Click()
Dim bytarray() As String
Dim count As Long
Dim temp As Long
Dim m2 As Long
Dim i As Long
Dim b As Long
Dim l As Long
bytarray = Split(Text8.Text, "@") '将密文中有“@”前面的字符取出存入数组
For l = 0 To UBound(bytarray)
b = Int(bytarray(l))
temp = b Mod n

'生成明文

m2 = 1
For i = 1 To d
m2 = (m2 * temp) Mod n
Next
Text9.Text = Text9.Text + Chr(m2)
Next


End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -