How to make function to encrypt password in C #? [closed]

2

How to do function to encrypt password in C #? I want to get the password attribute and encrypt it.

    public String gravarCadastro(Cadastro cadastro)
    {
        string sql;
        int retorno;
        string resp;
        try
        {
            SqlConnection conexao = Conecta.getConexao(); //abre a conexão com o banco de dados

            sql = "INSERT INTO login ( cpf, nome, login, senha) ";
            sql += "VALUES (@cpf, @nome, @login, @senha)";
            SqlCommand cmd = conexao.CreateCommand();
            cmd.CommandText = sql;
            cmd.Parameters.AddWithValue("@cpf", cadastro.Cpf);
            cmd.Parameters.AddWithValue("@nome", cadastro.Nome);
            cmd.Parameters.AddWithValue("@login", cadastro.Login);
            cmd.Parameters.AddWithValue("@senha", cadastro.Senha);

            retorno = cmd.ExecuteNonQuery();
            if (retorno > 0)
            {
                resp = "Cadastro efetuado";
            }
            else
            {
                resp = "Cadastro não realizado";
            }

            //encerra a conexão com o banco de dados
            cmd.Dispose();
            conexao.Dispose();

        }
        catch (SqlException ex)
        {

            //caso a exceção seja a tentativa de inserir um CPF já cadastrado
            resp = "Erro " + ex.ToString();
            /*if (ex.Number == 2627)
            {
                resp = "CPF já cadastrado";
            }*/
        }
        return resp;
    }
    
asked by anonymous 07.10.2016 / 19:58

1 answer

3

There are several ways to encrypt data out there ( here for example ) and one of them is the MD5 ( it is not safe to be an example ), which is a 128-bit unidirectional cryptographic (or cryptographic hash function) ).

The class below has everything you need to create MD5 ( ReturnMD5 ) and compare it ( CompareMD5 ) with an MD5 already saved in your database.

using System;
using System.Security.Cryptography;
using System.Text;

namespace CriarMD5_
{
    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;
            }
        }
    }
}

I recommend reading this question .

    
07.10.2016 / 20:24