Showing posts with label decryption. Show all posts
Showing posts with label decryption. Show all posts

Thursday, March 22, 2012

Encryption and Decryption by Key

I am trying to use the encryptionbykey and decryptionbykey from the database to return a key that is used in a web application as a querystring. I can encrpyt fine. But when the value is passed back to a function as as varchar and convert it to a varbinary it does not decrypt. Below is an example:

CREATE SYMMETRIC KEY DummyKey

WITH ALGORITHM = DESX

ENCRYPTION BY PASSWORD = N'd7mmy';

GO

DECLARE @.UnsubscribeURL varchar(300)

DECLARE @.OriginalUserKey varchar(250)

DECLARE @.EncryptedUserKey varchar(250)

-- Get Unsubscribe URL

SELECT @.UnsubscribeURL = 'http://hrowdn01.paychex.com/secure/hronlineApplication/unsubscribe.aspx?UserKey='

-- Create the user key (Company_Id + '_' + Employee_Id)

SELECT @.OriginalUserKey = CONVERT(varchar, 161) + '_' + CONVERT(varchar, 3381)

-- Open Encryption key

OPEN SYMMETRIC KEY DummyKey

DECRYPTION BY PASSWORD = N'd7mmy'

-- Encrypt the user key

SELECT @.EncryptedUserKey = master.dbo.fn_varbintohexstr(EncryptByKey(Key_GUID('DummyKey'),@.OriginalUserKey))

--EXEC master..xp_blowfishencrypt @.OriginalUserKey, @.key, @.EncryptedUserKey OUTPUT

CLOSE SYMMETRIC KEY DummyKey

-- Finish creating the unsubscribe URL

SET @.UnsubscribeURL = @.UnsubscribeURL + @.EncryptedUserKey

SELECT @.UnsubscribeURL

--New function would be here

DECLARE @.decrypted_str VARBINARY(MAX)

DECLARE @.DecryptedUserKey varchar(MAX)

OPEN SYMMETRIC KEY DummyKey

DECRYPTION BY PASSWORD = N'd7mmy'

SET @.decrypted_str = CONVERT(varbinary(max),'0x002da862c3f37f449936e9a3eabc83340100000007f5728f4a1ff60d6a08a3ee30dd7d8626551f0da25d14719f4e81a00147a2d9')

SET @.DecryptedUserKey = DecryptByKey(@.decrypted_str)

-- display decrypted text

SELECT @.DecryptedUserKey AS PlainText;

-- close and drop the key

CLOSE SYMMETRIC KEY DummyKey

DROP SYMMETRIC KEY DummyKey

Thanks,

J

Jason,

I've been playing around with this a little bit.

I don't have an answer.

But from what i can tell, you're going to need a function that reverses the process of

master.dbo.fn_varbintohexstr().

It appears that just recasting that output to varbinary isn't doing what you need.

sql

Encryption and Decryption

Can anybody let me know how I can do Encryption and Decryption of data in
SQL Server 2000 Database
Thanks
Check the FAQ and the encryption section of this article at
SQLSecurity.com
http://www.sqlsecurity.com/FAQs/SQLServerFAQ/tabid/55/Default.aspx
It goes through the different levels and different options
(most of them are third party) for SQL Server 2000.
SQL Server 2005 has built in encryption mechanisms but SQL
Server 2000 doesn't really have much (doesn't have anything
work using anyway).
-Sue
On Tue, 3 Apr 2007 17:23:14 -0400, "Rogers"
<naissani@.hotmail.com> wrote:

>Can anybody let me know how I can do Encryption and Decryption of data in
>SQL Server 2000 Database
>Thanks
>
|||You can get a free SQL 2000 encryption toolkit w/ source code here:
http://www.sqlservercentral.com/columnists/mcoles/sql2000dbatoolkitpart1.asp
and here: http://www.sqlservercentral.com/products/mcoles/default.asp
"Rogers" <naissani@.hotmail.com> wrote in message
news:ePD6IZjdHHA.4220@.TK2MSFTNGP06.phx.gbl...
> Can anybody let me know how I can do Encryption and Decryption of data in
> SQL Server 2000 Database
> Thanks
>

Encryption and decryption

