Home All Groups Group Topic Archive Search About

TripleDES class - Suggestions Requested.

Author
16 Feb 2006 11:51 AM
d4v3y0rk@gmail.com
i have a class i found/wrote (meaning i found it originally and
tinkered with it to make it mine) and i would like some suggestions on
how to make it better. i am looking for things i need to add,
weaknesses, whatnot. Thanks in advance.

-----------------------------------------------------------------------------------------------------------------

Imports System.Security.Cryptography
Imports System.Security
Imports System.Text
Imports System.IO

Public Class encoder

    Private m_des As New TripleDESCryptoServiceProvider
    Private m_utf8 As New UTF8Encoding
    Private m_key() As Byte
    Private m_iv() As Byte = {8, 7, 6, 5, 4, 3, 2, 1}

    Public Function Encrypt(ByVal text As String, ByVal strPassword As
String) As String
        m_key =
System.Text.Encoding.UTF8.GetBytes(strPassword.PadRight(24, "&"))
        Dim input() As Byte = m_utf8.GetBytes(text)
        Dim output() As Byte = Transform(input,
m_des.CreateEncryptor(m_key, m_iv))
        Return Convert.ToBase64String(output)
    End Function

    Public Function Decrypt(ByVal text As String, ByVal strPassword As
String) As String
        m_key =
System.Text.Encoding.UTF8.GetBytes(strPassword.PadRight(24, "&"))
        Dim input() As Byte = Convert.FromBase64String(text)
        Dim output() As Byte = Transform(input,
m_des.CreateDecryptor(m_key, m_iv))
        Return m_utf8.GetString(output)
    End Function

    Private Function Transform(ByVal input() As Byte, ByVal
CryptoTransform As ICryptoTransform) As Byte()
        Dim memStream As MemoryStream = New MemoryStream
        Dim cryptStream As CryptoStream = New CryptoStream(memStream,
CryptoTransform, CryptoStreamMode.Write)
        cryptStream.Write(input, 0, input.Length)
        cryptStream.FlushFinalBlock()
        memStream.Position = 0
        Dim result(CType(memStream.Length - 1, System.Int32)) As Byte
        memStream.Read(result, 0, CType(result.Length, System.Int32))
        memStream.Close()
        cryptStream.Close()
        Return result
    End Function

End Class

-----------------------------------------------------------------------------------------------------------------

Author
16 Feb 2006 10:05 PM
TrtnJohn
Make it more general purpose.  For example:

1)  Create a constructor that can set the key data up
2)  Don't hard your Encrypt and Decrypt to only work for your specific need
today.  Make these types of functions general.
3)  If you want to add an Encrypt/Decrypt Password function make them
seperate functions or possibly a derived class.

BTW.  You do know there is an easier way to encrypt passwords?  Have you
ever tried using SHA256 instead of 3DES.  If you use SHA you don't need to
keep a key around somewhere.

Show quoteHide quote
"d4v3y***@gmail.com" wrote:

> i have a class i found/wrote (meaning i found it originally and
> tinkered with it to make it mine) and i would like some suggestions on
> how to make it better. i am looking for things i need to add,
> weaknesses, whatnot. Thanks in advance.
>
> -----------------------------------------------------------------------------------------------------------------
>
> Imports System.Security.Cryptography
> Imports System.Security
> Imports System.Text
> Imports System.IO
>
> Public Class encoder
>
>     Private m_des As New TripleDESCryptoServiceProvider
>     Private m_utf8 As New UTF8Encoding
>     Private m_key() As Byte
>     Private m_iv() As Byte = {8, 7, 6, 5, 4, 3, 2, 1}
>
>     Public Function Encrypt(ByVal text As String, ByVal strPassword As
> String) As String
>         m_key =
> System.Text.Encoding.UTF8.GetBytes(strPassword.PadRight(24, "&"))
>         Dim input() As Byte = m_utf8.GetBytes(text)
>         Dim output() As Byte = Transform(input,
> m_des.CreateEncryptor(m_key, m_iv))
>         Return Convert.ToBase64String(output)
>     End Function
>
>     Public Function Decrypt(ByVal text As String, ByVal strPassword As
> String) As String
>         m_key =
> System.Text.Encoding.UTF8.GetBytes(strPassword.PadRight(24, "&"))
>         Dim input() As Byte = Convert.FromBase64String(text)
>         Dim output() As Byte = Transform(input,
> m_des.CreateDecryptor(m_key, m_iv))
>         Return m_utf8.GetString(output)
>     End Function
>
>     Private Function Transform(ByVal input() As Byte, ByVal
> CryptoTransform As ICryptoTransform) As Byte()
>         Dim memStream As MemoryStream = New MemoryStream
>         Dim cryptStream As CryptoStream = New CryptoStream(memStream,
> CryptoTransform, CryptoStreamMode.Write)
>         cryptStream.Write(input, 0, input.Length)
>         cryptStream.FlushFinalBlock()
>         memStream.Position = 0
>         Dim result(CType(memStream.Length - 1, System.Int32)) As Byte
>         memStream.Read(result, 0, CType(result.Length, System.Int32))
>         memStream.Close()
>         cryptStream.Close()
>         Return result
>     End Function
>
> End Class
>
> -----------------------------------------------------------------------------------------------------------------
>
>