Hey, I've collected a code snippet to encrypt and decrypt a string and found some code to make it a disposable class but not sure if did it properly.
First that _Key and _IV are constant private, possible to remove the Key and IV properties?
And under Dispose If disposing Then what to dispose here?
Friend NotInheritable Class secureDES
Implements IDisposable
Private _Key As String
Private _IV As String
Private Disposed As Boolean = False
Sub New() '(ByVal Key As String)
On Error Resume Next
_Key = "XXXXXXXXXXXXXXXX"
_IV = "ZZZZZZZZZZZZZZZZ"
End Sub
Private Property Key() As String
Get
Return _Key
End Get
Set(ByVal Value As String)
_Key = Value
End Set
End Property
Private Property IV() As String
Get
Return _IV
End Get
Set(ByVal Value As String)
_IV = Value
End Set
End Property
Private Function GenerateKey() As String
Dim Ret As String = String.Empty
Try
Using Key As New DESCryptoServiceProvider
Ret = _ByteArrayToHexString(Key.Key)
End Using
Catch Exception As Exception
Exit Try
End Try
Return Ret
End Function
Private Function GenerateIV() As String
Dim Ret As String = String.Empty
Try
Using IV As New DESCryptoServiceProvider
Ret = _ByteArrayToHexString(IV.IV)
End Using
Catch Exception As Exception
Exit Try
End Try
Return Ret
End Function
Friend Function EncryptString(ByVal Value As String) As String
Dim Ret As String = String.Empty
Try
Using Key As New DESCryptoServiceProvider
Key.Key = _HexStringToByteArray(_Key)
Key.IV = _HexStringToByteArray(_IV)
Ret = _ByteArrayToHexString(_Encrypt(Value, Key))
Return Ret
End Using
Catch Exception As Exception
Exit Try
End Try
Return Ret
End Function
Friend Function DecryptString(ByVal Value As String) As String
Dim Ret As String = String.Empty
Try
Using Key As New DESCryptoServiceProvider
Key.Key = _HexStringToByteArray(_Key)
Key.IV = _HexStringToByteArray(_IV)
Ret = _Decrypt(_HexStringToByteArray(Value), Key)
Return Ret
End Using
Catch Exception As Exception
Exit Try
End Try
Return Ret
End Function
Private Function _Encrypt(ByVal PlainText As String, ByVal Key As SymmetricAlgorithm) As Byte()
Dim Ret As Byte() = Nothing
Try
Using MyMemoryStream As New MemoryStream
Using MyCryptoStream As New CryptoStream(MyMemoryStream, Key.CreateEncryptor(), CryptoStreamMode.Write)
Using MyStreamWriter As New StreamWriter(MyCryptoStream)
MyStreamWriter.WriteLine(PlainText)
End Using
Ret = MyMemoryStream.ToArray()
Return Ret
End Using
End Using
Catch Exception As Exception
Exit Try
End Try
Return Ret
End Function
Private Function _Decrypt(ByVal CypherText() As Byte, ByVal Key As SymmetricAlgorithm) As String
Dim Ret As String = String.Empty
Try
Using MyMemoryStream As New MemoryStream(CypherText)
Using MyCryptoStream As New CryptoStream(MyMemoryStream, Key.CreateDecryptor(), CryptoStreamMode.Read)
Using MyStreamReader As New StreamReader(MyCryptoStream)
Ret = MyStreamReader.ReadLine()
End Using
End Using
End Using
Catch Exception As Exception
Exit Try
End Try
Return Ret
End Function
Private Function _HexStringToByteArray(ByVal Value As String) As Byte()
Dim Ret(Convert.ToInt32((Value.Length / 2) - 1)) As Byte
Try
For MyLoop As Integer = 0 To Ret.Length - 1
Ret(MyLoop) = Byte.Parse(Value.Substring(MyLoop * 2, 2), NumberStyles.HexNumber)
Next
Catch Exception As Exception
Exit Try
End Try
Return Ret
End Function
Private Function _ByteArrayToHexString(ByVal Value() As Byte) As String
Dim Ret As New StringBuilder
Try
For Each MyByte As Byte In Value
Ret.Append(MyByte.ToString("x2"))
Next
Catch Exception As Exception
Exit Try
End Try
Return Ret.ToString
End Function
Friend Sub Dispose() Implements IDisposable.Dispose
On Error Resume Next
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Sub Dispose(disposing As Boolean)
On Error Resume Next
If Not Disposed Then
If disposing Then
'What2Dispose?.Dispose()
'What2Dispose?.Dispose()
End If
Disposed = True
End If
End Sub
Protected Overrides Sub Finalize()
On Error Resume Next
Dispose(False)
MyBase.Finalize()
End Sub
End Class