EncryptByPassphrase Encryption Algorithm

/*

Copyright 2006 Danal Technology Inc

*/

/*

SQL Server has a really straightforward way to encrypt data by using EncryptDataByPassphrase. What's handy about it is that it doesn't require any keys to be added to SQL Server as the other encryption mechanisms do, you just use DecryptByPassphrase to get the original data back.

*/

DECLARE @cyphertext VARBINARY(MAX)

SET @cyphertext = EncryptByPassphrase('some handy passphrase', 'encrypt me!')

SELECT CAST(DecryptByPassphrase('some handy passphrase', @cyphertext) AS VARCHAR(MAX))

/*

This produces:

encrypt me!

As expected

One of the questions that seems to come up quite often is "What the algorithm is being used?" Afterall if all it was doing was XORing the passphrase with the clear text that is input to it, it would not be much more useful than those "Secret Decoder" rings that sometimes are included as prizes in cerial boxes.

The algorithm isn't documented in the Books Online, but a number of people have speculated that triple DES is used. That kinda' makes sense because that's what SQL Server uses to encrypt database master passwords and such.

This blog enry shows how to confirm that EncyptByPassphrase, EBP for short, uses triple DES and also passes along a little bit of other info about symmetric encryption in SQL Server.

One strategy that might be used to confirm that EBP uses triple DES is to encrypt some data using a triple DES directly and then encrypt the same data with EBP and compare the results. This technique will not work, but let's try it anyhow and see why that is. In the end part of implementing this strategy will be useful for the one that does work.

Making a triple DES symmetric key in SQL Server is pretty easy:

*/

CREATE SYMMETRIC KEY MyKey
WITH ALGORITHM = TRIPLE_DES
ENCRYPTION BY PASSWORD = '58!^8Cg3G7)!1)4='

/*

This creates, in effect, a random symmetric key. We want some more control. Another way to create a symmetric key is to have the key be based on a passphrase... hmmmm maybe that's what EBP does?

*/

CREATE SYMMETRIC KEY MyKeyEBP WITH
ALGORITHM = TRIPLE_DES,
KEY_SOURCE='some handy passphrase'
ENCRYPTION BY PASSWORD = '58!^8Cg3G7)!1)4='

/*

Once we have a key we can encrypt some data.

*/

OPEN SYMMETRIC KEY MyKeyEBP DECRYPTION BY PASSWORD='58!^8Cg3G7)!1)4='
SELECT EncryptByKey(Key_GUID('MyKeyEBP'), 'encrypt me!')

/*

This produces some cyphertext:

0x006516ECAA24984EBC491AF8BB1266F30100000069B2C375271736F6F762E1721AB97AAEC297BE868E89865856DFDF60134A7E02

/*

A symmetric key must be opened before it is used. It has to be identified by it GUID, not its name, that's what the Key_GUID function is for.

Lets encrypt the same text using EPB with the passphrase the same as the KEY_SOURCE for our symmetric key.

*/

SELECT EncryptByPassphrase('some handy passphrase', 'encrypt me!')

 

/*

This produces:

0x01000000CD8F1DEDAFD40521968C9DC614463AB62D79B994A67C85F251E28D896EC51108

As you can see, this is not even close. This doesn't prove one way or the other that EPB uses triple DES, but what's going on here?

There are a couple of things working against us. To see one of them, lets redo the MyKeyEBP encryption again.

*/

SELECT EncryptByKey(Key_GUID('MyKeyEBP'), 'encrypt me!')

0x006516ECAA24984EBC491AF8BB1266F30100000013868CB3274E23A53E011ACADE6D5167520EA94616CA5ECA96DA903B59DB5446

/*

If you look closely you will see that even though we are encrypting the same text with the same key it is producing different results. That is because triple DES, as used by SQL Server, is non-deterministic. It does not produce the same results even if the same text is encrypted again with the same key.

Without going into a lot of detail it is due to the fact that triple DES is a block cypher and SQL Server uses a different random first block everytime it runs it, and includes the block it uses in the results.

This is actually a very good thing, otherwise the bad guys could recognize when the same text was being encypted.

So there is no reason to believe EBP would produce the same output as some other triple DES encrytion would.

But compare the EPB output and the triple DES output again. They are not even the same length. The reason for this will become apparent by looking at the DecryptByKey function.

*/

