Search code examples
c#sql-server-2008desencryption-symmetric

EncryptByPassPhrase key given in bytes


Friends,

I want to use EncryptByPassPhrase in SQL SERVER (which I found here http://msdn.microsoft.com/en-us/library/ms190357.aspx) to update existing values in a table by encrypting them. However, the passphrase (or key) is given in bytes such as:

private static readonly byte[] TrippleDesKey = new byte[] { 0xF1, 0x07, 0xE6, 0x13, 0xBA, 0x85, 0x7F, 0xDC, 0x6D, 0x85, 0x67, 0x9B, 0x68, 0x7A, 0xC7, 0x1F, 0x10, 0xBA, 0xB0, 0x2F, 0xA2, 0xAE, 0xDA, 0xEA };

I want to apply EncryptByPassPhrase using the TrippleDesKey variable.

Any suggestions on how I can do that? I assume if I convert the TrippleDesKey variable into string I could just simple use it EncryptByPassPhrase.

I tried converting the TrippleDesKey variable such as

string result = System.Text.Encoding.UTF8.GetString(TrippleDesKey); 
Console.WriteLine(result);

and the ouptut was �����m�g�hz���/����

I used �����m�g�hz���/���� in EncryptByPassPhrase but no value is encrypted i.e., an empty string is returned.


Solution

  • You can just join the bytes hex values in only one value and use it:

    new byte[] { 0xF1, 0x07, 0xE6, 0x13, 0xBA, 0x85, 0x7F, 0xDC, 0x6D, 0x85, 0x67, 0x9B, 0x68, 0x7A, 0xC7, 0x1F, 0x10, 0xBA, 0xB0, 0x2F, 0xA2, 0xAE, 0xDA, 0xEA } = 0xF107E613BA857FDC6D85679B687AC71F10BAB02FA2AEDAEA
    

    Then use it like this:

    EncryptByPassPhrase( 0xF107E613BA857FDC6D85679B687AC71F10BAB02FA2AEDAEA, 'test')