Use of des symmetric encryption

Source: Internet
Author: User
Encryption in the Web site using cookies or storing data to the database often use encryption and decryption, MD5 very useful, but sometimes need to inverse. Then the DES symmetric encryption is more useful. Set a key, and then encrypt all the data. Code introduction as follows, the prior declaration only for the younger brother personal understanding, please advise
Imports System
Imports System.IO
Imports System.Text
Imports System.Diagnostics
Imports System.Security.Cryptography
Imports System.Text.RegularExpressions

' Symmetric encryption using standard des
Public Function encryptdes (ByVal sourcestr As String) as String

' Get Encodekey string from web.config
Dim Skey as String
Skey = ConfigurationSettings.AppSettings ("Encodekey")

' Put the input string into the byte array
Dim des as DESCryptoServiceProvider = New DESCryptoServiceProvider ()
Dim Inputbytearray as Byte ()
Inputbytearray = Encoding.Default.GetBytes (SOURCESTR)

' Set Encrypt object and Skey
Des. Key = ASCIIEncoding.ASCII.GetBytes (skey)
DES.IV = ASCIIEncoding.ASCII.GetBytes (skey)
Dim ms as MemoryStream = New MemoryStream ()
Dim cs as CryptoStream = New CryptoStream (MS, Des. CreateEncryptor (), CryptoStreamMode.Write)
Dim SW As StreamWriter = New StreamWriter (CS)
Sw. Write (SOURCESTR)
Sw. Flush ()
Cs. FlushFinalBlock ()
Ms. Flush ()
Return convert.tobase64string (Ms. GetBuffer (), 0, Ms. Length)

End Function

' Symmetric decryption using standard des
Public Function decryptdes (ByVal sourcestr As String) as String

' Get Encodekey string from web.config
Dim SKey as String
SKey = ConfigurationSettings.AppSettings ("Encodekey")

' Put the input string into the byte array
Dim des as DESCryptoServiceProvider = New DESCryptoServiceProvider ()

Des. Key = ASCIIEncoding.ASCII.GetBytes (SKey)
DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey)

Dim buffer as Byte () = convert.frombase64string (SOURCESTR)

Dim ms as MemoryStream = New MemoryStream (buffer)
Dim cs as CryptoStream = New CryptoStream (MS, Des. CreateDecryptor (), CryptoStreamMode.Read)
Dim sr as StreamReader = New StreamReader (CS)
return Sr. ReadToEnd ()

End Function



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.