Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Encrypt and Decrypt
Message
 
To
23/07/2003 09:55:25
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00812762
Message ID:
00813072
Views:
9
Here is a VB example:
' Feb/2003 - hcorrea@visionds.com
'
' This class encrypts and decrypts values. Basically
' it is a wrapper for some of the .NET cryptography 
' classes.

' Code for this class was originally developed by 
' Paolo Miserini [@datamat.it]. 
' Reference: http://www.dotnet247.com/247reference/msgs/13/65847.aspx
'
' To do list:
'   We should probably create shared version of Deciper and Ciper methods.
'
' Usage sample:
' Dim Cipher As New Cipher()
' Me.txtOriginalValue.Text = Cipher.DeCipher(Me.txtEncrypted.Text, Me.txtKey.Text)
'
'
' Dim Cipher As New Cipher()
' Me.txtEncrypted.Text = Cipher.Cipher(Me.txtOriginalValue.Text, Me.txtKey.Text)
'

Imports System.Text
Imports System.Security.Cryptography

Public Class Cipher

    Dim DefaultEncryptionKey As String
    Dim des As New TripleDESCryptoServiceProvider()
    Dim hashmd5 As New MD5CryptoServiceProvider()

    Public Sub New()
        ' Default encryption/decryption key. You should
        ' provide your own when you instanciate the class or
        ' pass a key value to Cipher and Decipher methods.
        DefaultEncryptionKey = "DefaulEncryptionKeyYouShouldProvideYourOwn"
    End Sub

    Public Function DeCipher(ByVal TextToDecipher As String) As String
        Return DeCipher(TextToDecipher, DefaultEncryptionKey)
    End Function

    Public Function DeCipher(ByVal TextToDecipher As String, ByVal EncryptionKey As String) As String
        Dim DeCipheredValue As String = Nothing
        des.Key = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(EncryptionKey))
        des.Mode = CipherMode.ECB
        Dim desdencrypt As ICryptoTransform = des.CreateDecryptor()
        Dim buff() As Byte = Convert.FromBase64String(TextToDecipher)
        DeCipheredValue = ASCIIEncoding.ASCII.GetString(desdencrypt.TransformFinalBlock(buff, 0, buff.Length))
        Return DeCipheredValue
    End Function

    Public Function Cipher(ByVal TextToCipher As String) As String
        Return Cipher(TextToCipher, DefaultEncryptionKey)
    End Function

    Public Function Cipher(ByVal TextToCipher As String, ByVal EncryptionKey As String) As String
        Dim CipheredValue As String = Nothing
        des.Key = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(EncryptionKey))
        des.Mode = CipherMode.ECB
        Dim desdencrypt As ICryptoTransform = des.CreateEncryptor()
        Dim MyASCIIEncoding = New ASCIIEncoding()
        Dim buff() As Byte = ASCIIEncoding.ASCII.GetBytes(TextToCipher)
        CipheredValue = Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length))
        Return CipheredValue
    End Function

End Class
Hector Correa
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform