Home

ExecuteQuery is having trouble with byte[]

I am trying to build a helper function to insert data into a column that is using column encryption with a symmetric key

The decrypt is working fine, using the following code:

T Decrypt<T>(Binary value)
{
    return this.ExecuteQuery<T>(@"
    OPEN SYMMETRIC KEY symtest
    DECRYPTION BY CERTIFICATE certtest;
    select convert(varchar, DecryptByKey({0})) as data;
    CLOSE SYMMETRIC KEY symtest
    ", value).First();
}

But I can't seem to get a similar Encrypt function to work

Binary Encrypt<T>(T value)
{
    return ExecuteQuery<Binary>(@"
    OPEN SYMMETRIC KEY symtest
    DECRYPTION BY CERTIFICATE certtest;
    select EncryptByKey(Key_GUID('symtest'), {0}) as data;
    close symmetric key symtest
    ", value)
    .First()
    ;
}

Since Binary/byte[] do not have a public parameterless constructor.
If I try using string, I get an error though that I can not convert byte[] to string
So the driver seems to know what the underlying type is, but it can't manage to give it to me.

Sign In or Register to comment.