TripleDES

vb:
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography

Friend Class cTripleDES

    ' define the triple des provider
    Private m_des As New TripleDESCryptoServiceProvider

    ' define the string handler
    Private m_utf8 As New UTF8Encoding

    ' define the local property arrays
    Private m_key() As Byte
    Private m_iv() As Byte

    Public Sub New(ByVal key() As Byte, ByVal iv() As Byte)
        Me.m_key = key
        Me.m_iv = iv
    End Sub

    Public Function Encrypt(ByVal input() As Byte) As Byte()
        Return Transform(input, m_des.CreateEncryptor(m_key, m_iv))
    End Function

    Public Function Decrypt(ByVal input() As Byte) As Byte()
        Return Transform(input, m_des.CreateDecryptor(m_key, m_iv))
    End Function

    Public Function Encrypt(ByVal text As String) As String
        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) As String
        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()
        ' create the necessary streams
        Dim memStream As MemoryStream = New MemoryStream
        Dim cryptStream As CryptoStream = New _
            CryptoStream(memStream, CryptoTransform, _
            CryptoStreamMode.Write)
        ' transform the bytes as requested
        cryptStream.Write(input, 0, input.Length)
        cryptStream.FlushFinalBlock()
        ' Read the memory stream and convert it back into byte array
        memStream.Position = 0
        Dim result(CType(memStream.Length - 1, System.Int32)) As Byte
        memStream.Read(result, 0, CType(result.Length, System.Int32))
        ' close and release the streams
        memStream.Close()
        cryptStream.Close()
        ' hand back the encrypted buffer
        Return result
    End Function

End Class


'''''''''''''''''''''''''''''''''
'Aquí un ejemplo de como usar la clase cTripleDES en tu codigo
'
'    ' define the local key and vector byte arrays
'    Private ReadOnly key() As Byte = _
'    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, _
'    15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
'    Private ReadOnly iv() As Byte = {8, 7, 6, 5, 4, 3, 2, 1}
'   
'    ' instantiate the class with the arrays
'    Private des As New cTripleDES(key, iv)
'   
'    ' for the example, define a variable with the encrypted value
'    Private ReadOnly encryptedData As String = "++XIiGymvbg="
'   
'    ' now, decrypt the data
'    Private decryptedData As String = des.Decrypt(encryptedData)
'   
'    ' the value of decryptedData should be "test",
'    ' but for our example purposes, let's re-encrypt it
'    Private newEncryptedData As String = des.Encrypt(decryptedData)
¿Ya le viste algún error? Dejanos tu correción ;-)

Antes de comentar: Gran parte de los ejercicios propuestos no tienen librerías debido a que Wordpress elimina los tags HTML. Si sabes/tienes/conoces las librerías que hacen falta, déjalo en los comentarios.

Otro punto antes de comentar, Si vas a sugerir un segmento de código en algún lenguaje debes hacerlo así:

De esta manera el código sale coloreado.

Otro punto importante para muchos que sienten que se les ignora: Todos los comentarios los reviso y en su debido momento los apruebo, pero ojo con el último párrafo.

Para poner los símbolos de las librerías sin que desaparezcan, debes escribir los símbolos de mayor > y menor qué < con su entidad HTML correspondiente, así como el símbolo de &

Mis Algoritmos se reserva el derecho de alterar, publicar o no los comentarios así como cambiar estas reglas de uso.

Si estas de acuerdo, adelante puedes comentar :)

P.D. No le hago tareas a nadie, mejor hagan la mía :P