Logo
You
Code

Encriptar y Desencriptar base64

Autor YouCode - http://www.youcode.com.ar/visual-basic-net/encriptar-y-desencriptar-base64-426

Visual Basic .net , funciones para encriptar y desencriptar un string, valido para enc/desenc claves o numbre de usuario.

En realidad se puede usar para encriptar y desencriptar cualquier string.

Importar la libreria cryptography
Imports System.Security.Cryptography
las funciones:
Function Encriptar(ByVal dato As String) As String

        Dim IV() As Byte = ASCIIEncoding.ASCII.GetBytes("algo1234") 'La clave debe ser de 8 caracteres
        Dim EncryptionKey() As Byte = Convert.FromBase64String("rpaSPvIvVLlrcmtzPU9/c67Gkj7yL1S5") 
        
        Dim buffer() As Byte = Encoding.UTF8.GetBytes(dato)
        Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
        des.Key = EncryptionKey
        des.IV = IV

        Return Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length()))

    End Function

    Function Desencriptar(ByVal Dato As String) As String

        Dim IV() As Byte = ASCIIEncoding.ASCII.GetBytes("algo123") 'La clave debe ser de 8 caracteres
        Dim EncryptionKey() As Byte = Convert.FromBase64String("rpaSPvIvVLlrcmtzPU9/c67Gkj7yL1S5") 'No se puede alterar la cantidad de caracteres pero si la clave
        
        Dim buffer() As Byte = Convert.FromBase64String(Dato)
        Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
        des.Key = EncryptionKey
        des.IV = IV
        Return Encoding.UTF8.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length()))

    End Function
llamamos a la funcion que necesitemos y listo!
http://www.youcode.com.ar/visual-basic-net/encriptar-y-desencriptar-base64-426