vb加密算法源码_vb字符串加密解密算法

hacker|
91

文章目录:

求一个简单短小的VB加密算法,每段要有解释

Public Function StringEnDeCodecn(strSource As String, MA) As String '该函数只对中西文起到加密作用 '参数为:源文件,密码 On Error GoTo ErrEnDeCode Dim X As Single Dim CHARNUM As Long, RANDOMINTEGER As Integer Dim SINGLECHAR As String * 1 Dim strTmp As String If MA 0 Then MA = MA * (-1) End If X = Rnd(-MA) For i = 1 To Len(strSource) Step 1 '取单字节内容 SINGLECHAR = Mid(strSource, i, 1) CHARNUM = Asc(SINGLECHAR) g: RANDOMINTEGER = Int(127 * Rnd) If RANDOMINTEGER 30 Or RANDOMINTEGER 100 Then GoTo g CHARNUM = CHARNUM Xor RANDOMINTEGER strTmp = strTmp Chr(CHARNUM) Next i StringEnDeCodecn = strTmp Exit Function ErrEnDeCode: StringEnDeCodecn = "" MsgBox Err.Number "\" Err.Description End Function Private Sub Command1_Click() tmp1 = StringEnDeCodecn(Text1.Text, 75) Text2.Text = tmp1 End Sub 窗体放两个文本框Text1,Text2和一个按钮Command1。 使用上面代码可以把Text1中的内容加密,显示在Text2中。 要解密直接把Text2的内容复制到Text1中,再次点击按钮Command1,解密后的内容会显示在Text2中。

请提供份RSA加密算法的VB源码

rem Simple RSA Program

rem (c) W.Buchanan

rem Jan 2002

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

vb代码哈希加密算法?

  Private Sub Initialize(ByVal vKeyString As String)

        Dim intI As Integer, intJ As Integer

        Randomize(Rnd(-1))  '得到初始值(种子值)

        '每次调用初始值均相同

        '根据初始值(种子值)得到随机数序列,每次调用Initialize时,初始值均相同。只要vKeyString相同,所产生的随机数序列一定相同 

        For intI = 1 To Len(vKeyString)

            intJ = Rnd(-Rnd * AscW(Mid(vKeyString, intI, 1)))

            Randomize(intJ)

        Next intI

    End Sub

    Public Sub DoXor(ByRef msFileText As String)

        Dim intC As Integer

        Dim intB As Integer

        Dim lngI As Long

        For lngI = 1 To Len(msFileText)

            intC = AscW(Mid(msFileText, lngI, 1))

            intB = Int(Rnd() * 2 ^ 7)

            '选用 =127可正确处理汉字,ChrW(n):n 有一个范围

            Mid(msFileText, lngI, 1) = ChrW(intC Xor intB)

        Next lngI

    End Sub

    Public Function Hash(ByVal ET As String) As String

        Dim BitLenString As String, KeyString As String,

        FileText As String

        BitLenString = "12345678"

        KeyString = ET  BitLenString

        Call Initialize(KeyString)

        '根据KeyString产生随机数序列

        FileText = ET  BitLenString

        Call DoXor(FileText)

        '根据上述随机数序列对FileText加密

        KeyString = FileText

        Call Initialize(KeyString)

        '根据上述的加密结果产生新的随机数序列

        FileText = BitLenString

        Call DoXor(FileText)

        '根据上述随机数序列对FileText加密,8位字符

        Hash = FileText

        '8位字符送作HASH值

    End Function

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

        Dim str As String = "abcd"  vbCrLf

        TextBox6.Text = str  Hash(str)

    End Sub

VB 加密算法

Private Declare Function icePub_machineGetInfo Lib "icePubDll.dll" (ByVal typeFlag As Integer, ByVal strInfo As String) As Integer

Dim a2 As Integer

Dim str1 As String

str1 = Space(1024 * 10)

a2 = icePub_machineGetInfo(1, str1)

MsgBox "mac:" + str1

a2 = icePub_machineGetInfo(2, str1)

MsgBox "cpu:" + str1

a2 = icePub_machineGetInfo(3, str1)

MsgBox "hard:" + str1

a2 = icePub_machineGetInfo(4, str1)

MsgBox "memery:" + str1

a2 = icePub_machineGetInfo(5, str1)

MsgBox "boarddate:" + str1

Private Declare Function icePub_machineCode Lib "icePubDll.dll" (ByVal flag As Integer, ByVal strMachineCode As String) As Integer

Dim a2 As Integer

Dim str1 As String

str1 = Space(1024 * 10)

a2 = icePub_machineCode(0,str1)

MsgBox str1

a2 = icePub_machineCode(1,str1)

MsgBox str1

Private Declare Function icePub_encryptText2 Lib "icePubDll.dll" (ByVal strInput As String,ByVal strOutputHexstring As String, ByVal strKey As String) As Integer

Dim len2 As Integer

Dim buff As String

Dim buff2 As String

Dim key As String

buff="Recall Dream Miss, Keep Silk-silk accept as a souvenir, Between You And Me, Stringed music touching."

key="11223344"

buff2=Space(1024)

len2=icePub_encryptionText2(buff,buff2,key)

MsgBox buff2

Private Declare Function icePub_decryptText2 Lib "icePubDll.dll" (ByVal strInputHexstring As String, ByVal strOutput As String, ByVal strKey As String) As Integer

Dim len2 As Integer

Dim buff As String

Dim buff2 As String

Dim key As String