SELECT CAST( DecryptByKey(0x006516ECAA24984EBC491AF8BB1266F30100000013868CB3274E23A53E011ACADE6D5167520EA94616CA5ECA96DA903B59DB5446) AS VARCHAR(MAX))

/*

This produces:

encrypt me!

which is what we would expect.

Notice that all we gave DecryptByKey was the cypher text, we did not tell it which key to use. How did it pick the right one? The secret is that EncryptByKey always adds puts the guid for the key at the beginning of the cypher text it produces.

*/

SELECT CAST(Key_GUID('MyKeyEBP') AS VARBINARY(16))

/*

Produces:

0x006516ECAA24984EBC491AF8BB1266F3

which is the beginning of

0x006516ECAA24984EBC491AF8BB1266F30100000013868CB3274E23A53E011ACADE6D5167520EA94616CA5ECA96DA903B59DB5446

That means that if we put the GUID for the MyKeyEBP in front of the cypher text produced by EBP we should be able to decrypt it using DecryptByKey. The batch script below shows this. Make sure the MyKeyEBP is open when you run this script.

*/

-- make a variable to hold the cypher text
DECLARE @cyphertext VARBINARY(MAX)
-- use EBP to encrypt some text
SET @cyphertext = EncryptByPassphrase('some handy passphrase', 'encrypt me!')
-- select it just so we can see it
SELECT @cyphertext
-- next stick the guid for MyKeyEBP in front of the cypher text produced by EBP
SET @cyphertext = CAST(Key_GUID('MyKeyEBP') AS VARBINARY(16)) + @cyphertext
-- select it again just so we can see it
SELECT @cyphertext
-- now use DecryptByKey with the fixed up cyphertext
SELECT CAST(DecryptByKey(@cyphertext) AS VARCHAR(MAX))

/*

The script produces:

0x01000000A54E180B2BF9545268BE3E7CD59235E48053F4174FF1848C69A61ACA37159337

0x006516ECAA24984EBC491AF8BB1266F301000000A54E180B2BF9545268BE3E7CD59235E48053F4174FF1848C69A61ACA37159337

encrypt me!

*/

/*

So we can conclude that EBP uses triple DES becuase we can run the cyphertext it produce through a triple DES decypter and it produces the same text that EBP encrypted.

At this point some caveats are in order.

When you use a passphrase to encrypt data, or use one to produce a symmetric key, you are responsible for maintaining the privacy of the passphrase. This typically is not that easy to do, especially if the passphrase has to be given to others. Most of the other encryption features in SQL Server are there so that you will can use SQL Server to keep your keys private.

The passphrase you choose determines how strong the key is that encrypts your data, not so much the algorithm used. BTW 'some handy passphrase' is a terrible choice for a passphrase. It has to be much longer and preferably be just a random sequence of characters.

In any case if you abide by the caveats for EncryptByPassphrase is a very handy tool to have. It is very easy to use, and not even the sysadmin or database owner will be able to decrypt your data.

 

A Developers Guide To SQL Server 2005

*/


Posted Apr 09 2006, 01:13 PM by dan-sullivan
Filed under:

Comments

Jason Haley wrote Interesting Finds
on 04-10-2006 4:24 AM
Scott Whigham (LearnSqlServer.com) wrote re: EncryptByPassphrase Encryption Algorithm
on 08-02-2006 12:25 PM
Thanks for the interesting article and breakdown :)

I would agree with you that this seems to be done with TRIPLE_DES. One reason I think so is that the CREATE MASTER KEY statement uses TRIPLE_DES in addition to the passwords that you mentioned. Makes sense to me...
Knoahsoft Announces Pci Compliance For Today’s Ip Contact Centers | voip equipment blog wrote Knoahsoft Announces Pci Compliance For Today’s Ip Contact Centers | voip equipment blog
on 05-18-2010 12:17 PM

Pingback from  Knoahsoft Announces Pci Compliance For Today’s Ip Contact Centers | voip equipment blog

Second Hand 300 Lexus Gs430, Lexus Gs430 Used Search Pre Owned wrote Second Hand 300 Lexus Gs430, Lexus Gs430 Used Search Pre Owned
on 05-21-2010 3:02 PM

Pingback from  Second Hand 300 Lexus Gs430, Lexus Gs430 Used Search Pre Owned

Parts 1989 Mercury Topaz Bumper Cover Headlight Assembly, Smokey Topaz Quartz Countertop wrote Parts 1989 Mercury Topaz Bumper Cover Headlight Assembly, Smokey Topaz Quartz Countertop
on 05-22-2010 3:43 AM

Pingback from  Parts 1989 Mercury Topaz Bumper Cover Headlight Assembly, Smokey Topaz Quartz Countertop

Sale Mazda Millenia S, Millenia Code Egr Valve wrote Sale Mazda Millenia S, Millenia Code Egr Valve
on 05-22-2010 5:17 AM

Pingback from  Sale Mazda Millenia S, Millenia Code Egr Valve

Buy 1998 Volvo S90 Replacement, Onkyo Ht S907 Home Theater System wrote Buy 1998 Volvo S90 Replacement, Onkyo Ht S907 Home Theater System
on 05-22-2010 8:43 PM

Pingback from  Buy 1998 Volvo S90 Replacement, Onkyo Ht S907 Home Theater System

2002 Mercedes Benz S500 Manual, Toshiba Portege R500 S5007v Review - 183.ja3ra.com wrote 2002 Mercedes Benz S500 Manual, Toshiba Portege R500 S5007v Review - 183.ja3ra.com
on 05-23-2010 6:06 AM

Pingback from  2002 Mercedes Benz S500 Manual, Toshiba Portege R500 S5007v Review - 183.ja3ra.com

S2000 Duraflex Body Kits 2008 Lexus Sc430, Oem Sc400 Lexus Sc430 - 291.luna-atra.net wrote S2000 Duraflex Body Kits 2008 Lexus Sc430, Oem Sc400 Lexus Sc430 - 291.luna-atra.net
on 05-23-2010 7:38 AM

Pingback from  S2000 Duraflex Body Kits 2008 Lexus Sc430, Oem Sc400 Lexus Sc430 - 291.luna-atra.net

Lexus Lx450 Heater Aftermarket, Lx450 Episode Lexus Ls400 - 143.mfbattle.com wrote Lexus Lx450 Heater Aftermarket, Lx450 Episode Lexus Ls400 - 143.mfbattle.com
on 05-24-2010 10:28 AM

Pingback from  Lexus Lx450 Heater Aftermarket, Lx450 Episode Lexus Ls400 - 143.mfbattle.com

Sage 2005 Acura Mdx Touring, Radiator 2007 Acura Tsx Brake Caliper - 302.1fh.org wrote Sage 2005 Acura Mdx Touring, Radiator 2007 Acura Tsx Brake Caliper - 302.1fh.org
on 05-24-2010 10:36 AM

Pingback from  Sage 2005 Acura Mdx Touring, Radiator 2007 Acura Tsx Brake Caliper - 302.1fh.org

Pulsar Nx 04 Nissan Sentra 240sx S14, 620 Pickup Discounts Nissan 240sx - 90.luna-atra.net wrote Pulsar Nx 04 Nissan Sentra 240sx S14, 620 Pickup Discounts Nissan 240sx - 90.luna-atra.net
on 05-24-2010 11:42 AM

Pingback from  Pulsar Nx 04 Nissan Sentra 240sx S14, 620 Pickup Discounts Nissan 240sx - 90.luna-atra.net

Cj5a Used Review, Car Lights Jeep Cj5a - 484.an74.com wrote Cj5a Used Review, Car Lights Jeep Cj5a - 484.an74.com
on 05-24-2010 2:18 PM

Pingback from  Cj5a Used Review, Car Lights Jeep Cj5a - 484.an74.com

Scrambler Rent Thruxton, Scrambler 78 - 41.mfbattle.com wrote Scrambler Rent Thruxton, Scrambler 78 - 41.mfbattle.com
on 05-24-2010 3:19 PM

Pingback from  Scrambler Rent Thruxton, Scrambler 78 - 41.mfbattle.com

