By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,694 Members | 1,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,694 IT Pros & Developers. It's quick & easy.

TripleDES class - Suggestions Requested.

P: n/a
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.Pad Right(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.Pad Right(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

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

Feb 16 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

"d4******@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.Pad Right(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.Pad Right(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

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

Feb 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.