473,887 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trigger to encrypt field before write

While working through my encryption questions from preivous posts, I am
finding that I may have to resort to use triggers to do the encryption
(not that this is the only way, but might be the best way for my
I would like to create a trigger that will encrypt the field before the
write is committed.
I've found serveral posts about triggers, but nothing that fits what I
wish to do.
Upon an insert or update I want to modify the ssn field with this:

cast(EncryptByA symKey(AsymKey_ ID('Student_aKe y'), cast(SSN as
nvarchar(11))) as nvarchar(40))

so, ssn '123456789' in SSN would become <something encrypted> in SSN

This is the trigger I have so far, but it is generating an error:

AS SELECT cast(EncryptByA symKey(AsymKey_ ID('Student_aKe y'), cast(SSN as
nvarchar(11))) as nvarchar(40))


Apr 28 '06 #1
3 7728
more research I have:
ON ds_v6_staging.F _STUDENT_SSN
update, insert
update f_student_ssn
set essn = cast(EncryptByA symKey(AsymKey_ ID('Student_aKe y'),
cast(eSSN as nvarchar(11))) as nvarchar(40))

But still get error

Apr 28 '06 #2
rcamarda (ro*****@hotmai l.com) writes:
more research I have:
ON ds_v6_staging.F _STUDENT_SSN
update, insert
update f_student_ssn
set essn = cast(EncryptByA symKey(AsymKey_ ID('Student_aKe y'),
cast(eSSN as nvarchar(11))) as nvarchar(40))

But still get error

I realise that you are working with sensitive data, and may want to
disclose much. However, with knowing what error you get, it's difficult
to assist.

Of course the trigger as such is not a good one, since you are updating
the entire table, you will encrypt already encrypted data on each

You need to add:

update f_student_ssn
set essn = cast(EncryptByA symKey(AsymKey_ ID('Student_aKe y'),
cast(eSSN as nvarchar(11))) as nvarchar(40))
from f_student_ssn f
join inserted i ON f.pkcol = i.pkcol

The table "inserted" holds an after-image of the inserted/updated rows.
Note the plural: a trigger fires one per statement.

But the above only makes sense for an INSERT trigger. For an UPDATE
it's tricker. You could add:


But if someone for some reason says:


you will end up encrypting the encrypted value.
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
Books Online for SQL Server 2000 at
Apr 28 '06 #3
OH...thanks for the Help Erland!
You bring up some very valid points. I may luck out because my plan was
to truncate the table after I am done. Once the data is encrypted, I
can then processed it 'normally', so I wont have to worry about
encypting encrypted data. I am tying to minimize the exposure of clear
text data that I wish to encrypt.
I tried to load the text and encypted using Integration Services, but
could spit that atom yet. (I got data to load, but was stumped when I
needed to add the encrypted functions)

Apr 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: wqhdebian | last post by:
As far as I know,when encrypt or decrypt ,a key must first be got,and the key is first generate by a tool or from SecurityRandom,that means I can not generate the same key with the same input.Does there is a method which can generate a same with the same input string? There is a need to transfer file between to site,and the customer wish to encrypt these files during transfering,and they want to store a string into each database at...
by: Alan Murrell | last post by:
Hello, I am setting up a Postfix + MySQL + Courier-IMAP system. I am trying to write a shell script which will insert the values into the database. For the password encryption, I wish to use MySQL's encrypt() function. Thisis what I have in my script: echo "INSERT INTO users(id,address,crypt,clear,name,domain,maildir)" > /tmp/insert.sql echo " ...
by: Trevor Fairchild | last post by:
I am trying to create a very minimal auditing system for a series of databases. I am in the process of writing Update triggers for 5 Tablse. I will write a trigger for each table-the trigger's function will be to INSERT a row into my MasterChanges table everytime ANY data is changed in each of the 5 tables. I have set up MasterChanges to capture the following: what Table the change was made in
by: Lauren Quantrell | last post by:
Is there a way to create a text file (such as a Windows Notepad file) by using a trigger on a table? What I want to do is to send a row of information to a table where the table: tblFileData has only one column: txtOutput I want to use the DB front end (MS Access) to send the text string to the SQL backend, then have the SQL Server create a file to a path, such as F:/myfiledate.txt that holds the text in txtOutput, then the trigger...
by: Javier Gomez | last post by:
I have a table with 15.000 records. How can encrypt all information if after will shown in a form (text box)decryted ????? Thanks in advance Javier Gomez
by: Chris Gamache | last post by:
Tsearch2 comes with its own tsearch2 trigger function. You pass column names to it, and it puts a vanilla tsvector into the column named in TG_ARGV. Not only can you pass column names to it, but you can pass simple functions to it as well. This is magical to me. :) I'm trying to figure out how to do the same thing using plpgsql, except instead of returning a vanilla tsvector, I want to return a specially weighted tsvector. I've created a...
by: Justin Clift | last post by:
Hi all, I'm creating a centralised table to keep a log of changes in other tables. In thinking about the PL/pgSQL trigger to write and attach to the monitored tables (probably a row level AFTER trigger), I can see two approaches: a) Write a separate PL/pgSQL function for each table, with the hard coded field names in the function.
by: Ots | last post by:
I'm using SQL 2000, which is integrated with a VB.NET 2003 app. I have an Audit trigger that logs changes to tables. I want to apply this trigger to many different tables. It's the same trigger, with the exception of the table name. I could manually change the table name in the trigger and create it, over and over, but I'd like to automate this - by iterating through the collection of tables and passing the tablename to something that...
by: Oliver | last post by:
I'm fairly new to DB2. I have been assigned to build a delete trigger that finds the data type of each of the table's fields so that the trigger can then build a string consisting of OLD values pre-wrapped in quote marks as needed. The deleted record's field values, all strung together as a single string, would then be inserted into a single archiving table (an architecture I inherited and cannot change). I've got the trigger doing...
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.