Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Encrypt and Decrypt
Message
 
À
23/07/2003 09:55:25
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00812762
Message ID:
00813072
Vues:
10
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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform