Public Class Form1
Dim Charaarray () as Char
' Encryption
Private Sub Btn_en_click (sender as System.Object, E as System.EventArgs) Handles Btn_en.click
Dim S as String
Dim result as Boolean
result = Encryptordecrypt (Txt_plaintext_1.text, Txt_secrect_key.text, 1, s)
Txt_text_1.text = S
MessageBox.Show (IIf (result, "Success", "Failure"))
End Sub
' Decryption
Private Sub Btn_de_click (sender as System.Object, E as System.EventArgs) Handles Btn_de.click
Dim S as String
Dim result as Boolean
result = Encryptordecrypt (Txt_text_2.text, Txt_secrect_key.text,-1, s)
Txt_plaintext_2.text = S
MessageBox.Show (IIf (result, "Success", "Failure"))
End Sub
' Add decrypted string
' Str is a string to be encrypted
' Secretkey as secret key
' F for encryption and decryption, f=1-coded, f=-1-coded decryption
' Outputstr is the string after decryption
' Returns the result of the operation
Function Encryptordecrypt (ByVal str As String, ByVal Secretkey As Integer, ByVal f As Integer, ByRef outputstr as String) As Boolean
Outputstr = ""
If (String.isnullorwhitespace (str)) Then
Return False
End If
Dim C as Char
For i as Integer = 0 to Str. Length-1
c = str (i)
c = Encryptchar (c, Secretkey, F)
Outputstr &= C
Next I
Return True
End Function
Private Sub Form1_Load (sender as System.Object, E as System.EventArgs) Handles MyBase.Load
Charaarray = Createchararray ()
MessageBox.Show ("OK")
End Sub
Function Createchararray () as System.Array
Dim s as String = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789_"
Dim Rnd as New Random
Dim CArray (s.length-1) as Char
Dim Index as Integer
Dim I as Integer
i = 0
Do
index = rnd. Next (s.length)
If not Carray.contains (S (index)) then
CArray (i) = S (index)
i + = 1
End If
Loop while (I < s.length)
Return CArray
End Function
' Encrypt a single character
' C is the character to be encrypted
' Secretkey as secret key
' F for encryption and decryption, f=1-coded, f=-1-coded decryption
' Returns the characters after encryption
Function Encryptchar (ByVal c as Char, ByVal secretkey As Integer, ByVal f As Integer) as Char
Dim CIndex as Integer
Dim Len as Integer
Len = Charaarray.length
CIndex = Array.indexof (Charaarray, C)
If F = 1 Then
CIndex + = Secretkey
Else
CIndex-= Secretkey
End If
If (CIndex >= len) Then
Math.divrem (CIndex, Len, CIndex)
End If
If (CIndex < 0) Then
Do
CIndex + = Len
Loop while (CIndex < 0)
End If
Return Charaarray (CIndex)
End Function
End Class
Simple plus decryption algorithm-vb.net