How do function to encrypt password in C#?

Asked

Viewed 593 times

2

How do function to encrypt password in C#? I want to take 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;
    }
  • 3

    What have you tried to do, ? Have you researched any kind of encryption you want to use?

  • 4

    Raphael, it would help a lot if you could [Dit] your question and add a little more detail to understand the context. Maybe it will get a little long talk about all the ways to encrypt a password, without knowing how it will be used.

  • 3

    Welcome to Stack Overflow. Your question is a little broad. You could post the code you are using and explain a little more what you want. Taking advantage, do a [tour], to learn a little more about the operation of the site to increase your chances of getting a good response.

  • 1

1 answer

3


There are various forms of data encryption out there ( here for example ) and one of them is MD5 (it’s not safe to be an example), which is a 128-bit cryptographic hash (or cryptographic hash) function unidirectional (no return).

The class below has everything you need to create the MD5 (Rmd5) and compare it (Comparamd5) 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.