比如一个text1.text里面内容为123 text2.text2里的内容为密码 然后点击按钮 在text3.text中变成5324什么的
1个回答
展开全部
给邮箱吧,发你邮箱里。在这里直接发答案老是通不过审核
Function zh(s1 As String, s2 As String) As String
Dim Code As String, x As Long
Dim i As Long, k As Long, S As String
For i = 1 To Len(s1)
For k = 1 To Len(s2)
x = Asc(Mid(s1, i, 1)) Xor Asc(Mid(s2, k, 1))
Next k
Code = Code & Chr(x)
Next i
zh = Code
End Function
Private Sub Command1_Click()
Dim a As String, b As String
a = Text1.Text: b = Text2.Text
Text3.Text = zh(a, b)
Label3.Caption = "密文"
End Sub
Private Sub Command2_Click()
Text3.Text = zh(Text3.Text, Text2.Text)
Label3.Caption = "明文"
End Sub
Function zh(s1 As String, s2 As String) As String
Dim Code As String, x As Long
Dim i As Long, k As Long, S As String
For i = 1 To Len(s1)
For k = 1 To Len(s2)
x = Asc(Mid(s1, i, 1)) Xor Asc(Mid(s2, k, 1))
Next k
Code = Code & Chr(x)
Next i
zh = Code
End Function
Private Sub Command1_Click()
Dim a As String, b As String
a = Text1.Text: b = Text2.Text
Text3.Text = zh(a, b)
Label3.Caption = "密文"
End Sub
Private Sub Command2_Click()
Text3.Text = zh(Text3.Text, Text2.Text)
Label3.Caption = "明文"
End Sub
追问
邮箱是717907859@qq.com
这代码要是有2和其他的数组成一个组合 比如 123 那么23再变为明文的时候就没了。要是12 最后变为明文只有1 这是怎么回事
追答
呵呵,不好意思,是我没注意。
不是有2的原因,因为如果text1和text2中有相同的字符,那么在异或后,就会变成0,而vb中,chr(0)是空值,所以在解密回来的时候就不对了。比如说text1=111,text2= 1,那解密的时候就会出错。
我修改了一下,加了一个去掉text1和text2中相同的字符的函数,在加密之前调用,就ok了。
Private temp As String '定义一个全局变量,在解密时调用
Function zh(s1 As String, s2 As String) As String
Dim Code As String, x As Long
Dim i As Long, k As Long, S As String
For i = 1 To Len(s1)
For k = 1 To Len(s2)
x = Asc(Mid(s1, i, 1)) Xor Asc(Mid(s2, k, 1))
Next k
Code = Code & Chr(x)
Next i
zh = Code
End Function
Private Sub Command1_Click()
Dim a As String, b As String
a = Text1.Text: b = Text2.Text
b = qd(b, a) '调用去相同字符函数
S = zh(a, b)
Text3.Text = S
Label3.Caption = "密文"
End Sub
Private Sub Command2_Click()
Text3.Text = zh(Text3.Text, temp)
Label3.Caption = "明文"
End Sub
Function qd(s1 As String, s2 As String) As String
Dim i As Long, k As Long, x1 As String, x2 As String
temp = "X" '给全局变量temp一个初值,防止去掉相同字符后temp成空值
For i = 1 To Len(s1)
For k = 1 To Len(s2)
x1 = Mid(s1, i, 1)
x2 = Mid(s2, k, 1)
If x1 = x2 Then
GoTo lin1
End If
Next k
temp = temp & x1
lin1:
Next i
qd = temp
End Function
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询