DES symmetrical encryption

zhaozj2021-02-16  65

When you use cookies or store data to your database, you often use encrypted decryption, MD5 is very easy, but sometimes it needs to be reversed. Then DES symmetry is more useful. Set a key and encrypt all data. Code is described below, prior statements only brother personal understanding, please exhibitions Imports SystemImports System.IOImports System.TextImports System.DiagnosticsImports System.Security.CryptographyImports System.Text.RegularExpressions

'Using standard DES symmetrical encryption public function encryptds (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

'Using Standard DES Symmetrical Decryption 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

转载请注明原文地址:https://www.9cbs.com/read-23950.html

New Post(0)