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

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.