Click here to monitor SSC

John Magnabosco

SQL Server Development and Data Security

Encrypting Large Values

Published Thursday, March 12, 2009 5:45 AM

Federal identification numbers and credit card numbers are regulars when it comes to encrypting data. The US plain text version of the prior is eleven characters. The latter is typically sixteen characters. When these are encrypted using a symmetric key the size of the cipher text will range from thirty-nine to sixty eight bytes depending upon the selected algorithm. These lengths hardly raise the question of whether the cipher text will result in a breach of the size limit for a row.

In a nutshell, the row size limit in SQL Server can be explained as follows:

  • The limitation of the row size is a page.
  • A page comprises of 8 kilobytes (8,060 bytes).
  • 8 kilobytes is approximately 8,000 characters.

There may be times when the data that is requiring encryption is very large. Book manuscripts, official documents, schematics, blue prints, images, and archived e-mail messages are just a few examples of data that are stored in a database and can easily be larger than the maximum row size.

Varbinary The Maximus
Encrypted data is stored in the database as a varbinary data type. When defining a column of this data type you have the option to define the size explicitly from 1 to 8,000. You can also define the size as "max" which is 2,147,483,647 bytes (2^31-1) which is approximately 266,437 pages.

The behavior of data that exceeds the row size limit when the "max" size option is utilized depends upon how the sp_tableoption system stored procedure is set for the table in question. If the "large value types out of row" option is set to the value of "1", the varbinary value will automatically be stored on a separate page whether the initial row has reached the row limit or not. If the option is set to "0" the data will be stored on a separate page only if it exceeds the row size limit. The subsequent pages that are used are linked to the initial page with a 16 byte pointer.

Using War and Peace
Leo Tolstoy's famed book "War and Peace" often is used as an example of a very long book. The English translation of this book contains approximately 3.1 million characters. This characteristic made it's text an excellent candidate for illustration of encrypting large text.

Consider the first chapter of this book which, in plain text, is 6,566 bytes in length. Using a symmetric key with the AES_256 algorithm, the results is a cipher text that is 6,612 bytes in length. This certainly fits within the row size limitations; but does not allow for much additional data to be stored within that same row.

Gotcha!
Based upon the definition and behavior of the varbinary(max) data type, the entire text of "War and Peace" should fit within approximately 388 pages. This is certainly within the boundaries of the "max" size limitations. Although, when attempting to encrypt the entire manuscript of this legendary book, using the very same symmetric key, we receive the following error message:

Msg 8152, Level 16, State 10, Line 13
String or binary data would be truncated.

The devil in the details is that the output of the ENCRYPTBYKEY method is a maximum size of 8,000 bytes. Therefore despite our column being set to accept cipher text larger than the row limits the encryption process in SQL Server does not allow us to create cipher text of that size. This is true for the other encryption methods including the HashBytes method.

What, Me Worry?
One possible solution to this challenge would be to break up the large text into segments of less than 8,000 bytes prior to encryption. Book manuscripts have logical break points. Each chapter can be encrypted and stored separately. Implementing a normalized table structure will provide an flexible and organized method of storage.

Another approach might be to seek encryption options externally from the database. Encrypting the large text before it is introduced to the database could yield a broader list of options.

A final consideration is to challenge why the large text requires encryption. The intent to obfuscate data does not necessarily call for encryption. As an alternative, consider converting the plain text directly into a varbinary data type by utilizing the CONVERT method. For example:

INSERT INTO MyTable ([MyVarbinaryColumn]) VALUES
(CONVERT(Varbinary(max),@YourPlainText))

This statement will return a varbinary version of the plain text that is equal to the full length of the string. It's resulting value is not discernable to the naked eye. To return this value to plain text requires only the conversion of the varbinary(max) data to a varchar(max) data type using the same CONVERT method. This approach does not offer the security level that encryption offers; but it may suffice for the intended obfuscation while overcoming the encryption method limitations.

by Johnm

Comments

 

Jason Haley said:

March 12, 2009 9:07 AM
 

