python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
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

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions