473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Encrypting/Decrypting data in SQL Server

Seth Schrock
2,965 Expert 2GB
I'm trying to create an encryption/decryption system in SQL server that makes it so that my MS Access projects don't have to know that it is encrypted. I don't have the Enterprise edition of SQL Server, so I can't use TDE. My production server is running SQL Server 2005, but my test system is running SQL Server 2012 and we will (hopefully) soon be upgrading our production server to 2012 Standard edition in about a year. The encryption that I'm needing is only the data at rest, not the data going over the network.

Anyway, here is what I'm attempting to do. I want a View that shows the decrypted information from the table that only stores the encrypted data. The View would then have Update and Insert triggers that would encrypt the necessary fields as they are changed/added.

What I have so far... I have created a testing database that I plan on filling with junk data to test my design. So I have the table Encrypted:
Expand|Select|Wrap|Line Numbers
  1. ID, int PK
  2. OpenString, varchar(50)
  3. OpenNumber, smallint
  4. eCustFN, varbinary(max)
  5. eCustLN, varbinary(max)
  6.  
The only fields that I'm going encrypt is the eCustFN and eCustLN. I also have two UDFs. One to encrypt and one to decrypt the data.
Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION [dbo].[DecryptMax](@value varbinary(max))
  2. RETURNS varchar(max)
  3. AS
  4. BEGIN
  5.     DECLARE @PlainText varchar(max);
  6.  
  7.     SELECT @PlainText = CONVERT(VARCHAR(MAX), DECRYPTBYKEY(@value));
  8.     RETURN @PlainText;
  9. END
  10.  
  11.  
  12. CREATE FUNCTION [dbo].[EncryptMax](@value varchar(max))
  13. RETURNS varbinary(max)
  14. AS
  15. BEGIN
  16.     DECLARE @EncryptedText varbinary(max);
  17.  
  18.     SELECT @EncryptedText = ENCRYPTBYKEY(Key_GUID('PasswordFieldSymmetricKey'), @Value);
  19.     RETURN @EncryptedText;
  20. END
  21. GO
I then have a View that pulls the ID, OpenString and OpenNumber fields as well as using the the DecryptMax function on the eCustFN and eCustLN fields to display the data decrypted. This view has two triggers connected to it on the Insert and Update events that use the EncryptMax UDF to encrypt the data back into the table.

My problem is that I need to be able to open symmetric key for the encryption process to work.
Expand|Select|Wrap|Line Numbers
  1. OPEN SYMMETRIC KEY PasswordFieldSymmetricKey
  2. DECRYPTION BY CERTIFICATE PasswordFieldCertificate;
I can't put this in my UDFs because they aren't allowed. I thought that I could trick it by creating a stored proc that would open it up, but that didn't work either. I read something online about using a stored proc that returned a value instead of the UDFs, but you can't run a store proc inside a query like you can with a function, so I'm not sure how to do that either. I don't have a lot of experience doing this type of stuff in SQL Server so I'm not sure what to even search for online.
Dec 3 '13 #1
0 1145

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: M Wells | last post by:
Hi All, I'm trying to implement encryption on certain data fields in my MySQL database and I'm experiencing ongoing problems. I seem to be able to encrypt the data without issues, but can't...
1
by: Bruno | last post by:
Hi, Does anybody have any experience with Oracle Data Server (the Progress product)? I would love to exchange experiences and findings :-) Thanks, Bruno
0
by: Steve Cramer | last post by:
We have a sql server database that serves a departmental client server application. The database contains confidential information and I would like to encrypt the stored data. Has anyone done...
0
by: cmrchs | last post by:
Hi, Using the RSA-crypto algorithm in .NET , when actually encrypting/decrypting, all the code samples I ran into always used something like, to encrypt : // create keypair and store in...
3
by: VB Programmer | last post by:
I have my SQL Server connectionstring in my web.config file. I'm scared that someone will open the file and get my username/password. How do I encrypt, then decrypt the connection string in the...
0
by: cmrchs | last post by:
Hi, Using the RSA-crypto algorithm in .NET , when actually encrypting/decrypting, all the code samples I ran into always used something like, to encrypt : ' create keypair and store in...
0
by: smart.bug | last post by:
hi, i have SQLserver properly running on my machine, when i open a project in VB.Net and add a SQLConnection object, while setting its connection string (when i click on combo box to choose data...
4
by: laredotornado | last post by:
Hi, I am using PHP 4.4.3 and trying to encrypt and decrypt data. Below are my functions. The problem is that when I run this code, different results are printed out ... ...
3
by: dfa_geko | last post by:
Hi All, I had a question about encrypting and decrypting XML files using asymmetric keys. I copied some sample code from MSDN, here are the samples: ...
1
by: diSangro | last post by:
I need to transfer data from one machine to another , both have HP UX B.10.20 with same usr settings. Data to be transferred are used by Unify data server r.6.1 , installed on both these...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.