Arjan`s World » LINKBLOG for March 12, 2009 said:

March 12, 2009 11:29 AM
 

Topics about Peace » Archive » Encrypting Large Values said:

March 25, 2009 4:06 PM
 

Larry Leonard said:

Regarding "... consider converting the plain text directly into a varbinary data type by utilizing the CONVERT method."  IIRC, BOL states that the binary format for such a conversion is not guaranteed to be the same from version to version of SQL Server, and advises not to do that.  Does that apply here?
November 3, 2009 10:45 PM
 

It Depends said:

Brent posted it first and I’m going to borrow his list to post here: Best Business Intelligence Blog
November 4, 2009 7:18 AM
 

PASS Log Reader Award Winners | Brent Ozar - SQL Server DBA said:

November 5, 2009 12:15 PM
 

PASS Log Reader Award Winners | The SQL UPDATE Statement said:

November 10, 2009 8:36 AM
 

Map Daytona Beach, Daytona 500 Sprint Dale Earnhardt Jr said:

May 20, 2010 1:32 PM
 

Buy Saratoga Tour, 1960 Chrysler Saratoga For Sale Golden Lion said:

May 20, 2010 1:52 PM
 

Dodge Shadow Used Cars, 2005 Honda Shadow Aero Price said:

May 20, 2010 3:42 PM
 

S500 Parts 1995 Mercedes Benz C280, Gs500e Heater Plates said:

May 21, 2010 2:06 AM
 

1999 Bmw 323is Sedan, Bmw 323is Bulb Tail Light said:

May 21, 2010 2:54 AM
 

Volkswagen 412 Replacement Warranty, 412 Help said:

May 21, 2010 5:29 AM
 

1999 Acura Slx Review Dealer, Burton Slx Bios said:

May 21, 2010 5:53 AM
 

Lexus Es350 Tire Pressure Light Reset, Mercedes S350 Photos said:

May 21, 2010 6:14 AM
 

J2000 Fiero Body Kits, 1985 Pontiac J2000 said:

May 21, 2010 6:41 AM
 

Ml350 H7 Arctic Yellow Headlight Bulbs, Hp Ml350 G5 E5335 said:

May 21, 2010 12:59 PM
 

Car Audio Systems Package Boston Acoustics, Discount Audi A3 2.0 Tdi Dsg said:

May 21, 2010 6:59 PM
 

Avalanche Pendant, 2008 Avalanche Lt Fog Lights said:

May 21, 2010 7:10 PM
 

Download Coolpix S550 Digital Camera Li Ion Battery, Denon Dn Hs5500 said:

May 21, 2010 7:40 PM
 

Lw200 Performance Lights, Help Saturn Lw200 said:

May 21, 2010 9:00 PM
 

Rabbit Convertible Radiator Fuel Injector Oxygen Sensor Power Steering Pump, Suburban 1500 Horsepower said:

May 21, 2010 10:06 PM
 

Relay 3 Used Exhaust Saturn Lw200, Lw200 We Buy said:

May 21, 2010 11:13 PM
 

Buy Mercedes Benz 560sel Intake Manifold Egr Valve, 560sel Ancient said:

May 22, 2010 7:05 AM
 

Corvair Exhaust Systems, 1965 Chevrolet Corvair Sale said:

May 22, 2010 3:45 PM
 

Gs350 Sold 350 Car, Gs350 Replacement Light Bulb - 359.tgrconversions.com said:

May 22, 2010 10:39 PM
 

Mercedes S420 Headlight Bulbs, Porsche S420 Radiator C32 Amg - 160.codebluehacks.org said:

May 22, 2010 11:33 PM
 

New Features 2009 Lexus Gs350, Es350 Part Promo New Car - 320.cmanager.org said:

May 22, 2010 11:47 PM
 

530i Cooling System Front, 530i Led 2003 Bmw E39 - 220.cmanager.org said:

May 23, 2010 2:13 AM
 

1963 Pontiac Tempest Super Duty Wagon, Tempest Countermeasures For Facilities - 465.akemet.com said:

May 23, 2010 4:25 AM
 

T1000 Promo Our Pontiac, Pontiac T1000 Discount - 94.cmanager.org said:

May 23, 2010 5:07 AM
 

1991 Chevrolet Caprice Classic Station Wagon, Classic Night Club - 189.tijuanareader.com said:

May 23, 2010 5:35 AM
 

Manual Grand Vitara Xl7, Xl 7 Used 2008 Suzuki Xl7 Grand Vitara - 50.rkwrh.com said:

May 24, 2010 10:27 AM
 

Five Ten Insight Nubuck Leather Abrasion Resistance, Business Insight Zone Day - 461.unlockiphone30.net said:

May 24, 2010 1:44 PM
 

2007 Ford Freestyle Brake Problems, Freestyle Dive Watch - 237.rkwrh.com said:

May 25, 2010 6:50 PM
 

Oem Chrysler Lhs Sebring Convertible, Plastic Tanks Chrysler Sebring Replacement Radiator - 82.renters.ws said:

May 25, 2010 8:38 PM
You need to sign in to comment on this blog

About Johnm

John Magnabosco manages the Data Services Group at one of the fastest growing companies in the United States. He is also a Co-Founder of the Indianapolis Professional Association for SQL Server (IndyPASS), Co-Founder of IndyTechFest, the author of the book titled "Protecting SQL Server Data" and contributing author of "SQL Server MVP Deep Dives Volume 2".
<March 2009>
SuMoTuWeThFrSa
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234
Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Converting String Data to XML and XML to String Data
 We all appreciate that, in general, XML documents or fragments are held in strings as text markup. In... Read more...

Geek of the Week: Don Syme
 With the arrival of F# 3.0 Microsoft announced a wide range of improvements such as type providers that... Read more...

How to Document and Configure SQL Server Instance Settings
 Occasionally, when you install identical databases on two different SQL Server instances, they will... Read more...

What's the Point of Using VARCHAR(n) Anymore?
 The arrival of the (MAX) data types in SQL Server 2005 were one of the most popular feature for the... Read more...