This article is written to answer following two questions I have received in last one week.
Questions 1) How to hide code of my Stored Procedure that no one can see it? 2) Our DBA has left the job and one of the function which retrieves important information is encrypted, how can we decrypt it and find original code?
Answers 1) Use WITH ENCRYPTION while creating Stored Procedure or User Defined Function. 2) Sorry, unfortunately there is no simple way to decrypt the code. Hard way is too hard to even attempt.
Explanations of WITH ENCRYPTION clause If SP or UDF are created WITH ENCRYPTION it is one way street and it is not possible to decrypt it using SQL Server commands. It is always advised to save a copy of the script used to create the SP or UDF on other media than SQL Server. There is no way to get the original source code once it is executed on Server. Only privileged users who can access system tables over the DAC port or directly access database files as well as can attach a debugger to the server process can retrieve the decrypted procedure from memory at runtime.
CLR SP and UDF can not be encrypted. SQL Server replication can not replicate encrypted SP or UDF.
I believe in fair programming techniques and strongly recommend against encryption. There should be no need to encrypt any code. If there is need to hide any code from certain users in that case user should be restricted using user login permissions.
Reference : Pinal Dave (http://blog.SQLAuthority.com)




Hi,
I am working on SQL 2000 n 2005,Can you plz send me the major differences b/w them. And when will the cold fusion be applicable..Wat kind of scenarios
Answers
1) Use WITH ENCRYPTION while creating Stored Procedure or User Defined Function.
2) Sorry, unfortunately there is no simple way to decrypt the code. Hard way is too hard to even attempt.
————————————————
No2, I have a simple code for this, I’m not sure about Micro$oft Security.
In fact, I’ve spend alot of money for this buG.
Hi EvilSoul,
Please explain what you are suggesting. I am interesting to learn.
Regards,
Pinal Dave ( http://www.SQLAuthority.com )
Well, in my opinion. Instead of encrypt ur stored with FULL and CLEAR code, you need have a “key unlock” column, and a “indexed filter” column. Use “key column” with random algorithms on “indexed filter column” when you do criteria thing. Remember “RANDOM”.
Problem IS: how to genarate Key Col and Indx Col? Depend your decide.
Sorry about my bad English.
In fact, due to a flaw in how SQL Server encrypts an altered procedure, decrypting the original procedure text is easy.
SQL Server uses the RC4 cipher algorithm to encrypt the procedure text. This algorithm generates a stream of key data (called the keystream) from a starting key, then encrypts the plaintext by using the XOR operation. The beauty of the XOR operation is that to reverse it, you simply XOR the ciphertext with the keystream.
Unfortunately, when you use ALTER PROCEDURE, SQL Server encrypts the new text using the same key as it did when encrypting the original text. This means it uses the same keystream. To recover the keystream when you know the ciphertext and the plaintext, you can simply XOR the ciphertext and plaintext together. You can then recover the original plaintext by XORing the recovered keystream with the original ciphertext.
You can find copies of scripts that do this in numerous places, for example at http://education.sqlfarms.com/education/ShowPost.aspx?PostID=783.
I’m not sure how SQL Server decides what the encryption key should be – it may be as simple as deriving a key from the procedure’s objectid.
SQL Server 2005’s documentation describes the result as ‘obfuscated text’ rather than ‘encrypted’.
Hello,
There is a tool which can decrypt the encrypted stored procedure. Is there any way with which we can restrict decryption of stored procedures? In my situation the database would be in the client side and I would like to enrypt the Stored procedure. Please help.
Thanks,
Abhilash
Hi,
Is there anyway of encrypting more than 10 stored procedures at a time?????
Please suggest…….
Thanks in Advance
Hi Vinay,
Yes, Use the code provided above (copyright prevents redistribution so you’ll need to sign up and download the code yourself):
http://education.sqlfarms.com/education/ShowPost.aspx?PostID=783.
and wrap this up in a cursor or loop. You can even call this using xp_cmdshell ‘SQLCMD…’ via a batch file if you wanted to pass in other params (multiple db’s, server\instance, etc).
All the best,
Matt.
Hai
can anyone tell me how to create a stored procedure with an encryption and password to decrypt it back again when ever requrired.
Hai
can anyone tell me how to create a stored procedure with an encryption and password to decrypt it back again when ever requrired.