Category: Blog

  • Using a Symmetric Key

    In my Encryption Primer talk, I do demo on symmetric key use, and wanted to document it here. Encryption is a serious subject, and please do your research and education, as well as testing, before you implement it.

    This post will look at some simple encryption and decryption using symmetric keys. I showed how to create a symmetric key before, so I won’t talk about that here, but I’ll just show the code.

    Let’s set up a test table. In this case, I’m showing salary in a table, which isn’t something you want to do, but you might have this in an existing application: data you need to encrypt, but it’s stored unencrypted.

    -- create a table 
    create table Employees ( 
     id int identity(1,1)
     , firstname varchar(200)
     ,lastname varchar(200)
     ,title varchar(200)
     ,salary numeric(10, 4) );
    go
    insert Employees 
    values
      ('Steve','Jones','CEO', 5000)
     , ('Delaney','Jones','Manure Shoveler', 10)
     , ('Kendall','Jones','Window Washer', 5)
    ;
    go 

    Here I have three employees and salaries. Now I want to encrypt the salary. However I cannot just encrypt this value. Encryption creates a binary representation of the data, and that won’t fit in a varchar field. So I need to add a column.

    alter table Employees 
     add EnryptedSalary varbinary(max);
    go 

    Now I have a placeholder, so let’s create a key and then update the new binary column with the encrypted value.

    -- create a symmetric key
    create symmetric key MySalaryProtector
     WITH ALGORITHM=AES_256
     , IDENTITY_VALUE = 'Salary Protection Key'
     , Key_SOURCE = N'Keep this phrase a secr#t' 
     ENCRYPTION BY PASSWORD = 'Us#aStrongP2ssword'
    ;
    go 
    -- open the key 
    open symmetric key MySalaryProtector
     decryption by password='Us#aStrongP2ssword'
    ;
    
    -- encrypt the data 
    update Employees 
     set EnryptedSalary = ENCRYPTBYKEY(key_guid('MySalaryProtector'),cast(salary as nvarchar))
    ; 
    go 
    -- remove the old data 
    update employees
     set salary = 0
    ; 
    go 

    Note that I open the key, which is needed. I can close it at the end, or it will close when my session ends. I don’t close it here as I usually run this demo in the course of one session.

    The encryption takes place with the ENCRYPTBYKEY function, which requires the GUID of the key. Why the GUID and not the name I don’t know, but it seems like a PIA, halfway implementation. In any case, the KEY_GUID function is used as the first parameter.

    The number needs to be cast as a character, so I do that first, and then it’s the next parameter in the function. If I look at the data, it looks like this:

    select id , firstname , lastname , title , salary , EnryptedSalary
     from Employees; go 

    encryptsymm

    If you use the same identity_value and key_source, you should get the same encryption.

    To decrypt it, I do this:

    -- decrypt the data, with the casting  select id  , firstname  , lastname  , title  , Salary = cast(cast(DecryptByKey(EnryptedSalary) as nvarchar) as numeric(10,4))  , EnryptedSalary  from Employees go 

    The encrypted value has a header that tells it what key is used, so as long as it’s open, this works.

  • Last Week to Enter the Exceptional DBA Contest

    Just a reminder: you have until this Friday to enter the 2012 Exceptional DBA awards. I’m judging, and looking forward to reading the entries next week, so get them in.

    A hint: communication skills matter. Take some time and think about what you want to write for each question.

  • Down and Dark

    I came back from vacation to find the home DSL line bouncing up and down a bit. I wasn’t overly worried, and the kids knew to restart the router when it went down, crossing their fingers for a good connection. I suspected the router might be dying, and was getting mentally prepared to go hardware shopping. I figured I’d enjoy the downtime and stay unwired for the last few days before going back to work.

    On Sunday night, our connection was down for good, with a red light on the router. I called Monday morning into the Century Link tech support line, and they informed me they knew there was an outage. Good, I thought, they’ll have it fixed soon. I packed up and spent Monday in town working at the library to not fall any further behind.

    Tuesday morning had the line still down, despite complaints to Century Link. The people answering the phone apparently are not any more informed by their engineers than I am. I find that hard to believe, but there wasn’t much I could do. I packed up again, and am in town working again.

    Broadband access has almost become a commodity, and I’d argue that for many of us, it’s a utility. We pay bills online, get sent all sorts of information online, many of us not even getting newspapers anymore. It’s not longer a luxury, but a necessity for us to make do in the modern world. For me, it’s part of my job, and the lack of a line is a huge impact to me getting work done.

    Fingers are crossed that this gets fixed soon as work is piling up.

  • Almost an MCSA

    I took four beta tests earlier this year for SQL Server 2012. I had debated writing another book, and I’ve worked on a few of the cert books in the past, so I thought this would be a good chance to look at the tests. Since I had free invites to the beta program, I also thought, what the heck. The worst that can happen is I fail.

    It’s been about two months, and I was waiting for results when someone mentioned that the Prometric site had the results, though they weren’t yet up on the MCP site. I checked, and sure enough I had results for some of my exams. I checked a few days later, and found results for all four of my tests. They were:

    All passed, which was cool. I think certification is what you make of it, and I’ve been looking over lots of SQL Server 2012 stuff for almost a year, so to me it says I have some good knowledge of SQL Server 2012. The tests were a good mix of old and new stuff, and not as focused on the new features, which was good.

    The MCSA track for SQL Server 2012 needs 461, 462 and 463. I skipped 463 in the beta cycle because I’m not much of a DW person, and I was busy. Fitting in 4 exams across two weeks was hard, and took time away from work that I had to make up at night.

    I’m not overly concerned about being certified, but it’s a nice feather to have in your cap. The MCSE for SQL Server 2012 requires the MCSA and 464 and 465, which I have. So one more test and I’d have the MCSE as well.

    My goal now is to start studying for 463, after I get a few more presentations built that I need for work. I hope to get to this test in the fall, or perhaps at the end of the year, depending on my schedule. I haven’t done a lot with the DW side of things, so I’ll be working, and likely writing, about a few more DW topics later this year.