Dodge Pb200 Bulb Fog Light, Pb200 Support - 100.tvshowzone.com wrote Dodge Pb200 Bulb Fog Light, Pb200 Support - 100.tvshowzone.com
on 05-24-2010 5:06 PM

Pingback from  Dodge Pb200 Bulb Fog Light, Pb200 Support - 100.tvshowzone.com

Lincoln Aviator Manual Free, Used Navigator In Delaware Lincoln Aviator - 332.dlmreza.net wrote Lincoln Aviator Manual Free, Used Navigator In Delaware Lincoln Aviator - 332.dlmreza.net
on 05-24-2010 7:19 PM

Pingback from  Lincoln Aviator Manual Free, Used Navigator In Delaware Lincoln Aviator - 332.dlmreza.net

Chevrolet Trailblazer Kofferraum 2002 Audi, 2006 Chevrolet Trailblazer Ss Fuel Economy - 380.eumreborn.com wrote Chevrolet Trailblazer Kofferraum 2002 Audi, 2006 Chevrolet Trailblazer Ss Fuel Economy - 380.eumreborn.com
on 05-24-2010 7:30 PM

Pingback from  Chevrolet Trailblazer Kofferraum 2002 Audi, 2006 Chevrolet Trailblazer Ss Fuel Economy - 380.eumreborn.com

1953 Ford F100 Frame, 1981 Ford F100 Stepside - 46.jeepsunlimted.com wrote 1953 Ford F100 Frame, 1981 Ford F100 Stepside - 46.jeepsunlimted.com
on 05-24-2010 10:45 PM

Pingback from  1953 Ford F100 Frame, 1981 Ford F100 Stepside - 46.jeepsunlimted.com

2001 Corvette Sale, Factory Five Corvette - 86.ja3ra.com wrote 2001 Corvette Sale, Factory Five Corvette - 86.ja3ra.com
on 05-24-2010 11:32 PM

Pingback from  2001 Corvette Sale, Factory Five Corvette - 86.ja3ra.com

Aluminum Radiator Phoenix Az, Phoenix Honda Store Glasgow - 211.an74.com wrote Aluminum Radiator Phoenix Az, Phoenix Honda Store Glasgow - 211.an74.com
on 05-25-2010 12:20 AM

Pingback from  Aluminum Radiator Phoenix Az, Phoenix Honda Store Glasgow - 211.an74.com

P 51 Mustang Wingspan, Used Mustang Sale Bullitt Rims - 495.eumreborn.com wrote P 51 Mustang Wingspan, Used Mustang Sale Bullitt Rims - 495.eumreborn.com
on 05-25-2010 4:09 AM

Pingback from  P 51 Mustang Wingspan, Used Mustang Sale Bullitt Rims - 495.eumreborn.com

1961 Oldsmobile Super 88 Parts, Sony Ericsson K550i Cnet - 304.zapstreaming.com wrote 1961 Oldsmobile Super 88 Parts, Sony Ericsson K550i Cnet - 304.zapstreaming.com
on 05-25-2010 4:34 AM

Pingback from  1961 Oldsmobile Super 88 Parts, Sony Ericsson K550i Cnet - 304.zapstreaming.com

Chrylser Lamp Headlight, Chrylser Alternative Green - 94.renters.ws wrote Chrylser Lamp Headlight, Chrylser Alternative Green - 94.renters.ws
on 05-25-2010 11:26 AM

Pingback from  Chrylser Lamp Headlight, Chrylser Alternative Green - 94.renters.ws

Toyota Echo Headlight Halogen Bulbs, Echo Portugal - 106.tvshowzone.com wrote Toyota Echo Headlight Halogen Bulbs, Echo Portugal - 106.tvshowzone.com
on 05-25-2010 1:48 PM

Pingback from  Toyota Echo Headlight Halogen Bulbs, Echo Portugal - 106.tvshowzone.com

Colony Park Discount Won, Led Plymouth Colony - 380.tgrconversions.com wrote Colony Park Discount Won, Led Plymouth Colony - 380.tgrconversions.com
on 05-25-2010 5:50 PM

Pingback from  Colony Park Discount Won, Led Plymouth Colony - 380.tgrconversions.com

