SQL Encrypted Columns in WHERE Clause

后端 未结 3 2103
难免孤独
难免孤独 2020-12-31 07:51

I am looking to apply SQL column level encryption using symmetric keys. The initial steps needed to create the Database Master Key, Certificates and Symmetric Keys seems st

3条回答
  •  借酒劲吻你
    2020-12-31 08:33

    The typical way is to store both the encrypted value and a one-way hash of the value. When you seek a specific value, you would seek the hash. This way you can query efficiently, w/o having to decrypt every row in order to find the value you're interested:

    create table Table (
    EncryptedColumn varbinary(max),
    HashValue binary(20),
    PlainA int,
    PlainB varchar(256),
    PlainC Datetime);
    
    create index ndxTableHash on Table(HashValue);
    
    select PlainA, plainB, PlainC
    from table
    where HashValue = HashBytes('SHA1', @searchTerm);
    

    In theory, you can have a hash conflict once in a blue moon, to be paranoid-safe you add a double check on the decrypted column:

    select PlainA, plainB, PlainC
    from table
    where HashValue = HashBytes('SHA1', @searchTerm)
    and DecryptByKey(..., EncryptedColumn) = @searchTerm;
    

    Also see Indexing encrypted data and SQL Server 2005: searching encrypted data.

提交回复
热议问题