4/06/2009

Encriptar y Desencriptar Valores

Codigo para Encryptar y Desencrytar.... espero que les sirva

Imports Microsoft.VisualBasic

Imports System.Security.Cryptography

Imports System.Text

Imports System.IO

Public Class Class1

Public Shared Function Encryptar(ByVal strValor As String) As String

Dim strEncrKey As String = "&%#@?,:*"

Dim byKey() As Byte = {}

Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}

Try

byKey = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8))

Dim des As New DESCryptoServiceProvider

Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(strValor)

Dim ms As New MemoryStream

Dim cs As New CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write)

cs.Write(inputByteArray, 0, inputByteArray.Length)

cs.FlushFinalBlock()

Return Convert.ToBase64String(ms.ToArray())

Catch ex As Exception

Return ex.Message

End Try

End Function

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Public Shared Function Decrypt(ByVal strValor As String) As String

Dim sDecrKey As String = "&%#@?,:*"

Dim byKey() As Byte = {}

Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}

Dim inputByteArray(strValor.Length) As Byte

Try

byKey = System.Text.Encoding.UTF8.GetBytes(Left(sDecrKey, 8))

Dim des As New DESCryptoServiceProvider

If Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") = "x64" Then

If Trim(strValor).Length = 0 Then

Throw New Exception("Password No debe estar en Blanco")

End If

End If

inputByteArray = Convert.FromBase64String(strValor)

Dim ms As New MemoryStream

Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)

cs.Write(inputByteArray, 0, inputByteArray.Length)

cs.FlushFinalBlock()

Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8

Return encoding.GetString(ms.ToArray())

Catch ex As Exception

Return ex.Message

End Try

End Function

End Class