Results 1 to 3 of 3

Thread: [RESOLVED] Triple DES Encryption

  1. #1

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

    Resolved [RESOLVED] Triple DES Encryption

    I have created the following module for data encryption:

    Code:
    Module Data
    
        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 New System.Text.UTF8Encoding
            Dim TextBytes As Byte() = UTF8Encoder.GetBytes(Text)
    
            'Create triple DES service provider'
            Dim TDESProvider As 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 New System.IO.MemoryStream
            Dim CryptoStream As 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()
            Encrypt = Result
    
        End Function
    
    
        Public Function Decrypt(ByVal TextBytes() As Byte) As String
    
            'Create UTF8 Encoder to convert bytes back to string'
            Dim UTF8Encoder As New System.Text.UTF8Encoding()
    
            'Create triple DES service provider'
            Dim TDESProvider As New System.Security.Cryptography.TripleDESCryptoServiceProvider
    
            Dim CryptoTransform As System.Security.Cryptography.ICryptoTransform = TDESProvider.CreateEncryptor(Key, InitializationVector)
    
            'Create memory stream to store decrypted data'
            Dim DecryptionStream As New System.IO.MemoryStream
            Dim CryptoStream As 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)
            DecryptionStream.Close()
            Return UTF8Encoder.GetString(result)
    
        End Function
    
    End Module
    The problem im having is it is not decrypting the data correctly. For example if i encrypt the word "Hello" it is encrypted into a byte array correctly. Then when i decrypt it it returns the following string: ":��q��;&S���o��"

    Any help will be appreciated as i cant get my head around it

  2. #2
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    Re: Triple DES Encryption

    hey mouse88

    try following ...

    not mine taken from here

    vb Code:
    1. '**************************************
    2. ' Name: clsTripleDES.vb
    3. ' Description:Encryption / Decryption of String
    4. ' By: Iain Kusel
    5. '
    6. ' Assumes:Triple DES algorithm used in
    7. ' combination with conversion to base64 string within
    8. ' Encrypted / Decrypted properties for easy storage in e.g. database.
    9. ' Uses MemoryStream as opposed to FileStream.
    10. ' Key() should be stored in seperate binary.
    11.    
    12.     Imports System
    13.     Imports System.IO
    14.     Imports System.Text
    15.     Imports System.Security.Cryptography
    16.     Imports System.Xml
    17.     Public Class clsTES
    18.     Private _CreditCardNumber As String
    19.     Private _EncCreditCardNumber As String
    20.     Public Property CreditCardNumber() As String
    21.     Get
    22.     Return _CreditCardNumber
    23.     End Get
    24.     Set(ByVal Value As String)
    25.     'exit if no value
    26.     If Value = String.Empty Then Exit Property
    27.     _CreditCardNumber = Value
    28.     'encrypt class ref. set
    29.     Dim objTES As New clsTES()
    30.     'encrypt ccard string
    31.     Dim baEnc As Byte() = objTES.Encrypt(_CreditCardNumber)
    32.     'get base64 string from byte array
    33.     Dim s64 As String = System.Convert.ToBase64String(baEnc)
    34.     'set class string enc.
    35.     _EncCreditCardNumber = s64
    36.     End Set
    37.     End Property
    38.     Public Property EncryptedCreditCardNumber() As String
    39.     Get
    40.     Return _EncCreditCardNumber
    41.     End Get
    42.     Set(ByVal Value As String)
    43.     'exit if no value
    44.     If Value = String.Empty Then Exit Property
    45.     If _CreditCardNumber = String.Empty Then Exit Property
    46.     _EncCreditCardNumber = Value
    47.     'decrypt class ref. set
    48.     Dim objTES As New clsTES()
    49.     'get byte array from base64 string i.e. from db stored as base64
    50.     Dim baDec As Byte() = System.Convert.FromBase64String(_CreditCardNumber)
    51.     'decrypt ccard string
    52.     Dim sDec As String = objTES.Decrypt(baDec)
    53.     'set string decrypted
    54.     _CreditCardNumber = sDec
    55.     End Set
    56.     End Property
    57.     'keys
    58.     Private key() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
    59.     Private iv() As Byte = {65, 110, 68, 26, 69, 178, 200, 219}
    60.     Public Function Encrypt(ByVal plainText As String) As Byte()
    61.     ' Declare a UTF8Encoding object so we may use the GetByte
    62.     ' method to transform the plainText into a Byte array.
    63.     Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
    64.     Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)
    65.     ' Create a new TripleDES service provider
    66.     Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
    67.     ' The ICryptTransform interface uses the TripleDES
    68.     ' crypt provider along with encryption key and init vector
    69.     ' information
    70.     Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(Me.key, Me.iv)
    71.     ' All cryptographic functions need a stream to output the
    72.     ' encrypted information. Here we declare a memory stream
    73.     ' for this purpose.
    74.     Dim encryptedStream As MemoryStream = New MemoryStream()
    75.     Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream, _
    76.     cryptoTransform, CryptoStreamMode.Write)
    77.     ' Write the encrypted information to the stream. Flush the information
    78.     ' when done to ensure everything is out of the buffer.
    79.     cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
    80.     cryptStream.FlushFinalBlock()
    81.     encryptedStream.Position = 0
    82.     ' Read the stream back into a Byte array and return it to the calling
    83.     ' method.
    84.     Dim result(encryptedStream.Length - 1) As Byte
    85.     encryptedStream.Read(result, 0, encryptedStream.Length)
    86.     cryptStream.Close()
    87.     'test section
    88.     'Convert to / fro the memory stream to a base64 string
    89.     'compare decText to result
    90.     Dim encText As String = Convert.ToBase64String(encryptedStream.ToArray())
    91.     Dim decText As Byte() = Convert.FromBase64String(encText)
    92.     Dim sDec As String = Decrypt(decText)
    93.     Return result
    94.     End Function
    95.     Public Function Decrypt(ByVal inputInBytes() As Byte) As String
    96.     ' UTFEncoding is used to transform the decrypted Byte Array
    97.     ' information back into a string.
    98.     Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
    99.     Dim tdesProvider As TripleDESCryptoServiceProvider = New _
    100.     TripleDESCryptoServiceProvider()
    101.     ' As before we must provide the encryption/decryption key along with
    102.     ' the init vector.
    103.     Dim cryptoTransform As ICryptoTransform = _
    104.     tdesProvider.CreateDecryptor(Me.key, Me.iv)
    105.     ' Provide a memory stream to decrypt information into
    106.     Dim decryptedStream As MemoryStream = New MemoryStream()
    107.     Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, _
    108.     cryptoTransform, CryptoStreamMode.Write)
    109.     cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
    110.     cryptStream.FlushFinalBlock()
    111.     decryptedStream.Position = 0
    112.     ' Read the memory stream and convert it back into a string
    113.     Dim result(decryptedStream.Length - 1) As Byte
    114.     decryptedStream.Read(result, 0, decryptedStream.Length)
    115.     cryptStream.Close()
    116.     Dim myutf As UTF8Encoding = New UTF8Encoding()
    117.     Return myutf.GetString(result)
    118.     End Function
    119.     End Class
    * If my post helped you, please Rate it
    * If your problem is solved please also mark the thread resolved it is there in right top of page under thread tools
    * Why Rating is useful

  3. #3

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

    Thumbs up Re: Triple DES Encryption

    Ok appreciate the help have discovered where my code was going wrong though. if anyone wants to view this at all the error was in the decrypt function where this line:

    Code:
    Dim CryptoTransform As System.Security.Cryptography.ICryptoTransform = TDESProvider.CreateEncryptor(Key, InitializationVector)
    should have been:

    Code:
    Dim CryptoTransform As System.Security.Cryptography.ICryptoTransform = TDESProvider.CreateDecryptor(Key, InitializationVector)
    In my code i was creating an encryptor rather than a decryptor.

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