How to encrypt and decrypt data in MD5 using C #?

11

How to make MD5 encryptions with C # of a text or file?

And once already encrypted, how to decrypt?

    
asked by anonymous 26.05.2014 / 21:27

3 answers

14

MD5 is not reversible, ie it is only possible to create the hash (or, as you said, encrypt) and compare with another hash. If they are the same, the password entered is considered the same.

Here's an example:

public static string GerarHashMd5(string input)
{
    MD5 md5Hash = MD5.Create();
    // Converter a String para array de bytes, que é como a biblioteca trabalha.
    byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

    // Cria-se um StringBuilder para recompôr a string.
    StringBuilder sBuilder = new StringBuilder();

    // Loop para formatar cada byte como uma String em hexadecimal
    for (int i = 0; i < data.Length; i++)
    {
        sBuilder.Append(data[i].ToString("x2"));
    }

    return sBuilder.ToString();
}
    
26.05.2014 / 21:36
1

MD5 is a function one-way hash, that is, it only encrypts, what you can do is encrypt saved in the database, after this you can receive the password from a login screen (EX;) and do the encryption process and do to search the bank for this password. Or if you want to buy two a variable with an MD5 in the application the example below demonstrates how to do this.

See worked here .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;


public class CriarMD5
{
    public string RetornarMD5(string Senha)
    {
        using (MD5 md5Hash = MD5.Create())
        {
            return RetonarHash(md5Hash, Senha);
        }
    }

    public bool ComparaMD5(string senhabanco, string Senha_MD5)
    {
        using (MD5 md5Hash = MD5.Create())
        {
            var senha = RetornarMD5(senhabanco);
            if (VerificarHash(md5Hash, Senha_MD5, senha))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    private string RetonarHash(MD5 md5Hash, string input)
    {
        byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

        StringBuilder sBuilder = new StringBuilder();

        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        return sBuilder.ToString();
    }

    private bool VerificarHash(MD5 md5Hash, string input, string hash)
    {
        StringComparer compara = StringComparer.OrdinalIgnoreCase;

        if (0 == compara.Compare(input, hash))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

public class Program
{
    public static void Main()
    {
        CriarMD5 md5 = new CriarMD5();

        var senhabanco = "827ccb0eea8a706c4c34a16891f84e7b"; 
        var Senha = "12345";

        Boolean ComparaSenha = md5.ComparaMD5(Senha, senhabanco);

        Console.WriteLine(ComparaSenha.ToString());
    }
}
    
23.08.2016 / 22:41
-2

It would look like this:

if(GerarHashMd5(senhaDigitada) == registroTabela.Senha) { ... }

You must convert the password entered by the user into MD5 and after comparing it with the MD5 password of the database.

    
23.08.2016 / 20:01