Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

Nelson_Hoffman

decrypt com SQL e C#

Recommended Posts

Nelson_Hoffman

boas povo preciso de ajuda, quero que o meu prog encripte as passwords guardadas na bd e quero que descripte quando faço login, estou com problemas na conversão de strings para bytes tambem

vou por aqui o código e sei que é um pouco grande mas se me derem 1a ajuda agradecia..

Código de Login:

private void login_Click(object sender, EventArgs e)
 {
	 ligacao = new SqlConnection (@"Data Source = ......");
	 string user = textBox1.Text;
	 string pass = textBox2.Text;
	 cmd = new SqlCommand("select username,password from login where username='" + textBox1.Text + "'and password='" + pass + "'", ligacao);
	 ligacao.Open();
	 adaptador = new SqlDataAdapter(cmd);
	 DataTable dt = new DataTable();
	 adaptador.Fill(dt);
	 if (dt.Rows.Count > 0)
	 {
		 Form2 form2 = new Form2();
		 form2.Show();
		 this.Hide();
	 }
	 else
	 {
		 MessageBox.Show("Username ou Password errado. Tente novamente.");
	 }
 }

e dp para programar a encriptaçao e decriptaçao usei este codigo

public void encryptPassword()
	{
	 string user = textBox1.Text;
	 string pass = textBox2.Text;
	 SqlParameter paramUsername;
	 paramUsername = new SqlParameter("@username", SqlDbType.VarChar, 10);
	 paramUsername.Value = user;
	 cmd.Parameters.Add(paramUsername);

	 //Encrypt the password
	 MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
	 byte[] hashedBytes;
	 UTF8Encoding encoder = new UTF8Encoding();
	 hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(pass));
	 SqlParameter paramPwd;
	 paramPwd = new SqlParameter("@Password", SqlDbType.Binary, 16);
	 paramPwd.Value = hashedBytes;
	 cmd.Parameters.Add(paramPwd);
		try
		{
			ligacao.Open();
			cmd.ExecuteNonQuery();
			MessageBox.Show("usuario criado");
		}
		catch
		{
			MessageBox.Show("erro");
		}
		finally
		{
			ligacao.Close();
		}
	}

public void decryptPassword()
	{
		string user = textBox1.Text;
		string pass = textBox2.Text;
		SqlParameter paramUser;
		paramUsername = new SqlParameter("@Username", SqlDbType.VarChar, 25);
		paramUser.Value = user;
		cmd.Parameters.Add(paramUsername);


		//Hash the password
		MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
		byte[] hashedDataBytes;
		UTF8Encoding encoder = new UTF8Encoding();
		hashedDataBytes = md5Hasher.ComputeHash(encoder.GetBytes(pass));

		SqlParameter paramPwd;
		paramPwd = new SqlParameter("@Password", SqlDbType.Binary, 16);
		paramPwd.Value = hashedDataBytes;
		int iResults;
		try
		{
			ligacao.Open();
			//We use execute scalar, since we only need one cell
			iResults = Convert.ToInt32(cmd.ExecuteScalar().ToString());
		}
		catch
		//Connection failure (most likely, though
		//you can handle this exception however)
		{
			MessageBox.Show("erro2");
		}
		finally
		{
		 ligacao.Close();
		}

Agardeço a vossa ajuda. Cumps

Edited by apocsantos

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.