Home > Article > Computer Tutorials > Write a string encryption program in vb 2008 to encrypt the letters of an input line of characters
Dim S1 As String
Dim S2 As String
S1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJ"
S2 = "abcdefghijklmnopqrstuvwxyzabcdefghij"
Dim LStr As String
LStr = "AXDCF1245CDxhsavs" # Keep the original text
Dim TStr As String
Dim i As Integer
Dim S As String
TStr = """
For i = 1 To Len(LStr)
S = Mid(LStr, i, 1)
If InStr(S1, S) 0 Then
S = Mid(S1, InStr(S1, S) 10, 1)
ElseIf InStr(S2, S) 0 Then
S = Mid(S2, InStr(S2, S) 10, 1)
End If
TStr = TStr & S
Next i
Print TStr
First the program defines two strings k1 and k2;
Here he provides a variable named Text1, which should be a global variable and has been defined before the program.
First, we need to define a string variable code to store the text in Text1. Then, use the LCase() function to convert the uppercase letters in Text1 to lowercase letters.
The encryption process begins, which is a cyclic process.
In this loop, set the range of i from 1 to the length of code
At the beginning of each loop, take out each letter in the code and assign it to the s variable.
Then determine if s is not an empty string, assign n as the "distance" between the letters in s and the letter a (it is assumed that you already understand this concept, if not, please think about it carefully) .
Assume that the position of the letter 'i' in the string s in the string code is the nth position. If n is not a multiple of 2, then we will add the nth letter to the string decode. This letter comes from String k1. If n is a multiple of 2, then we will add the nth letter from the string k2 to the string decode.
In the process of judging whether s is a null character just now, if the obtained s is indeed a null character, then we will jump to the following processing process.
Assuming that the entered letter is not a multiple of 2, then change the letter of space into the 27th letter of k1, which is p, and add it to the 27th letter of k2, which is z.
Finally assign decode to text2.
Actually, this procedure is very simple. First define two strings, each containing 27 letters, including 26 letters and a space. Then shuffle the order of one of the strings. Next, according to the string that needs to be encrypted, take out each letter one by one, and determine the mask letter corresponding to k1 or k2 according to the position of the letter in the string. In this way, encryption operations can be achieved.
Dim s As String
Private Sub Command1_Click()
's = InputBox ("Please enter an English string:")
s = "I have an English book. It has 1234 pages."
For i = 1 To Len(s)
C = UCase(Mid(s, i, 1))
If C >= "A" And C
C = Chr(Asc(C) - 3)
If C is less than "A", then C will be set to the corresponding character of the ASCII code plus 26.
End If
Mid(s, i, 1) = C
Next i
MsgBox ("The encrypted string is: " & vbCrLf & s)
End Sub
Private Sub Command2_Click()
For i = 1 To Len(s)
C = Mid(s, i, 1)
If C >= "A" And C
C = Chr(Asc(C) 3)
If C is greater than "Z", then C will be equal to Chr(Asc(C) - 26).
End If
Mid(s, i, 1) = C
Next i
MsgBox ("The decrypted string is: " & vbCrLf & s)
End Sub
The following is the VB program for encryption and decryption based on the last 2 digits of the student number:
Private Sub Form_Click()
x = Val(Right(Text1.Text, 2)) 'Get the last 2 digits of the student number
'The following is encryption
s = Text2.Text
s1 = s
For i = 1 To Len(s)
c = Mid(s1, i, 1)
Mid(s1, i, 1) = Chr(Asc(c) Xor x)
Next i
Text3.Text = s1
'The following is the decryption
s = s1
For i = 1 To Len(s)
c = Mid(s, i, 1)
Mid(s, i, 1) = Chr(Asc(c) Xor x)
Next i
Text4.Text = s
End Sub
The above is the detailed content of Write a string encryption program in vb 2008 to encrypt the letters of an input line of characters. For more information, please follow other related articles on the PHP Chinese website!