Results 1 to 4 of 4

Thread: Data Encryption (Triple DES)

  1. #1

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Post 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).

  2. #2
    Member
    Join Date
    Aug 2009
    Posts
    46

    Re: Data Encryption (Triple DES)

    edit sorry, wrong topic

  3. #3
    Lively Member
    Join Date
    Jun 2004
    Posts
    99

    Re: Data Encryption (Triple DES)

    Thanks a lot! This was very helpful.

  4. #4
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Data Encryption (Triple DES)

    Nice one.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width