Grand Marquis Body Kits, Marquis Sable Used Parts Mercury Ln7 - 226.computeronlinebingo.com wrote Grand Marquis Body Kits, Marquis Sable Used Parts Mercury Ln7 - 226.computeronlinebingo.com
on 05-25-2010 7:18 PM

Pingback from  Grand Marquis Body Kits, Marquis Sable Used Parts Mercury Ln7 - 226.computeronlinebingo.com

1995 - 1983 @ Junk Yards Ford Escape Dismantle, Parts 2008 Ford Escape Mercury Mariner - 254.codebluehacks.org wrote 1995 - 1983 @ Junk Yards Ford Escape Dismantle, Parts 2008 Ford Escape Mercury Mariner - 254.codebluehacks.org
on 05-27-2010 4:57 AM

Pingback from  1995 - 1983 @ Junk Yards Ford Escape Dismantle, Parts 2008 Ford Escape Mercury Mariner - 254.codebluehacks.org

1989 - 2000 @ S Golf Clubs Renewable Choice, Nickent Golf Entertainment - 464.myipgirl.com wrote 1989 - 2000 @ S Golf Clubs Renewable Choice, Nickent Golf Entertainment - 464.myipgirl.com
on 05-27-2010 5:11 AM

Pingback from  1989 - 2000 @ S Golf Clubs Renewable Choice, Nickent Golf Entertainment - 464.myipgirl.com

2005 - 2002 @ Lion Country Safari Tickets, Safari Cars Envoy Xl Used Gmc Acadia - 139.unlockiphone30.net wrote 2005 - 2002 @ Lion Country Safari Tickets, Safari Cars Envoy Xl Used Gmc Acadia - 139.unlockiphone30.net
on 05-27-2010 8:13 AM

Pingback from  2005 - 2002 @ Lion Country Safari Tickets, Safari Cars Envoy Xl Used Gmc Acadia - 139.unlockiphone30.net

1981 - 1980 @ Gmc V3500 Part Fender Flares Matte Black, Motors V3500 Part Fuel - 192.computeronlinebingo.com wrote 1981 - 1980 @ Gmc V3500 Part Fender Flares Matte Black, Motors V3500 Part Fuel - 192.computeronlinebingo.com
on 05-30-2010 5:51 PM

Pingback from  1981 - 1980 @ Gmc V3500 Part Fender Flares Matte Black, Motors V3500 Part Fuel - 192.computeronlinebingo.com

1981 - 2003 @ V323i Headlight Accessories, Wheels 1998 Bmw 323is - 262.unlockiphone30.net wrote 1981 - 2003 @ V323i Headlight Accessories, Wheels 1998 Bmw 323is - 262.unlockiphone30.net
on 05-30-2010 5:55 PM

Pingback from  1981 - 2003 @ V323i Headlight Accessories, Wheels 1998 Bmw 323is - 262.unlockiphone30.net

1986 - 1982 @ Part Energy Gmc C2500, C25k C2500 Tacoma Buy - 431.computeronlinebingo.com wrote 1986 - 1982 @ Part Energy Gmc C2500, C25k C2500 Tacoma Buy - 431.computeronlinebingo.com
on 05-30-2010 6:16 PM

Pingback from  1986 - 1982 @ Part Energy Gmc C2500, C25k C2500 Tacoma Buy - 431.computeronlinebingo.com

2002 - 2007 @ Compaq Presario F500 Battery Prices, Compaq Presario F500 Cell Replacement Hp Pavilion Dv6000 - 378.1fh.org wrote 2002 - 2007 @ Compaq Presario F500 Battery Prices, Compaq Presario F500 Cell Replacement Hp Pavilion Dv6000 - 378.1fh.org
on 05-30-2010 8:22 PM

Pingback from  2002 - 2007 @ Compaq Presario F500 Battery Prices, Compaq Presario F500 Cell Replacement Hp Pavilion Dv6000 - 378.1fh.org

1995 - 1997 @ 1971 Pontiac Grandville Sale Convertible Top, Pontiac Grandville Used Am Fm Music - 177.binggreen.com wrote 1995 - 1997 @ 1971 Pontiac Grandville Sale Convertible Top, Pontiac Grandville Used Am Fm Music - 177.binggreen.com
on 05-30-2010 8:26 PM

