3rd highest salary in sql server

Encrypt and Decrypt in SQL Server

Hello everyone, I am going to share the query and functions for Encrypt and Decrypt GUID, Text, String using SQL Server.

Table of Content:-

1.      Query for Encrypt and Decrypt.
2.      Create Function for Encrypt
3.      Create Function for Decrypt
5.      Query Result.

QUERY FOR  ENCRIPT & DECRIPT :-

DECLARE @encrypt_Token VARBINARY(255)
DECLARE @decrypt_Token NVARCHAR(255)

SELECT @encrypt_Token = ENCRYPTBYPASSPHRASE('TOKEN', '997AEBEC-2362-40DA-A506-9F0E965BC4AE')
SELECT @encrypt_Token AS GUID_TO_ENCRYPT

SET @decrypt_Token = (SELECT CONVERT(VARCHAR(100), DECRYPTBYPASSPHRASE('TOKEN', @encrypt_Token)))
SELECT @decrypt_Token AS DECRYPT_TO_GUID


FUNCTION - ENCRIPT TOKEN STRING :-

CREATE FUNCTION FNC_ENCRIPTION_TOKEN(@Token NVARCHAR(255))
RETURNS VARBINARY(8000)
AS
                BEGIN
                                DECLARE @encriptToken VARBINARY(8000)
                                                SET @encriptToken = ENCRYPTBYPASSPHRASE('TOKEN', @Token)
                                RETURN (@encriptToken)
                END
GO


FUNCTION - DECRIPT TOKEN STRING :-

CREATE FUNCTION FNC_DECRIPTION_TOKEN(@encrypToken VARBINARY(8000))
RETURNS NVARCHAR(255)
                AS
                                BEGIN
                                                DECLARE @decriptToken VARBINARY(4000)
                                                                SET @decriptToken = DECRYPTBYPASSPHRASE('TOKEN',@encrypToken)
                                                RETURN (@decriptToken)
                                END
GO


HOW TO USE OF ENCRIPT AND DECRIPT A TOKEN STRING FUNCTIONS?

DECLARE @decriptToken NVARCHAR(4000);
DECLARE @encriptToken VARBINARY(8000);
                SET @encriptToken = [dbo].[FNC_ENCRIPTION_TOKEN]('997AEBE1-236E-40DX-A5A6-9F02965BX4AE')
SELECT @encriptToken

                SET @decriptToken = [dbo].[FNC_DECRIPTION_TOKEN](@encriptToken)
SELECT @decriptToken


The output/result looks like,





ANIL SINGH

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.