DES is a symmetric encryption where a secret key is used to encrypt data and the very same key is used to decrypt it too.
The below snippet can be tested as follows: TextBox1.Text = "dotnetspider" TextBox2.Text = EncryptToBase64String(TextBox1.Text, "91817161") TextBox3.Text = DecryptFromBase64String(TextBox2.Text, "91817161")
Public Function EncryptToBase64String(ByVal stringToEncrypt As String, ByVal SEncryptionKey As String) As String Try 'Convert the Encryption Key to an array of Bytes Dim key As Byte() = {} key = System.Text.Encoding.UTF8.GetBytes(SEncryptionKey)
'Convert the original string to an array of Bytes Dim inputByteArray As Byte() = Encoding.UTF8.GetBytes(stringToEncrypt)
'set the initialization vector Dim IV As Byte() = {12, 34, 56, 78, 90, 92, 94, 96}
'create a memory stream. Dim ms As MemoryStream = New MemoryStream
'create a crypto service provider object Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider
'Create a CryptoStream using the memory stream,Key and the initialization vector Dim cs As CryptoStream = New CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write) cs.Write(inputByteArray, 0, inputByteArray.Length) cs.FlushFinalBlock()
'Return the encrypted byte array that represents the memory stream. Return Convert.ToBase64String(ms.ToArray) Catch e As Exception Return e.Message End Try End Function
Public Function DecryptFromBase64String(ByVal stringToDecrypt As String, ByVal sDecryptionKey As String) As String Try
'Convert the Decryption Key to an array of Bytes Dim key As Byte() = {} key = System.Text.Encoding.UTF8.GetBytes(sDecryptionKey)
'Convert the original string to an array of Bytes Dim inputByteArray((stringToDecrypt.Length)) As Byte inputByteArray = Convert.FromBase64String(stringToDecrypt)
'set the initialization vector Dim IV As Byte() = {12, 34, 56, 78, 90, 92, 94, 96}
'create a memory stream. Dim ms As MemoryStream = New MemoryStream
'create a crypto service provider object Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider
'Create a CryptoStream using the memory stream,Key and the initialization vector Dim cs As CryptoStream = New CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write) cs.Write(inputByteArray, 0, inputByteArray.Length) cs.FlushFinalBlock()
'Return the decrypted byte array that represents the memory stream. Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8 Return encoding.GetString(ms.ToArray)
Catch e As Exception Return e.Message End Try End Function
|
No responses found. Be the first to respond and make money from revenue sharing program.
|