Jumat, 16 April 2010

Enkripsi di SQL Server

Dibandingan dengan MySQL enkrip-dekrip di SQL Server jauh lebih rumit (itu sih pendapat gwe loh). Cukup banyak variasi enkrip-dekrip di MySQL seperti :
  • AES_ENCRYPT(str,key_str), AES_DECRYPT(crypt_str,key_str) 
  • ENCODE(str,pass_str), DECODE(crypt_str,pass_str)
  • DES_DECRYPT(crypt_str[,key_str]), DES_ENCRYPT(str[,{key_num|key_str}]) 
  • ENCRYPT(str[,salt]) 
  • MD5(str) 
  • SHA1(str), SHA(str) 
Setelah browsing beberapa lama akhirnya gwe temuin juga function enkrip-dekrip (pake RC4) di SQL Server. Gwe lupa dapet darimana. Kalian bisa copas function berikut:

--------------------------
CREATE FUNCTION dbo.fnInitRc4
(
 @Pwd VARCHAR(256)
)
RETURNS @Box TABLE (i TINYINT, v TINYINT)
with ENCRYPTION
AS

BEGIN
 DECLARE @Key TABLE (i TINYINT, v TINYINT)

 DECLARE @Index SMALLINT,
  @PwdLen TINYINT

 SELECT @Index = 0,
  @PwdLen = LEN(@Pwd)

 WHILE @Index <= 255
  BEGIN
  INSERT @Key(i,v)
  VALUES (@Index,ASCII(SUBSTRING(@Pwd, @Index % @PwdLen + 1, 1)))

  INSERT @Box(i,v)
  VALUES (@Index,@Index)

  SELECT @Index = @Index + 1
  END


 DECLARE @t TINYINT,
  @b SMALLINT

 SELECT @Index = 0,
  @b = 0

 WHILE @Index <= 255
  BEGIN
  SELECT @b = (@b + b.v + k.v) % 256
  FROM @Box AS b
  INNER JOIN @Key AS k ON k.i = b.i
  WHERE b.i = @Index

  SELECT @t = v
  FROM @Box
  WHERE i = @Index

  UPDATE b1
  SET b1.v = (SELECT b2.v FROM @Box b2 WHERE b2.i = @b)
  FROM @Box b1
  WHERE b1.i = @Index

  UPDATE @Box
  SET v = @t
  WHERE i = @b

  SELECT @Index = @Index + 1
  END

 RETURN
END

---------------------

CREATE FUNCTION dbo.fnRC4
(
 @Pwd VARCHAR(256),
 @Text VARCHAR(8000)
)
RETURNS VARCHAR(8000)
with ENCRYPTION
AS

BEGIN
 DECLARE @Box TABLE (i TINYINT, v TINYINT)

 INSERT @Box(i,v)
 SELECT i,v
 FROM dbo.fnInitRc4(@Pwd)

 DECLARE @Index SMALLINT,
  @i SMALLINT,
  @j SMALLINT,
  @t TINYINT,
  @k SMALLINT,
  @CipherBy TINYINT,
  @Cipher VARCHAR(8000)

 SELECT @Index = 1, @i = 0, @j = 0, @Cipher = ''

 WHILE @Index <= DATALENGTH(@Text)
  BEGIN
  SELECT @i = (@i + 1) % 256

  SELECT @j = (@j + b.v) % 256
  FROM @Box b
  WHERE b.i = @i

  SELECT @t = v
  FROM @Box
  WHERE i = @i

  UPDATE b
  SET b.v = (SELECT w.v FROM @Box w WHERE w.i = @j)
  FROM @Box b
  WHERE b.i = @i

  UPDATE @Box
  SET v = @t
  WHERE i = @j

  SELECT @k = v
  FROM @Box
  WHERE i = @i

  SELECT @k = (@k + v) % 256
  FROM @Box
  WHERE i = @j

  SELECT @k = v
  FROM @Box
  WHERE i = @k

  SELECT @CipherBy = ASCII(SUBSTRING(@Text, @Index, 1)) ^ @k,
  @Cipher = @Cipher + CHAR(@CipherBy)

  SELECT @Index = @Index +1
  END

 RETURN @Cipher
END

------------------------------
kalau kalian punya metode lain bisa di share dunk .... :)

(koral web)

2 comments:

Ivan Jaya mengatakan...

Kalau enkripsi MD5 dengan Visual Basic (6 atau Net) punya gak source code nya?

Btw, theme nya clean banget.. cool. Buat sendiri atau download bro..?

Koral Web mengatakan...

Kalau keperluannya buat enkrip data di database MySQL atau di SQL Server pake function md5 aja mas bawaan ke dua database di atas. kalo dari VB.NET saya dapetin nih dari google..

Imports System.Text
Imports System.Security.Cryptography

Private Function GenerateHash(ByVal SourceText As String) As String
'Create an encoding object to ensure the encoding standard for the source text
Dim Ue As New UnicodeEncoding()
'Retrieve a byte array based on the source text
Dim ByteSourceText() As Byte = Ue.GetBytes(SourceTStext)
'Instantiate an MD5 Provider object
Dim Md5 As New MD5CryptoServiceProvider()
'Compute the hash value from the source
Dim ByteHash() As Byte = Md5.ComputeHash(ByteSourceText)
'And convert it to String format for return
Return Convert.ToBase64String(ByteHash)
End Function


Kalau themesnya saya pake themes generator mas. Males harus koding ulang.... :D cuma butuh setengah sampe sejaman dah jadi deh nih website :D

Posting Komentar

About Me

Koral Web
Kami adalah web developer. Beberapa produk yang pernah kami buat antara lain website, aplikasi klinik, aplikasi apotik, aplikasi EDMS (Electronic Database Management System), Energy Consumption Management System, RKBI (Rencana Kunjungan Barang Import) dan lain-lain sesuai dengan request dari client kami. Jika Anda tertarik untuk membuat system atau aplikasi, jangan sungkan-sungkan menghubungi kami.
Lihat profil lengkapku

Bahasa Pemrogramanmu?

Nasihat

Barangsiapa capek lelah dan letihnya bukan karena Allah maka celakalah dia
Diberdayakan oleh Blogger.

Blog Archieve

SMS Gratis