How to do encryption and decryption?Check out the FAQ section at SQLSecurity.com
--
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Rn" <ryanhls@.yahoo.com> wrote in message
news:eAbHqPdjFHA.1416@.TK2MSFTNGP09.phx.gbl...
How to do encryption and decryption?

Encryption and "WHERE encrypted_column LIKE"

I am starting an encryption project for my database and I'm performing
some tests on decryption speed. A lot of my application queries use a
LIKE parameter in the WHERE clause. To keep from changing my
application I am performing all the work on the back-end; creating
views, triggers and UDFs to encrypt/decrypt the data. A problem has
arisen around the LIKE parameter, though.

Currently:
SELECT SSN, FNAME, LNAME FROM USERS WHERE LNAME LIKE 'BON%'

will become:
SET @.NEWVALUE = dbo.decrypt_hash('BON%')
SELECT SSN, FNAME, LNAME FROM USERS_VIEW WHERE LNAME_HASH LIKE
@.NEWVALUE

This will not work. A hash can only compare a string value to a string
value. Has anyone else worked with this type of encryption and how did
you get around using LIKE?

Thanks,
JoshUsing TSQL to encrypt in a UDF is a non-starter. It's always going to
destroy performance because any non-trivial encryption algorithm is likely
to be unfeasibly slow implemented in TSQL.

Firstly, what is the goal of encrypting the data? Understand that encryption
is not a good way to control access to a database. There are legitimate uses
of encryption in a database but encrypting user's names seems a little
unusual. Since your example code doesn't even seem to include a key for the
decryption function I don't quite understand what you are trying to
implement here.

If you really need encryption then Google for some of the third-party
solutions available. You'll also find previous posts on this topic in the
microsoft.public.sqlserver.* hierarchy.

--
David Portas
SQL Server MVP
--|||David Portas (REMOVE_BEFORE_REPLYING_dportas@.acm.org) writes:
> Using TSQL to encrypt in a UDF is a non-starter. It's always going to
> destroy performance because any non-trivial encryption algorithm is likely
> to be unfeasibly slow implemented in TSQL.

You could call an extended stored procedure from the UDF to perform
the actual encryption. Of course, it will still be slow since the UDF
and XP calls are expensive in themselves. Then again, Encryption
and high performance do not really go well together.

As for the problem posted, I would suggest that what is needed is:

SELECT SSN, FNAME, LNAME FROM USERS_VIEW
WHERE dbo.decrypt(LNAME_HASH) LIKE 'BON%'

Which is not going to perform well at all.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||"joshsackett" <joshsackett@.gmail.com> wrote in message
news:1117212160.020006.275040@.z14g2000cwz.googlegr oups.com...
> I am starting an encryption project for my database and I'm performing
> some tests on decryption speed. A lot of my application queries use a
> LIKE parameter in the WHERE clause. To keep from changing my
> application I am performing all the work on the back-end; creating
> views, triggers and UDFs to encrypt/decrypt the data. A problem has
> arisen around the LIKE parameter, though.

I was just reading an article on this I think in this month's SQL Server
magazine.

I'll agree that encrypting last name is a bit "different".

One thing they suggested for things like credit card numbers is a) being
able to index on a column OTHER than the ccn so you can get the row(s) in
question and only decrypt that absolute minimum needed and if you DO need to
use the ccn, b) store the last 4 digits unencrypted to use that to help
narrow your search.

> Currently:
> SELECT SSN, FNAME, LNAME FROM USERS WHERE LNAME LIKE 'BON%'
> will become:
> SET @.NEWVALUE = dbo.decrypt_hash('BON%')
> SELECT SSN, FNAME, LNAME FROM USERS_VIEW WHERE LNAME_HASH LIKE
> @.NEWVALUE
> This will not work. A hash can only compare a string value to a string
> value. Has anyone else worked with this type of encryption and how did
> you get around using LIKE?
> Thanks,
> Josh|||Josh,

Hashing is not encryption, and hashing something like a last name
is useless, except for obfuscation. Only the 1,000,000 most common
last names in the world (if not 10,000) account for virtually everyone,
so if someone has a hash (say SHA1) of a last name, they basically
have the last name and can look up the hash in a small dictionary of
hashed last names. When hashing is appropriate, such as for creating
a message digest, it is not reversible. The sum of this is that
something based on the idea of "decrypting a hash" is flawed.

