Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7031939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:52:09+00:00 2026-05-28T00:52:09+00:00

I am trying to write a stored procedure to decrypt some data encrypted by

  • 0

I am trying to write a stored procedure to decrypt some data encrypted by a Symmetric Key that’s encrypted with an Asymmetric Key with a password.

OPEN SYMMETRIC KEY requires a string literal for the password so I had to do a work around with EXEC sp_executesql. Is there a better way to do this?

DECLARE @open nvarchar(200), @close nvarchar(200)
SET @open = 'OPEN SYMMETRIC KEY skey DECRYPTION BY ASYMMETRIC KEY akey WITH PASSWORD = ' + quotename(@password,'''') + ';';
SET @close = 'CLOSE SYMMETRIC KEY skey;';

EXEC sp_executesql @open

SELECT [TransactionID],Convert(varchar(max),DECRYPTBYKEY([EncryptedText])) as DecryptedText FROM [dbo].[TestTable];

EXEC sp_executesql @close

If you execute it with the wrong password it throws up the following errors:

Msg 15466, Level 16, State 1, Line 1
An error occurred during decryption.
Msg 15315, Level 16, State 1, Line 1
The key 'skey' is not open. Please open the key before using it.

Should I just wrap EXEC sp_executesql @open in a TRY ... CATCH and return NULL or is there a (more) elegant way of handling this?

EDIT: What’s the best way to handle someone calling this procedure with an incorrect password?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:52:10+00:00Added an answer on May 28, 2026 at 12:52 am

    EXAMPLE of using keys, check your data and code:

    CREATE ASYMMETRIC KEY akey WITH ALGORITHM = RSA_2048 
    ENCRYPTION BY PASSWORD = 'aaa123'
    GO
    CREATE SYMMETRIC KEY skey WITH ALGORITHM = AES_256 ENCRYPTION BY ASYMMETRIC KEY akey
    GO
    
    DECLARE @t TABLE(plain VARCHAR(100), ciphered VARBINARY(MAX), unciphered VARCHAR(100))
    
    INSERT @t(plain)
    VALUES('11111'), ('22222'), ('33333')
    
    OPEN SYMMETRIC KEY skey DECRYPTION BY ASYMMETRIC KEY akey WITH PASSWORD = 'aaa123'
    
    UPDATE @t SET Ciphered = ENCRYPTBYKEY(KEY_GUID('skey'), plain)
    
    
    UPDATE @t SET unciphered = CAST(DECRYPTBYKEY(ciphered) AS VARCHAR)
    
    SELECT * FROM @t
    
    CLOSE SYMMETRIC KEY skey
    DROP SYMMETRIC KEY skey
    DROP ASYMMETRIC KEY akey
    

    For proper logging of wrong password decryption attempts try the following example, play with variable @password:

    CREATE ASYMMETRIC KEY akey WITH ALGORITHM = RSA_2048 
    ENCRYPTION BY PASSWORD = 'aaa123'
    GO
    CREATE SYMMETRIC KEY skey WITH ALGORITHM = AES_256 ENCRYPTION BY ASYMMETRIC KEY akey
    GO
    
    DECLARE @t TABLE(plain VARCHAR(100), ciphered VARBINARY(MAX), unciphered VARCHAR(100))
    
    INSERT @t(plain)
    VALUES('11111'), ('22222'), ('33333')
    
    OPEN SYMMETRIC KEY skey DECRYPTION BY ASYMMETRIC KEY akey WITH PASSWORD = 'aaa123'
    
    UPDATE @t SET Ciphered = ENCRYPTBYKEY(KEY_GUID('skey'), plain)
    CLOSE SYMMETRIC KEY skey
    
    DECLARE @open nvarchar(200), @close nvarchar(200), @password VARCHAR(20) = 'aaa123x'
    SET @open = 'OPEN SYMMETRIC KEY skey DECRYPTION BY ASYMMETRIC KEY akey WITH PASSWORD = ' + quotename(@password,'''') + ';';
    SET @close = 'CLOSE SYMMETRIC KEY skey;';
    BEGIN TRY 
      EXEC sp_executesql @open
      UPDATE @t SET unciphered = CAST(DECRYPTBYKEY(ciphered) AS VARCHAR)
      SELECT * FROM @t
      EXEC sp_executesql @close
    END TRY BEGIN CATCH 
      SELECT 'Do whatever you want to do here with this caller. Suspicious caller: '+SUSER_SNAME()+', at: '+CAST(GETDATE() AS VARCHAR)
    END CATCH
    
    DROP SYMMETRIC KEY skey
    DROP ASYMMETRIC KEY akey
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a stored procedure to select employees who have birthdays that
I'm trying to write a stored procedure that will read an Excel file into
I'm trying to write a stored procedure in SQL that will : Make a
I am trying to write a stored procedure that will allow me to write
I'm trying to write a stored procedure to copy a subset of data from
I'm trying to write a stored procedure that will have 6 bit value flags
I am trying to write a stored procedure that takes a table name as
I'm currently trying to write a stored procedure that can compute the biweekly periods
I am trying to write a stored procedure that inserts a row into an
I'm trying to write a stored procedure that will create a new FILEGROUP based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.