Self-written encryption string, number, date class (VB.net)

Source: Internet
Author: User

Vs2005! However, if you use a key to encrypt text, numbers, and dates at the same time, it may be a bit of trouble! I just wrote a simple one and tested it! Still usable! Haha!

The Code is as follows:

 

Public class myencrytppassstring
'Self-compiled simple encryption and decryption text string class
'1. Keys: two. Define a key type: stringkeytype, which has two member types: Keya and keyb, and a public encryption string publicstr.
'II. encryption:
① Generate the final key and the validation string used for segmentation. The getendkey function is implemented.
'② Implement encryption text encrytpublicstring
'Iii. instance:
'Dim key as myencrytppassstring. stringkeytype = new myencrytppassstring. stringkeytype
'Dim KKK as myencrytppassstring
'Key. Keya = "fjaljfsalkjflakf"
'Key. keyb = "fjsljdfs"
'Key. publicstr = "Simple Method of exploring ideas"
'Kkk = new myencrytppassstring (key)
'Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles button1.click
'Me. textbox2.text = KKK. encryptstring (Me. textbox1.text. Trim)
'End sub
'Private sub button2_click (byval sender as system. Object, byval e as system. eventargs) handles button2.click
'Me. textbox1.text = KKK. decryptstring (Me. textbox2.text. Trim)
'End sub

Structure stringkeytype
Dim Keya as string
Dim keyb as string
Dim publicstr as string
End Structure

Public mykey as stringkeytype
Dim endkey as double ()
Dim endchr () as char
Dim endint as double = 0

Public sub getendkey (byval STR as stringkeytype)
Dim X as integer
Dim y as integer
Dim Z as double
'Determine the length of the longest string
If Str. Keya. length> Str. keyb. Length then
X = Str. Keya. Length
Else
X = Str. keyb. Length
End if
If x <Str. publicstr. Length then
X = Str. publicstr. Length
End if
'Initialize the final key Array
Redim endkey (X-1)
With Str
For y = 0 to. Keya. Length-1
Endkey (y) = ASCW (. Keya. Chars (y ))
Endint + = ASCW (. Keya. Chars (y ))
Next
For y = 0 to. keyb. Length-1
Endkey (y) = endkey (y) + ASCW (. keyb. Chars (y ))
Endint + = ASCW (. keyb. Chars (y ))
Next
For y = 0 to. publicstr. Length-1
Endkey (y) = endkey (y) + ASCW (. publicstr. Chars (y ))
Endint + = ASCW (. publicstr. Chars (y ))
Next
End
'Generate the character string used for text segmentation. the ASCII code is between and.
Redim endchr (Str. publicstr. Length-1)
For y = 0 to str. publicstr. Length-1
X = endkey (y)
Do
If x <= 123 then
X = x * 2
End if
If X> = 254 then
X = x/3
End if
If X> 123 and x <254 then
Endchr (y) = chrw (X)
Exit do
End if
Loop
Next
End sub

Public sub new (byval key as stringkeytype) 'Parameter
If key. Keya. Length <= 0 or key. keyb. Length <= 0 or key. publicstr. Length <= 0 then
Msgbox ("Incorrect Parameter: at least one member of the parameter key is not assigned a value! ")
End if
Getendkey (key)
End sub
'Encrypted text
Public Function encryptstring (byval STR as string) as string
Dim endstr as string = ""
Dim X as double
Dim y as integer
Dim Z as integer
Dim L as integer
If endkey is nothing = true then
Msgbox ("set the key first! ")
Return ""
End if
If STR = "" then
Return ""
End if
Z = 0
L = 0
For y = 0 to str. Length-1
X = endkey (l) + ASCW (endchr (z) + ASCW (Str. Chars (y ))
Endstr = endstr & X. tostring. Trim & endchr (z)
If z <endchr. Length-1 then
Z = z + 1
Elseif z = endchr. Length-1 then
Z = 0
End if
If l <endkey. Length-1 then
L = L + 1
Elseif L = endkey. Length-1 then
L = 0
End if
Next
Return endstr
End Function
'Decrypt the text
Public Function decryptstring (byval STR as string) as string
If STR = "" then
Return ""
End if
If endkey is nothing = true then
Msgbox ("set the key first! ")
Return ""
End if
Dim endstr as string = ""
Dim A as string = ""
Dim X as double
Dim Z, L, M, keyin, chrin as integer
Dim C as char
Try
Keyin = 0
Chrin = 0
M = 0
For z = 0 to str. Length-1
C = Str. Chars (z)
If C = endchr (chrin) then
A = Str. substring (M, Z-m) 'find a broken character
X =
X = x-endkey (keyin)-ASCW (endchr (chrin ))
Endstr = endstr + chrw (x) 'Restore character
If chrin <endchr. Length-1 then
Chrin = chrin + 1
Elseif chrin = endchr. Length-1 then
Chrin = 0
End if
If keyin <endkey. Length-1 then
Keyin = keyin + 1
Elseif keyin = endkey. Length-1 then
Keyin = 0
End if
M = z + 1
End if
Next
Catch ex as exception
Return "decryption error! "
End try
Return endstr
End Function
'Encrypt a number
Public Function encryptint (byval int as double) as double
'Use the double type, mainly to facilitate comprehensive processing of data types
Return (INT-endint)
End Function
'Decrypt a number.
Public Function decryptint (byval int as double) as double
Return (INT + endint)
End Function
'Encryption processing date
Public Function encryptdate (byval D as date) as double
Dim X as integer = endint
Dim year, month, day, HH, mm, SS as integer
Dim s as string
Do
If x <1 then
X = x * 2 + 1
End if
If X> 11 then
X = x/3
End if
If X> 1 and x <12 then
Exit do
End if
Loop
Year = D. Year-(x * 3)
Month = D. Month + x
Day = D. day + x
HH = D. Hour + x
Mm = D. Minute + x
Ss = D. Second + x

S = year
If month> 9 then
S = S & month
Elseif month <10 then
S = S & "0" & month
End if
If day> 9 then
S = S & day
Elseif day <10 then
S = S & "0" & day
End if
If HH> 9 then
S = S & HH
Elseif hh <10 then
S = S & "0" & HH
End if
If MM> 9 then
S = S & mm
Elseif mm <10 then
S = S & "0" & mm
End if
If SS> 9 then
S = S & SS
Elseif SS <10 then
S = S & "0" & SS
End if
Return ctype (S, double)
End Function
'Decrypt date data
Public Function decryptdate (byval xdate as double) as datetime
Dim X as integer = endint
Dim year, month, day, HH, mm, SS as integer
Dim s as string
Do
If x <1 then
X = x * 2 + 1
End if
If X> 11 then
X = x/3
End if
If X> 1 and x <12 then
Exit do
End if
Loop
S = xdate. tostring
Ss = ctype (mid (S, S. Length-1, 2), integer)
Mm = ctype (mid (S, S. Length-3, 2), integer)
HH = ctype (mid (S, S. Length-5, 2), integer)
Day = ctype (mid (S, S. Length-7, 2), integer)
Month = ctype (mid (S, S. Length-9, 2), integer)
Year = ctype (mid (s, 1, S. Length-10), integer)

SS-= x
Mm-= x
Hh-= x
Day-= x
Month-= x
Year + = (x * 3)

Return new datetime (year, month, day, HH, mm, SS)
'The here seems to be a bug. The generated time is not hour, minute, or second. It may be because vs2005 functions are faulty and dizzy!
End Function

End Class

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.