buff="CCF8732A28BA4B6EC7460F43DD95CAEA4E8D100DD35A7667469015EB5722E0C2452D0E66895ECF294E3EAF39473B386E5999D0633F19296A13A44AF0BFAA38A956FBE465A57BA19C5C5FC86754AD029B39CF587EDD4651E20D06A92B8608F6ECD19841F52462A5A020479871017620FE"

key="11223344"

buff2=Space(1024)

len2= icePub_decryptText2(buff,buff2,key)

MsgBox buff2

如何用VB实现RSA加密算法,网上找到了一份代码,没有注释看不懂,请大神解释!!!

RSA算法非常简单,概述如下:

找两素数p和q

取n=p*q

取t=(p-1)*(q-1)

取任何一个数e,要求满足et并且e与t互素(就是最大公因数为1)

取d*e%t==1

这样最终得到三个数: n d e

设消息为数M (M n)

设c=(M**d)%n就得到了加密后的消息c

设m=(c**e)%n则 m == M,从而完成对c的解密。

注:**表示次方,上面两式中的d和e可以互换。

在对称加密中:

n d两个数构成公钥,可以告诉别人;

n e两个数构成私钥,e自己保留,不让任何人知道。

给别人发送的信息使用e加密,只要别人能用d解开就证明信息是由你发送的,构成了签名机制。

别人给你发送信息时使用d加密,这样只有拥有e的你能够对其解密。

rsa的安全性在于对于一个大数n,没有有效的方法能够将其分解

从而在已知n d的情况下无法获得e;同样在已知n e的情况下无法

求得d。

二实践

接下来我们来一个实践,看看实际的操作:

找两个素数:

p=47

q=59

这样

n=p*q=2773

t=(p-1)*(q-1)=2668

取e=63,满足et并且e和t互素

用perl简单穷举可以获得满主 e*d%t ==1的数d:

C:\Tempperl -e "foreach $i (1..9999){ print($i),last if $i*63%2668==1 }"

847

即d=847

最终我们获得关键的

n=2773

d=847

e=63

取消息M=244我们看看

加密:

c=M**d%n = 244**847%2773

用perl的大数计算来算一下:

C:\Tempperl -Mbigint -e "print 244**847%2773"

465

即用d对M加密后获得加密信息c=465

解密:

我们可以用e来对加密后的c进行解密,还原M:

m=c**e%n=465**63%2773 :

C:\Tempperl -Mbigint -e "print 465**63%2773"

244

即用e对c解密后获得m=244 , 该值和原始信息M相等。

三字符串加密

把上面的过程集成一下我们就能实现一个对字符串加密解密的示例了。

每次取字符串中的一个字符的ascii值作为M进行计算,其输出为加密后16进制

的数的字符串形式,按3字节表示,如01F

代码如下:

#!/usr/bin/perl -w

#RSA 计算过程学习程序编写的测试程序

#watercloud 2003-8-12

#

use strict;

use Math::BigInt;

my %RSA_CORE = (n=2773,e=63,d=847); #p=47,q=59

my $N=new Math::BigInt($RSA_CORE{n});

my $E=new Math::BigInt($RSA_CORE{e});

my $D=new Math::BigInt($RSA_CORE{d});

print "N=$N D=$D E=$E\n";

sub RSA_ENCRYPT

{

my $r_mess = shift @_;

my ($c,$i,$M,$C,$cmess);

for($i=0;$i length($$r_mess);$i++)

{

$c=ord(substr($$r_mess,$i,1));

$M=Math::BigInt-new($c);

$C=$M-copy(); $C-bmodpow($D,$N);

$c=sprintf "%03X",$C;

$cmess.=$c;

}

return \$cmess;

}

sub RSA_DECRYPT

{

my $r_mess = shift @_;

my ($c,$i,$M,$C,$dmess);

for($i=0;$i length($$r_mess);$i+=3)

{

$c=substr($$r_mess,$i,3);

$c=hex($c);

$M=Math::BigInt-new($c);

$C=$M-copy(); $C-bmodpow($E,$N);

$c=chr($C);

$dmess.=$c;

}

return \$dmess;

}

my $mess="RSA 娃哈哈哈~~~";

$mess=$ARGV[0] if @ARGV = 1;

print "原始串:",$mess,"\n";

my $r_cmess = RSA_ENCRYPT(\$mess);

print "加密串:",$$r_cmess,"\n";

my $r_dmess = RSA_DECRYPT($r_cmess);

print "解密串:",$$r_dmess,"\n";

#EOF

测试一下:

C:\Tempperl rsa-test.pl

N=2773 D=847 E=63

原始串:RSA 娃哈哈哈~~~

加密串:5CB6CD6BC58A7709470AA74A0AA74A0AA74A6C70A46C70A46C70A4

解密串:RSA 娃哈哈哈~~~

C:\Tempperl rsa-test.pl 安全焦点(xfocus)

N=2773 D=847 E=63

原始串:安全焦点(xfocus)

加密串:3393EC12F0A466E0AA9510D025D7BA0712DC3379F47D51C325D67B

解密串:安全焦点(xfocus)

MD5加密算法的解密源代码(VB的)

MD5加密算法: 见附件..

解密没有的,MD5不支持解密,因为在加密的时候是随机移位数的..

2条大神的评论

  • avatar
    访客 2022-07-13 下午 06:36:30

    3 = u(2) - q * v(2) u(0) = v(0) u(1) = v(1) u(2) = v(2) v(0) = temp1 v(1) = te

  • avatar
    访客 2022-07-13 下午 07:19:49

    not a prime or is too large, please re-enter" Else n = p * q Text3.

发表评论