|
-
Apr 26th, 2009, 05:58 AM
#1
Thread Starter
Addicted Member
Data Encryption (Triple DES)
Here is a module that i created to encrpyt/decrypt data using VB's TripleDESCryptoServiceProvider. Hope this may be useful. I have commented code as much as possible so that you may be able to understand it a bit better:
Code:
Module Data
'Declare key and initialization vector to use for encryption'
Private Key() As Byte = {26, 15, 5, 46, 98, 21, 2, 36, 19, 4, 22, 50, 42, 14, 24, 18, 34, 16, 82, 76, 65, 91, 54, 78}
Private InitializationVector() As Byte = {65, 110, 68, 26, 69, 178, 200, 219}
Public Function Encrypt(ByVal Text As String) As Byte()
'Create UTF8 encoder to convert text into byte array'
Dim UTF8Encoder As System.Text.UTF8Encoding = New System.Text.UTF8Encoding()
Dim TextBytes() As Byte = UTF8Encoder.GetBytes(Text)
'Create triple DES service provider'
Dim TDESProvider As System.Security.Cryptography.TripleDESCryptoServiceProvider = New System.Security.Cryptography.TripleDESCryptoServiceProvider
Dim CryptoTransform As System.Security.Cryptography.ICryptoTransform = TDESProvider.CreateEncryptor(Key, InitializationVector)
'Create memory stream to store encrypted data'
Dim EncryptionStream As System.IO.MemoryStream = New System.IO.MemoryStream
Dim CryptoStream As System.Security.Cryptography.CryptoStream = New System.Security.Cryptography.CryptoStream(EncryptionStream, CryptoTransform, Security.Cryptography.CryptoStreamMode.Write)
'Write the encrypted information to the stream. Flush the information when done to ensure everything is out of the buffer'
CryptoStream.Write(TextBytes, 0, TextBytes.Length)
CryptoStream.FlushFinalBlock()
EncryptionStream.Position = 0
'Read the stream back into a Byte array and return it to the calling method'
Dim Result(EncryptionStream.Length - 1) As Byte
EncryptionStream.Read(Result, 0, EncryptionStream.Length)
CryptoStream.Close()
Return Result
End Function
Public Function Decrypt(ByVal TextBytes() As Byte) As String
'Create triple DES service provider'
Dim TDESProvider As System.Security.Cryptography.TripleDESCryptoServiceProvider = New System.Security.Cryptography.TripleDESCryptoServiceProvider
Dim CryptoTransform As System.Security.Cryptography.ICryptoTransform = TDESProvider.CreateDecryptor(Key, InitializationVector)
'Create memory stream to store decrypted data'
Dim DecryptionStream As System.IO.MemoryStream = New System.IO.MemoryStream
Dim CryptoStream As System.Security.Cryptography.CryptoStream = New System.Security.Cryptography.CryptoStream(DecryptionStream, CryptoTransform, Security.Cryptography.CryptoStreamMode.Write)
CryptoStream.Write(TextBytes, 0, TextBytes.Length)
CryptoStream.FlushFinalBlock()
DecryptionStream.Position = 0
'Read contents of memory stream, convert back to a string and return result'
Dim result(DecryptionStream.Length - 1) As Byte
DecryptionStream.Read(result, 0, DecryptionStream.Length)
CryptoStream.Close()
'Create UTF8 Encoder to convert bytes back to string'
Dim UTF8Encoder As System.Text.UTF8Encoding = New System.Text.UTF8Encoding()
Return UTF8Encoder.GetString(result)
End Function
End Module
Please note that that you can use any numbers for your key and initialization vector but the key must only be 24 bytes (24 number) and the initialization vector 8 bytes (8 numbers).
-
Oct 13th, 2009, 01:43 AM
#2
Member
Re: Data Encryption (Triple DES)
-
Sep 23rd, 2010, 07:17 AM
#3
Lively Member
Re: Data Encryption (Triple DES)
Thanks a lot! This was very helpful.
-
Sep 26th, 2010, 11:46 AM
#4
Re: Data Encryption (Triple DES)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|