How to Encrypt and Decrypt a value in MS SQL using AES 256
Loading
How to Encrypt and Decrypt a value in MS SQL using AES 256
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Dec 15, 2022, 4:05 AM EST
Try this
CREATE SYMMETRIC KEY SK03 WITH ALGORITHM = AES_256 ENCRYPTION BY PASSWORD = 'abcdefghijklmnopqrstuvwxyzasdefb'; go CREATE TABLE [dbo].[_EncryptionTest]( [OriginalValue] [varchar](max) NULL, [Encryptedvalue] [varbinary](max) NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] go CREATE PROCEDURE [dbo].[_usp_EncryptionTest] @InputString varchar(MAX), @InputString2 varchar(MAX) AS BEGIN SET NOCOUNT ON; OPEN SYMMETRIC KEY SK03 DECRYPTION BY PASSWORD='abcdefghijklmnopqrstuvwxyzasdefb' INSERT INTO _EncryptionTest (OriginalValue, Encryptedvalue) VALUES ( @InputString ,ENCRYPTBYKEY(Key_GUID('SK03'), CONVERT(VARBINARY(max),@InputString2)) ) CLOSE SYMMETRIC KEY SK03; END go EXEC _usp_EncryptionTest 'Spring is Finally Here! Spring is Finally Here!','Spring is Finally Here! Spring is Finally Here!' go OPEN SYMMETRIC KEY SK03 DECRYPTION BY PASSWORD='abcdefghijklmnopqrstuvwxyzasdefb' SELECT [OriginalValue] , CONVERT(varchar(MAX), DECRYPTBYKEY([EncryptedValue])) FROM _EncryptionTest CLOSE SYMMETRIC KEY SK03; select * from _EncryptionTestref: https://dba.stackexchange.com/questions/167920/sql-aes-256-encryption-and-decryption
Puneet KankarRank #446 · 5.6K reputationPosted Dec 15, 2022, 5:55 AM EST
Hi Piyush
Please check the below links.
https://www.geopits.com/blog/sql-encryption.html
https://dba.stackexchange.com/questions/167920/sql-aes-256-encryption-and-decryption