Doubt over Encrypt Password - SQL Server

1

I am developing an ASP.NET MVC system, SQL Server database, which will have logins control, and to encrypt the password, I have developed the following functions and procedures:

CREATE FUNCTION [dbo].[ENCRIPTA_SENHA]
(
   -- Add the parameters for the function here
   @SENHA VARCHAR(200)
)
RETURNS VARBINARY(200)
AS
BEGIN
   DECLARE @pwd varchar(50) = 'maicongabriel', @RESULTADO VARBINARY(256)
   set @RESULTADO = ENCRYPTBYPASSPHRASE(@PWD, @SENHA)

   RETURN @RESULTADO
END
CREATE FUNCTION [dbo].[DECRIPTA_SENHA]
(
       -- Add the parameters for the function here
       @SENHA VARCHAR(200)
)
RETURNS VARCHAR(200)
AS
BEGIN

DECLARE @pwd varchar(50) = 'maicongabriel'

       RETURN CAST(DECRYPTBYPASSPHRASE(@pwd,@SENHA) As VARCHAR(200))

END
CREATE PROCEDURE [dbo].[VALIDA_SENHA]
       -- Add the parameters for the stored procedure here
         @USUARIO VARCHAR(200), @SENHA varchar(200)
AS
BEGIN
       -- SET NOCOUNT ON added to prevent extra result sets from
       -- interfering with SELECT statements.
       SET NOCOUNT ON;

    -- Insert statements for procedure here
       SELECT X.USUARIO FROM 
          (SELECT USUARIO, DBO.DECRIPTA_SENHA(SENHA) SENHA FROM USUARIOS_TESTE A ) AS X 
       WHERE X.USUARIO = @USUARIO AND X.SENHA = @SENHA

END

When I'm going to do the insert in the bank via ASP.NET MVC, I would do the same thing in more or less this way:

INSERT INTO USUARIOS_TESTE SELECT 'daniel', dbo.ENCRIPTA_SENHA('123456')

And when I process the login form, I would use my procedure and see if it has returned:

VALIDA_SENHA 'DANIEL','123456'

Is it a good practice to do this? Is there any better way? Because then the passwords will be encrypted in the database, and in the ASP.NET source code only the execution of ENCRIPTA_SENHA and VALIDA_SENHA ..

    
asked by anonymous 16.08.2018 / 16:47

1 answer

1

Encrypting the password in the database is perfect, but you should see that you are using direct encryption on INSERT , which means you should use it when you SELECT to compare the password.

Ideally, the password should not be "decrypted," the good practice is to use a Hash that can not be undone exactly for security.

To do this, use the HASHBYTES can be a good alternative. If you need the user can reset the password, but not "decrypt", this is the safest way to save the information.

    
16.08.2018 / 16:54