问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

RSA加密算法的API函数在VB中怎么用

发布网友 发布时间:2022-09-06 06:24

我来回答

1个回答

热心网友 时间:2024-12-12 04:26

Function check_prime(ByVal val As Long) As Boolean
Dim primes
primes = Array(1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397)
check_prime = False

For i = 0 To 78
If (val = primes(i)) Then
prime = True
End If
Next i
check_prime = prime
End Function

Function decrypt(ByVal c, ByVal n, ByVal d As Long)

Dim i, g, f As Long

On Error GoTo errorhandler

If (d Mod 2 = 0) Then
g = 1
Else
g = c
End If

For i = 1 To d / 2

f = c * c Mod n
g = f * g Mod n
Next i
decrypt = g

Exit Function
errorhandler:
Select Case Err.Number ' Evaluate error number.
Case 6
status.Text = "Calculation overflow, please select smaller values"
Case Else
status.Text = "Calculation error"
End Select

End Function

Function getD(ByVal e As Long, ByVal PHI As Long) As Long
Dim u(3) As Long
Dim v(3) As Long
Dim q, temp1, temp2, temp3 As Long

u(0) = 1
u(1) = 0
u(2) = PHI
v(0) = 0
v(1) = 1
v(2) = e

While (v(2) <> 0)
q = Int(u(2) / v(2))
temp1 = u(0) - q * v(0)
temp2 = u(1) - q * v(1)
temp3 = u(2) - q * v(2)
u(0) = v(0)
u(1) = v(1)
u(2) = v(2)
v(0) = temp1
v(1) = temp2
v(2) = temp3
Wend
If (u(1) < 0) Then
getD = (u(1) + PHI)
Else
getD = u(1)
End If
End Function

Function getE(ByVal PHI As Long) As Long
Dim great, e As Long

great = 0
e = 2

While (great <> 1)
e = e + 1
great = get_common_denom(e, PHI)
Wend
getE = e
End Function

Function get_common_denom(ByVal e As Long, ByVal PHI As Long)
Dim great, temp, a As Long

If (e > PHI) Then
While (e Mod PHI <> 0)
temp = e Mod PHI
e = PHI
PHI = temp
Wend
great = PHI
Else
While (PHI Mod e <> 0)
a = PHI Mod e
PHI = e
e = a
Wend
great = e
End If
get_common_denom = great
End Function

Private Sub show_primes()
status.Text = "1"
no_primes = 1
For i = 2 To 400
prime = True
For j = 2 To (i / 2)
If ((i Mod j) = 0) Then
prime = False
End If
Next j

If (prime = True) Then
no_primes = no_primes + 1
status.Text = status.Text + ", " + Str(i)
End If
Next i
status.Text = status.Text + vbCrLf + "Number of primes found:" + Str(no_primes)
End Sub

Private Sub Command1_Click()
Dim p, q, n, e, PHI, d, m, c As Long

p = Text1.Text
q = Text2.Text
If (check_prime(p) = False) Then
status.Text = "p is not a prime or is too large, please re-enter"
ElseIf (check_prime(q) = False) Then
status.Text = "q is not a prime or is too large, please re-enter"
Else
n = p * q
Text3.Text = n

PHI = (p - 1) * (q - 1)
e = getE((PHI))
d = getD((e), (PHI))
Text4.Text = PHI
Text5.Text = d
Text6.Text = e
m = Text7.Text

c = (m ^ e) Mod n
Text8.Text = c
m = decrypt(c, n, d)
Text9.Text = m
Label12.Caption = "Decrypt key =<" + Str(d) + "," + Str(n) + ">"
Label13.Caption = "Encrypt key =<" + Str(e) + "," + Str(n) + ">"
End If
End Sub

Private Sub Command2_Click()
End
End Sub

Private Sub Command3_Click()
frmBrowser.Show
End Sub

Private Sub Command4_Click()
Call show_primes
End Sub
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
昭君怨诗词大全(5首昭君怨古词) 道是春来花未,道是雪来香异。竹外一枝斜,野人家。词中吟诵的是 萌萌带着自己的压岁钱去买笔记本,买了2本后还剩1.8元。如果买同样的笔记... ...1.8元,买同样的作业本四本差2.4元,作业本多少钱一本。给算得方法说... ...如果买同样的硬面钞4本则差2.40元,那么小明带了多少钱? 小明带一些钱去买作业本,买两本之后还剩1.8元;如果买同样的练习本4本则... 我的QQ总是被盗,烦死了,我总要改密码,怎样才不会被盗密码? 我跟一个好兄弟绝交了有1年9个月,没有联系没有来往,但我的心一直惦记... ...了。最近他听说我生病做手术回来了,就去班长那里问我的近况,加我... 请问神舟七号载人航天飞行的圆满成功,将对我国经济产生哪些积极影响... 当当网api是如何调用的?当当的MD5加密算法什么样的?谢谢 功能描述:商家... 品胜充电电池2500豪安用着怎样啊 2500mah的充电电池应该充多久时间 2500mah的5号充电池充满电要多久? 什么鱼以小球藻为食 哪些食用鱼可池塘除藻? 心悸怎么读是什么意思 炸梅菜扣肉有什么小秘诀吗?如何才能让这道菜做得美味极致? 糖尿病可以吃梅干菜扣肉吗? vivo手机键盘变小怎么恢复? 烟机接水盘无锈钢和搪瓷哪个好 油烟机防水罩出水口周围破损漏水怎么修补? 全世界都找不到的第二个人的成语 找律师写起诉书可以吗 起诉状可以委托律师写吗 找律师写诉状可以吗 律师可以代当事人签诉状吗 牙黄牙疼是因为常喝的水有问题吗? 两天没联系,看到男朋友朋友圈发违章罚单要发信息安慰吗 世界上有四维空间么? 郑燮写的胸中之竹的全文解释 iPhone 4能升级IOS12吗?升级IOS12后性能会变好还是会变差? 魔兽世界 潜行者任务 抢劫海盗 Lumia手机用无线充电板充电时有噪音 怎么运用PS实现图片局部颜色的修改 关于arrive的用法及解释 俗话说得好狗咬老鼠劳而无功是什么意思? 居里夫人是放射性发现的关键人吗 网上银行和支付宝有什么关系 第三个字玉的成语有哪些 第三个字是玉的成语有那些? 第三个字是玉的成语 黑夜传说4剧情怎么样?黑夜传说4高清版 黑夜传说全集下载 《傅雷家书》个人心得感悟10篇 疯狂猜成语的第三个是玉的成语 玉字的成语是什么成语 会穿西服的女人,气质不会太差,酷飒的女人风你能驾驭吗? 有玉字的成语是 有什么带玉字的成语。知道的多说几个,越多越好。 第四个字是玉的成语有哪些