That said, the more you want to do efficiently with the encrypted
value, the less useful the encryption. If you can use LIKE or
other comparisons efficiently in predicates with the encrypted value,
you're letting your users play "Twenty Questions" with your data:

1. Does Secret start with the letter 'L' (LIKE 'L%')?
2. It does? Good. Does it satisfy WHERE Secret >= 'LN'?
3. No? Ok, does it satisfy WHERE Secret > 'LG'?
...

If you're just obfuscating the data with a reversible obfuscator,
you might just as well do this when someone needs a LIKE result:

select ...
from users_view
where dbo.deobfuscate(LNAME) like ' BON%'

If that's too slow, maybe you can manage to add dbo.deobfuscate(LNAME)
to the underlying table and index that computed column, hoping the
index will be used by the query. I'm not sure whether you can make
this work, but as Erland said, encryption and performance don't
go well together. Security and availability are Heisenbergian: you
can't have both, and the more of one you have, the less you have of
the other. This is as unavoidable as any law of physics.

Steve Kass
Drew University

joshsackett wrote:

> I am starting an encryption project for my database and I'm performing
> some tests on decryption speed. A lot of my application queries use a
> LIKE parameter in the WHERE clause. To keep from changing my
> application I am performing all the work on the back-end; creating
> views, triggers and UDFs to encrypt/decrypt the data. A problem has
> arisen around the LIKE parameter, though.
> Currently:
> SELECT SSN, FNAME, LNAME FROM USERS WHERE LNAME LIKE 'BON%'
> will become:
> SET @.NEWVALUE = dbo.decrypt_hash('BON%')
> SELECT SSN, FNAME, LNAME FROM USERS_VIEW WHERE LNAME_HASH LIKE
> @.NEWVALUE
> This will not work. A hash can only compare a string value to a string
> value. Has anyone else worked with this type of encryption and how did
> you get around using LIKE?
> Thanks,
> Josh|||First off, thanks to everyone who has provided their input. Secondly,
let me continue down the path I started:

My client performs searches on SSN, FNAME & LNAME. Any of these columns
can currently be included in a "LIKE" search. I am researching
encryption methods for the database that have minimal impact on the
application. The only way to accomplish this is to change the table
names, encrypt the data and create views to access the tables. The
problem (as you know) is that in order to perform a comparison on an
encrypted column is to completely decrypt the column and then compare.
This is not acceptable performance wise.

The next option is to not change the DB but the application. So to have
the application perform a search against an indexed, encrypted column I
would write (in essence)
SELECT dbo.decrypt(ENC_SSN), dbo.decrypt(ENC_FNAME),
dbo.decrypt(ENC_LNAME), ADDRESS FROM UserTable WHERE ENC_LNAME =
dbo.encrypt(SMITH) .

This is MUCH faster. The problem now is how to perform a LIKE search?
dbo.encrypt(SMITH) will look nothing like dbo.encrypt(SMI). The only
thing I can think of is to create another column containing the first 2
(or so) characters of the last name and perform a straight comparison
on that column using a SUBSTRING of the original LastNameString and
then decrypt all the matching columns and perform a like search on
those. Example:

@.LastNameString = 'WILLI%'
@.ShortLNS = SUBSTRING(@.LastNameString,1,2)

SELECT dbo.decrypt(ENC_SSN), dbo.decrypt(ENC_FNAME),
dbo.decrypt(ENC_LNAME), ADDRESS
FROM UserTable
WHERE dbo.decrypt(ENC_LNAME) LIKE @.LastNameString
AND
-- This next section limits the search result but only by
26^ShortColumnLength.
-- So in this case 26^2 = 676 unique rows (max.. assuming someone's
last name starts with "ZZ" :)
dbo.decrypt(ENC_LNAME) IN
(
SELECT dbo.decrypt(ENC_LNAME) from UserTable
WHERE SHORT_LNAME = @.ShortLNS
)|||I got it! This can be performed for every searchable column. Wrap the
entire thing in a stored procedure (expand as needed) and viola!

@.LastNameString = 'WILLI%'
@.ShortLNS = SUBSTRING(@.LastNameString,1,2)

DECLARE @.tbl_enc_lname TABLE
(enc_lname varchar(30))
INSERT INTO @.tbl_enc_lname
SELECT enc_lname FROM users WHERE short_lname = @.ShortLNS

