Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have designed a Log In System using c# where the username and password is checked in SQL server 2008 before loading the main page.I wish to encrypt the stored password on the database.Is it possible to do it using c# and SHA1 algorithm?

Following is my stored procedure

SQL
ALTER procedure [dbo].[proc_UserLogin]
 @userid varchar(20),
  @password nvarchar(50)
  As 

  declare
  @ReturnVal              varchar(500)


SET NOCOUNT ON      

  if exists(select userid,password from LoginManager where userid=@userid and password=@password)
  set @ReturnVal='0|Logged in Successfully'
  else
  set @ReturnVal='1|Login Failed/Username does not exist'

  select @ReturnVal


C# Code

C#
public void button1_Click(object sender, EventArgs e)
        {
            mainform = new Form1();
            string[] v;

            OleDbConnection conn = new OleDbConnection("File Name=E:\\Vivek\\License Manager\\License Manager\\login.udl");

            try
            {

                conn.Open();
                string query = "EXEC dbo.proc_UserLogin'" + username.Text+ "', '" + password.Text+"'";
                OleDbCommand cmd = new OleDbCommand(query, conn);
                string s = Convert.ToString(cmd.ExecuteScalar());
                v= s.Split('|');
                if (v[0]=="0")
                {
                    mainform.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("Please enter correct user credentials and try again");
                }


             }

             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
             }


              conn.Close();

         }

Can anyone suggest changes to the code ,so that password encryption can be accomplished.?

Thanks
Posted
Comments
ZurdoDev 8-Oct-14 8:45am    
Just search google or this site for c# encryption. There are tons of examples.
Richard Deeming 8-Oct-14 8:45am    
Your code is susceptible to SQL Injection[^]. You should fix that gaping security vulnerability before worrying about encrypting your stored procedure!
Richard Deeming 8-Oct-14 8:47am    
Once you've fixed the code, take a look at this article[^] to see how to store passwords properly.

Hint: Encrypting passwords is a very bad idea - almost as bad as storing them in plain text.

1 solution

You can Encrypt a user password using Scalar function in SQL Server.
lets see

Execute the below code in SSMS( Below function return type is varchar )


USE [DB_NAME]
GO
/****** Object:  UserDefinedFunction [dbo].[PSWD_ENCRYPT]    Script Date: 10/8/2014 10:36:47 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<MAHESH>
-- Create date: <04-11-2013>
-- Description:	<Password Encryption using SHA Algorithm>
-- =============================================
Create FUNCTION [dbo].[PSWD_ENCRYPT] 
(
	-- Add the parameters for the function here
	@pwd varchar(50)

)
returns varchar(50)
AS
BEGIN
	-- Declare the return variable here
	declare @Result varchar(50);
		declare @hexbin varbinary(max)
	set @hexbin = HASHBYTES('SHA1',HashBytes('SHA1', @pwd));
	set @Result = (select  cast('' as xml).value('xs:hexBinary(sql:variable("@hexbin") )', 'varchar(max)'));
	-- Return the result of the function
	RETURN  @Result

END



Usage

Insertion

SQL
declare @pswd varchar(50)
set @pswd='mahesh';
insert into dbo.users(uid,pswd) values ('mahesh',dbo.PSWD_ENCRYPT(@pswd))



Select:

SQL
declare @pswd varchar(50)
set @pswd='mahesh';
select uid from dbo.users where uid=@uid and pswd=dbo.PSWD_ENCRYPT(@pswd)



Update :

SQL
update dbo.users set pswd=dbo.PSWD_ENCRYPT(@pswd) where .....
 
Share this answer
 
Comments
Richard MacCutchan 8-Oct-14 13:43pm    
Do not use encryption for storing passwords. Encrypted data can be decrypted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900