Simple plus decryption algorithm-vb.net

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.