SELECT
dbo.decrypt(enc_ssn),dbo.decrypt(enc_fname),dbo.de crypt(enc_lname),
address from users
WHERE
dbo.decrypt(enc_lname) LIKE @.LastNameString
AND
enc_lname IN
(
SELECT enc_lname from @.enc_lname_holder
)

SQL Statistics:
44 unique last names out of 100,000 rows
2,301 rows returned

SQL Server Execution Times:
CPU time = 5428 ms, elapsed time = 7118 ms.

SQL Server IOSTATS:
Table '#21D600EE'. Scan count 0, logical reads 4494, physical reads 0,
read-ahead reads 0.
Table 'users'. Scan count 2, logical reads 497, physical reads 0,
read-ahead reads 0.
Table 'users'. Scan count 2, logical reads 7234, physical reads 0,
read-ahead reads 0.
Table '#21D600EE'. Scan count 1, logical reads 22, physical reads 0,
read-ahead reads 0.

Hardware:
Single Pentium 4 - 1.7GHz
384MB RAM
Dell Inspiron 8200 Notebook
SQL Server Desktop Edition|||SELECT enc_lname from @.enc_lname_holder
should read
SELECT enc_lname from @.tbl_enc_lname|||> The only way to accomplish this is to change the table
> names, encrypt the data and create views to access the tables.

This makes no sense at all. You would be better off creating SPs to access
the data unencrypted and then denying all permissions on the base tables. As
I said before, encryption is not the way to control access to data.

--
David Portas
SQL Server MVP
--|||I agree.. what good does it do to encrypt the DB data when the method
for decrypting it exists in the database? If anyone gets the database
(which is I assume what you are worried about) they can simply use the
routine that already exists in the DB to get the data. I assume this is
some sort of privacy feature...

I will say though, that we had a product where people kept asking us if
the usernames and passwords were encrypted in the database. We got sick
of repeatedly explaining why not, so we did a simple encryption on them
so that we could say, "yes, they are" and move on to the next topic.|||David: If someone steals my physical database files or the backups then
they have access to the data, so it must be encrypted. I am not merely
trying to keep people out, I am trying ot make sure that if someone
gets the data they cannot read it.

pb: The routine to decrypt the data exists in the database but you must
run a stored procedure with the routine alias and password before your
run a query if you wish to pull unencrypted data. Check out the program
XP_CRYPT (search Google) and you'll see where I am going with this.

Encryption & Decryption

Database Security, we are going to use AES 256 Symmetric Encryption. We will be using RSA for Asymmetric Key Encryption, 1024 Bits.

We got the code working for the seond case but for the first, WHEN:

CREATE SYMMETRIC KEY sym_Key WITH ALGORITHM =
AES_256 ENCRYPTION BY ASYMMETRIC KEY asym_Key
GO

THEN:

-- Msg 15314, Level 16, State 1, Line 1
-- Either no algorithm has been specified or the bitlength and the algorithm specified for the key are not available in this installation of Windows.

What can be the way out to be able to create the AES 256 Symmetric key.

AES is currently supported by SQL Server 2005 only on Windows 2003. If you are using Windows 2000 or Windows XP, you do not have access to AES encryption from SQL Server.

Thanks
Laurentiu

sql

Encryption

I have tried to encrypt by certificate and by symmetric key. In all cases the decryption comes back as null. Any ideas why?

I have used the code from a learnin tree course and the encryption works OK. I have also added a grant to the certificate to the login

Please post the code you used. Without it we would have to guess every possible problem.

Friday, March 9, 2012

Encrypt Data Issue

Does SQL Server 2000 provide any data encryption/decryption functionality so that certain fields (e.g. SSN, Age and Salary) will be encrypted before writing into the table and decrypted once loading out of the table?
J827Look up encryption in Books Online

Using Encryption Methods
Encryption is a method for keeping sensitive information confidential by changing data into an unreadable form. Encryption ensures that data remains secure by keeping the information hidden from everyone, even if the encrypted data is viewed directly. Decryption is the process of changing encrypted data back into its original form so it can be viewed by authorized users.

Microsoft SQL Server encrypts or can encrypt:

Login and application role passwords stored in SQL Server.

Any data sent between the client and the server as network packets.

Stored procedure definitions.

User-defined function definitions.

View definitions.

Trigger definitions.

Default definitions.

Rule definitions.