Pingback from  1995 - 1997 @ 1971 Pontiac Grandville Sale Convertible Top, Pontiac Grandville Used Am Fm Music - 177.binggreen.com

1999 - 1980 @ Relay January, 240sx Bulb Check Relay - 304.1fh.org wrote 1999 - 1980 @ Relay January, 240sx Bulb Check Relay - 304.1fh.org
on 05-30-2010 8:54 PM

Pingback from  1999 - 1980 @ Relay January, 240sx Bulb Check Relay - 304.1fh.org

2005 - 1992 @ Toyota Previa Dealers, Buy Previa Nissan Murano - 479.computeronlinebingo.com wrote 2005 - 1992 @ Toyota Previa Dealers, Buy Previa Nissan Murano - 479.computeronlinebingo.com
on 05-30-2010 11:54 PM

Pingback from  2005 - 1992 @ Toyota Previa Dealers, Buy Previa Nissan Murano - 479.computeronlinebingo.com

1981 - 1984 @ C55 Ml55 Amg Replacement Part, Ml430 Model Ml55 Amg Fcp Groton - 10.cmanager.org wrote 1981 - 1984 @ C55 Ml55 Amg Replacement Part, Ml430 Model Ml55 Amg Fcp Groton - 10.cmanager.org
on 05-31-2010 1:30 AM

Pingback from  1981 - 1984 @ C55 Ml55 Amg Replacement Part, Ml430 Model Ml55 Amg Fcp Groton - 10.cmanager.org

1986 - 1982 @ 1800 Find, 1800 Sale 2002 Honda Highway Pegs - 311.ja3ra.com wrote 1986 - 1982 @ 1800 Find, 1800 Sale 2002 Honda Highway Pegs - 311.ja3ra.com
on 05-31-2010 2:00 AM

Pingback from  1986 - 1982 @ 1800 Find, 1800 Sale 2002 Honda Highway Pegs - 311.ja3ra.com

1996 - 1985 @ Driver Hp Photosmart 3210 Tout, Natural Gas Grills Weber Spirit E 210 Finest - 473.myipgirl.com wrote 1996 - 1985 @ Driver Hp Photosmart 3210 Tout, Natural Gas Grills Weber Spirit E 210 Finest - 473.myipgirl.com
on 05-31-2010 2:09 AM

Pingback from  1996 - 1985 @ Driver Hp Photosmart 3210 Tout, Natural Gas Grills Weber Spirit E 210 Finest - 473.myipgirl.com

1998 - 1980 @ K2500 Suburban Headlight 6.5 L Petro, Gmc K2500 Used Car Apartments - 260.myipgirl.com wrote 1998 - 1980 @ K2500 Suburban Headlight 6.5 L Petro, Gmc K2500 Used Car Apartments - 260.myipgirl.com
on 05-31-2010 12:33 PM

Pingback from  1998 - 1980 @ K2500 Suburban Headlight 6.5 L Petro, Gmc K2500 Used Car Apartments - 260.myipgirl.com

1981 - 1984 @ Electric Ford Pinto, Pinto Radiator 1979 Ford Repair Estimates - 260.animejin.com wrote 1981 - 1984 @ Electric Ford Pinto, Pinto Radiator 1979 Ford Repair Estimates - 260.animejin.com
on 05-31-2010 1:43 PM

Pingback from  1981 - 1984 @ Electric Ford Pinto, Pinto Radiator 1979 Ford Repair Estimates - 260.animejin.com

1990 - 2005 @ P35 P3005 Van Aftermarket Years Msi K9n Neo F V2, Canon Pixma Ip3500 Driver - 352.renters.ws wrote 1990 - 2005 @ P35 P3005 Van Aftermarket Years Msi K9n Neo F V2, Canon Pixma Ip3500 Driver - 352.renters.ws
on 05-31-2010 1:59 PM

Pingback from  1990 - 2005 @ P35 P3005 Van Aftermarket Years Msi K9n Neo F V2, Canon Pixma Ip3500 Driver - 352.renters.ws

Add a Comment

(required)  
(optional)
(required)  
Remember Me?