Encrypt/Decrypt SQL Server

Berikut adalah funtion untuk encrypt dan decrypt di SQL Server…

create Function [dbo].[encrypt] (@Password varchar(100) ,@Key varchar(100))
RETURNS varbinary(max)WITH EXECUTE AS CALLER
BEGIN
return EncryptByPassPhrase(@key, convert(varchar(100),@password))
END

Create Function [dbo].[decrypt] (@Key varchar(100),@EncryptPwd varbinary(max))
RETURNS varchar(100)WITH EXECUTE AS CALLER
BEGIN
return convert(varchar(100),convert(varchar(100),decryptbypassphrase(@Key,@encryptPwd)))
END

declare @password as varchar(100)
declare @key as varchar(100)
set @password=’Passw0rd’
set @key=’Suprie’
declare @hsl1 as varbinary(max)
declare @hsl2 as varbinary(max)
declare @decr as varchar(100)
declare @decr1 as varchar(100)
print ‘Eksekusi pertama ‘
SELECT @hsl1= [dbo].[encrypt] (@password,@Key)
print @hsl1
select @decr=dbo.decrypt(@key,@hsl1)
print @decr
print ”
print ‘eksekusi kedua’
SELECT @hsl2= [dbo].[encrypt] (@password,@Key)
print @hsl2
select @decr1=dbo.decrypt(@key,@hsl2)
print @decr1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: