SQL Server encryption : create key inside stored procedure

kwangsa

I need to create a stored procedure that allows our password custodian to recreate the symmetric key in our DR database, but somehow it always complains about incorrect syntax. Is this allowed in SQL Server 2008 R2 or it is just incorrect syntax ? Thanks

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_BCPRecreateEncryption] 
    @Password varchar(255)
AS
BEGIN
    DROP SYMMETRIC KEY SymmetricKeyName
    DROP CERTIFICATE EncryptCert
    DROP MASTER KEY

    CREATE MASTER KEY ENCRYPTION BY PASSWORD = @Password

    CREATE CERTIFICATE EncryptCert
        WITH SUBJECT = N'EncryptCert', START_DATE = N'08/06/2014 07:16:08', EXPIRY_DATE = N'08/06/2042 07:16:08'
        ACTIVE FOR BEGIN_DIALOG = ON;

    CREATE SYMMETRIC KEY SymmetricKeyName
        WITH KEY_SOURCE = @Password, 
                IDENTITY_VALUE = @Password, 
             ALGORITHM = AES_256
        ENCRYPTION BY CERTIFICATE EncryptCert;
END

I also tried to use executesql but still get the same error

DECLARE @ParmDefinition nvarchar(500);
SET @ParmDefinition = N'@Password string' ;

EXECUTE sp_executesql N'CREATE MASTER KEY ENCRYPTION BY PASSWORD = @DCMPassword', @ParmDefinition , @DCMPassword=@Password

Edited to add error :

Msg 102, Level 15, State 1, Procedure sp_BCPRecreateEncryption, Line 13
Incorrect syntax near '@Password'.

Msg 102, Level 15, State 1, Procedure sp_BCPRecreateEncryption, Line 20
Incorrect syntax near '@Password'.

Solomon Rutzky

The main problem is that you cannot use variables to set passwords; they need to be string literals. If you look at the MSDN page for just CREATE MASTER KEY you will notice that there is no option to pass in a local variable for the password. The documentation for CREATE SYMMETRIC KEY also shows that only string literals are valid for KEY_SOURCE and IDENTITY_VALUE.

A secondary issue with your attempt to fix via Dynamic SQL is that string is not a valid datatype.

If you want to get the value from a stored procedure input parameter, then you will need to concatenate that value into the Dynamic SQL. Be sure to sanitize for single-quotes first ;-) to avoid SQL Injection issues.

SET @Password = REPLACE(@Password, N'''', N'''''');

DECLARE @SQL NVARCHAR(MAX) = N'
    CREATE MASTER KEY ENCRYPTION BY PASSWORD = '''
  + @Password
  + N''';';

EXEC(@SQL);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Encryption of results sent by SQL Server Stored Procedure

From Dev

SQL Server: trying to create view inside a stored procedure

From Dev

Create stored procedure on SQL Server

From Dev

SQL Server : create and call stored procedure

From Dev

SQL Server : create and call stored procedure

From Dev

SQL server create stored procedure syntax error

From Dev

Create and execute stored procedure in SQL Server

From Dev

Create and execute stored procedure with parameter in SQL Server

From Dev

SQL Server - create stored procedure that runs several stored procedures sequentially

From Dev

Create Stored Procedure in SQL

From Dev

SQL server : stored procedure

From Dev

How to create a stored procedure on SQL server 2014 for create table operation?

From Dev

Using XPath expressions inside SQL Server stored procedure

From Dev

Does ; means anything inside SQL Server stored procedure

From Dev

Temporary Tables in SQL Server created and used inside a stored procedure

From Dev

Stored procedure to retrieve all foreign key columns with values in SQL server

From Dev

In SQL Server, should I create synonym for a table or a stored procedure?

From Dev

Create text file from stored procedure SQL Server 2008

From Dev

How to create stored procedure in C#, then *save* it to SQL Server?

From Dev

How to create a stored procedure in SQL Server Management Studio

From Dev

Create stored procedure in SQL Server from a MS Access update query

From Dev

In SQL Server, should I create synonym for a table or a stored procedure?

From Dev

create a stored procedure that compare all the attribut betwen them with sql server

From Dev

How to create a select, and then an update stored procedure in SQL Server 2012

From Dev

How to create dynamic parameters in SQL Server stored procedure

From Dev

SQL Server stored procedure : create increment ID in each occurrence of value

From Dev

How to create a stored procedure in SQL Server Management Studio

From Dev

How to DROP and CREATE a table in SQL Server 2016 Stored Procedure

From Dev

SQL Server Stored Procedure Parameter

Related Related

  1. 1

    Encryption of results sent by SQL Server Stored Procedure

  2. 2

    SQL Server: trying to create view inside a stored procedure

  3. 3

    Create stored procedure on SQL Server

  4. 4

    SQL Server : create and call stored procedure

  5. 5

    SQL Server : create and call stored procedure

  6. 6

    SQL server create stored procedure syntax error

  7. 7

    Create and execute stored procedure in SQL Server

  8. 8

    Create and execute stored procedure with parameter in SQL Server

  9. 9

    SQL Server - create stored procedure that runs several stored procedures sequentially

  10. 10

    Create Stored Procedure in SQL

  11. 11

    SQL server : stored procedure

  12. 12

    How to create a stored procedure on SQL server 2014 for create table operation?

  13. 13

    Using XPath expressions inside SQL Server stored procedure

  14. 14

    Does ; means anything inside SQL Server stored procedure

  15. 15

    Temporary Tables in SQL Server created and used inside a stored procedure

  16. 16

    Stored procedure to retrieve all foreign key columns with values in SQL server

  17. 17

    In SQL Server, should I create synonym for a table or a stored procedure?

  18. 18

    Create text file from stored procedure SQL Server 2008

  19. 19

    How to create stored procedure in C#, then *save* it to SQL Server?

  20. 20

    How to create a stored procedure in SQL Server Management Studio

  21. 21

    Create stored procedure in SQL Server from a MS Access update query

  22. 22

    In SQL Server, should I create synonym for a table or a stored procedure?

  23. 23

    create a stored procedure that compare all the attribut betwen them with sql server

  24. 24

    How to create a select, and then an update stored procedure in SQL Server 2012

  25. 25

    How to create dynamic parameters in SQL Server stored procedure

  26. 26

    SQL Server stored procedure : create increment ID in each occurrence of value

  27. 27

    How to create a stored procedure in SQL Server Management Studio

  28. 28

    How to DROP and CREATE a table in SQL Server 2016 Stored Procedure

  29. 29

    SQL Server Stored Procedure Parameter

HotTag

Archive