|
|
8000XXXX Errors ASP.NET 2.0 Classic ASP 1.0 Databases Access DB & ADO General SQL Server & Access Articles MySQL Other Articles Schema Tutorials Sql Server 2000 Sql Server 2005 General Concepts Search Engine Optimization (SEO)Search | ASP FAQ Tutorials :: Databases :: Sql Server 2000 :: Can I implement an input mask in SQL Server? Can I implement an input mask in SQL Server?Access has a formatting feature that allows you to mask data (e.g. using ********) when users open a table in the Access GUI, instead of showing the sensitive data that's really stored in the table (be it a password, credit card number, social security number, etc).
People often forget that Access is both a front-end and a back-end tool. SQL Server is only a back-end tool, designed for data management and NOT data presentation. So, there are several other approaches you will need to choose from. Some of these will assume that you really want to secure and protect the underlying data, not just make it look pretty (e.g. in case users use profanity in their passwords). Store an encrypted version Of course, if you need to later retrieve the data, encryption is only as strong as the place where you store your decryption algorithm. In fact, many will argue that encryption that can be reversed should not be called encryption at all, but rather "encoding" or "obfuscation." In the case of passwords, there really is no reason to store the password in plain text. You can easily store an encrypted version, and when verifying permissions as the user logs in, apply the same encryption to the password they submitted. Then, you compare the encrypted versions and make sure they're equal, instead of comparing the plain text versions. My recommendation is to use available APIs in your application to encrypt the data BEFORE you pass it to the database. This prevents the plain text data from going across the wire. For information about MD5 and other components available for ASP, see Article #2249 and Article #2397. Here is a quick example. First, I created this table and stored procedures:
Next, I grabbed the MD5 code from ~linkid(131, '', 'http://userpages.umbc.edu/~mabzug1/cs/md5/md5.asp')~ and placed it into a file called MD5.inc (thanks, Betty and Wilma!).
Clean-up:
Obviously, your application wouldn't flow exactly like this. You would have a different page for adding users than for checking credentials (so you wouldn't be creating a hash of a password and then verifying that it worked four lines later), and you would likely have a lot more information in the users table than e-mail address and password. But the above is a starting point to demonstrate the methodology. As you are testing the above, you will want to stop between each refresh and do the following:
A benefit of this approach is that you won't be using an undocumented function in SQL Server. PWDENCRYPT() and PWDCOMPARE() are very tempting to use, but they are undocumented for a reason: they are unsupported, and could change or disappear with a new version of SQL Server, or even a service pack or hotfix. So if you build an application around this, it may come crumbling down at the worst possible time. Also, I'm not going to speak for the strength of the algorithm PWDENCRYPT uses, or the ability of a determined user to crack it. Nonetheless, to be thorough, I will include a sample of how I would do this so that password comparison were handled inside the database, were these functions documented and supported (and weren't I so risk averse).
Now the ASP code to run a test (change out e-mail addresses to run multiple times, and don't forget to use a purposely false password to verify the failure "works"):
Clean-up:
You could do something similar using supported code by using CONVERT(VARBINARY), however this will only shield the password from the most casual users. You can convert to and from VARBINARY <-> VARCHAR without difficulty, so anyone with this knowledge would have free range over all the passwords in the table. At least in the above cases they will need to resort to a dictionary attack of some sort, and that can be prevented to some degree by both software and hardware (and can be pinpointed to a workstation if it is coming from within your company <G>). In SQL Server 2005, you will be able to create CLR code, using any encryption algorithm you like, and handling it all inside the database, without using unsupported or undocumented functions. Deny read access to the table You could easily create a view or stored procedure that simulated the input mask functionality that Access provides. For example:
In order to prevent users from viewing the data, you can simply issue a DENY:
Now when this user tries to run the SELECT against dbo.Users listed above, they get the following error:
They get similar errors if they try to UPDATE, INSERT or DELETE. In other words, if they want to see the data in the table, add new rows, or modify existing data, they will need to interface through *your* stored procedures. Of course, if they are an elevated user (e.g. a sysadmin in SQL Server) or can access the database as a domain admin, they can easily thwart your object-level permissions settings. Users you need to be concerned about should only be in the public, db_datareader and db_datawriter roles. The benefit to this approach is that, because it is stored in plain text, the people who *should* have access to the data can easily view and update without having to use convoluted methods like 3rd party encryption or even data type conversion. Only you can determine how secure your data needs to be, relative to how easy you want your maintenance to be. Related Articles Are there tools available for auditing changes to SQL Server data? Can I create an index on a BIT column? Can I have optional parameters to my stored procedures? Can I make SQL Server format dates and times for me? Can I start IDENTITY values at a new seed? Can SQL Server tell me which row was inserted most recently? How can I learn more about undocumented SQL Server stored procedures? How can I make my SQL queries case sensitive? How do I audit changes to SQL Server data? How do I connect to a non-default instance of SQL Server? How do I connect to SQL Server on a port other than 1433? How do I create a cross-tab (or "pivot") query? How do I determine if a table exists in a SQL Server database? How do I drop a SQL Server database? How do I find all the available SQL Servers on my network? How do I get a list of SQL Server tables and their row counts? How do I get rid of Named Pipes / DBNMPNTW errors? How do I get the correct date/time from the msdb.sysjob* tables? How do I get the nth row in a SQL Server table? How do I get the result of dynamic SQL into a variable? How do I handle REPLACE() within an NTEXT column in SQL Server? How do I hide system tables in SQL Server's Enterprise Manager? How do I know which version of SQL Server I'm running? How do I limit the number of rows returned in my resultset? How do I load text or csv file data into SQL Server? How do I manage changes in SQL Server objects? How do I monitor SQL Server performance? How do I prevent linked server errors? How do I reclaim space in SQL Server? How do I recover data from SQL Server's log files? How do I search for special characters (e.g. %) in SQL Server? How do I start SQL Server Agent from ASP? How do I time my T-SQL code? How do I upsize from Access to SQL Server? How do I upsize my MSDE database to full-blown SQL Server 2000? How do I use a variable in a TOP clause in SQL Server? How do I use GETDATE() within a User-Defined Function (UDF)? How should I store an IP address in SQL Server? Schema: How do I find all the foreign keys in a database? SQL Server & MSDE What are reserved Access, ODBC and SQL Server keywords? What are the capacities of Access, SQL Server, and MSDE? What are the main differences between Access and SQL Server? What do I need to know about SQL Server 2000 SP4? Where else can I learn about SQL Server? Where is SP4 for SQL Server 2000? Why am I having problems with SQL Server 2000 SP3 / SP3a? Why can't I install SQL Server on Windows Server 2003? Why can't I install SQL Server on Windows XP? Why can't I use LIKE '%datepart%' queries? Why do I get "Login failed for user '\'."? Why do I get 'object could not be found' or 'invalid object name'? Why do I get errors about master..spt_values? Why do I get script errors in Enterprise Manager's 'taskpad' view? Why do I get SQLSetConnectAttr Failed errors? Why do I have problems with views after altering the base table? Why does EM crash when I get an error in a stored procedure? Why does Enterprise Manager return 'Invalid cursor state'? Why does my DELETE query not work? Why does sp_spaceused return inaccurate values? Why is Enterprise Manager slow at expanding my database list? Why is my app slow after upgrading from SQL Server 7 to 2000? Why is tempdb full, and how can I prevent this from happening? Why should I consider using an auxiliary calendar table? Why should I consider using an auxiliary numbers table? |