Los mejores
trucos de informática
y
trucos de programación
.
Inicio
Trucos de Windows
Informáticos
Nosotros
Registrar
Menú Privado
Trucos Windows
Trucos de Windows 7
Trucos de Windows Vista
Trucos de Windows XP
Windows 2003 Server
Trucos de Messenger
Trucos de Office
Trucos de Redes
Trucos Programación
Visual Basic.NET
Visual Basic 6
Trucos C#
Trucos ASP.NET
Trucos ASP.NET 2.0
SQL Server 2000
SQL Server 2005
Javascript
Trucos CSS
Otros Trucos
Trucos de SEO
Posicionar una web
Trucos de Google
Trucos Hardware
Varios
Nosotros
Webs recomendadas
Videos
Videos de humor
Trucos
Animales
Baja de Jazztel
<< Truco Anterior
Siguiente truco >>
Trucos de Visual Basic.NET
Encriptación - Desencriptación
Para encriptar y desencriptar datos podemos utilizar las siguientes funciones:
Public Shared Function EncryptString(ByVal InputString As String, ByVal SecretKey As String, Optional ByVal CyphMode As CipherMode = CipherMode.ECB) As String
Try
Dim Des As New TripleDESCryptoServiceProvider
'Put the string into a byte array
Dim InputbyteArray() As Byte = Encoding.UTF8.GetBytes(InputString)
'Create the crypto objects, with the key, as passed in
Dim hashMD5 As New MD5CryptoServiceProvider
Des.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(SecretKey))
Des.Mode = CyphMode
Dim ms As MemoryStream = New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, Des.CreateEncryptor(), _
CryptoStreamMode.Write)
'Write the byte array into the crypto stream
'(It will end up in the memory stream)
cs.Write(InputbyteArray, 0, InputbyteArray.Length)
cs.FlushFinalBlock()
'Get the data back from the memory stream, and into a string
Dim ret As StringBuilder = New StringBuilder
Dim b() As Byte = ms.ToArray
ms.Close()
Dim I As Integer
For I = 0 To UBound(b)
'Format as hex
ret.AppendFormat("{0:X2}", b(I))
Next
Return ret.ToString()
Catch ex As System.Security.Cryptography.CryptographicException
ExceptionManager.Publish(ex)
Return ""
End Try
End Function
Public Shared Function DecryptString(ByVal InputString As String, ByVal SecretKey As String, Optional ByVal CyphMode As CipherMode = CipherMode.ECB) As String
If InputString = String.Empty Then
Return ""
Else
Dim Des As New TripleDESCryptoServiceProvider
'Put the string into a byte array
Dim InputbyteArray(CType(InputString.Length / 2 - 1, Integer)) As Byte '= Encoding.UTF8.GetBytes(InputString)
'Create the crypto objects, with the key, as passed in
Dim hashMD5 As New MD5CryptoServiceProvider
Des.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(SecretKey))
Des.Mode = CyphMode
'Put the input string into the byte array
Dim X As Integer
For X = 0 To InputbyteArray.Length - 1
Dim IJ As Int32 = (Convert.ToInt32(InputString.Substring(X * 2, 2), 16))
Dim BT As New ByteConverter
InputbyteArray(X) = New Byte
InputbyteArray(X) = CType(BT.ConvertTo(IJ, GetType(Byte)), Byte)
Next
Dim ms As MemoryStream = New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, Des.CreateDecryptor(), _
CryptoStreamMode.Write)
'Flush the data through the crypto stream into the memory stream
cs.Write(InputbyteArray, 0, InputbyteArray.Length)
cs.FlushFinalBlock()
'//Get the decrypted data back from the memory stream
Dim ret As StringBuilder = New StringBuilder
Dim B() As Byte = ms.ToArray
ms.Close()
Dim I As Integer
For I = 0 To UBound(B)
ret.Append(Chr(B(I)))
Next
Return ret.ToString()
End If
End Function
Pon el truco en tus favoritos
Compartir en:
Comentarios sobre Encriptación - Desencriptación
Francisco
dice:
Este codigo tambien es muy bueno. Thx
Comentario publicado 26/03/2010 1:53:00
gelacio martinez c
dice:
gracias por su informacion ...muy buena...
Comentario publicado 08/05/2010 0:22:00
¿?
dice:
¿?
Comentario publicado 28/08/2010 13:06:00
Dejar un comentario
Nombre:
Mail:
Web:
Comentario:
Contactar con mistrucos.net