5 de jan. de 2010

Usando a Criptografia MD5

A algoritmo MD5 é um dos mais seguros algoritmos de criptografia existentes. Ele implementa um tipo de criptografia onde não é possível decriptar, sendo assim, é ideal para armazenar senhas. O exemplo abaixo mostra como usar este algoritmo na plataforma DotNET.

     MD5 em VB.Net

Imports System.Security.Cryptography

Public Function Encriptar_MD5(ByVal StrEntrada As String) As String
Dim ObjMD5 As New Security.Cryptography.MD5CryptoServiceProvider
Dim BytHash() As Byte = System.Text.Encoding.ASCII.GetBytes(StrEntrada)
Dim StrReturn As String = Nothing

BytHash = ObjMD5.ComputeHash(BytHash)

For Each ByteToEach As Byte In BytHash
StrReturn += ByteToEach.ToString("x2")
Next

Return StrReturn.ToUpper

End Function


     MD5 em C#

using System.Security.Cryptography;

public string Encriptar_MD5(string StrEntrada)
{
Security.Cryptography.MD5CryptoServiceProvider ObjMD5 = new Security.Cryptography.MD5CryptoServiceProvider();
byte[] BytHash = System.Text.Encoding.ASCII.GetBytes(StrEntrada);
string StrReturn = null;

BytHash = ObjMD5.ComputeHash(BytHash);

foreach (byte ByteToEach in BytHash) {
StrReturn += ByteToEach.ToString("x2");
}

return StrReturn.ToUpper;
}

Por hoje é só pe pe pessoal...

Nenhum comentário:

Postar um comentário