Alexander Galkin
Alexander Galkin

Reputation: 12534

RSA encryption in .Net using the public key from SQL Server?

I would like to perform some encryption on client using the asymmetric key stored and generated in SQL Server.

I can get the public key from the DMV using the following query:

SELECT public_key FROM sys.asymmetric_keys WHERE name = 'KeyName'

This returns the following value:

0x06020000002400005253413100020000010001008B656455D4C56392C45EEC3563203635F5F42DDA57069E7A880BF0AF055174A2A165DED75BA4E73E2A09BCBFAA50042B4E27354C1FEB3361F81C381AFF59A6A7

How can I use this binary value as public key for RSA_512 encryption in .Net? I have searched through similar questions here but failed to find any appropriate solution: I either need the key in XML form or at least to know .Modulus and .Exponent of the public key. Can I get it from these binary sequence?


EDIT:

Here is my code

   SqlCommand cmd = new SqlCommand(string.Format("SELECT dbo.GetKey(@KeyName)", conn);
   cmd.Parameters.AddWithValue("@KeyName", ConfigurationManager.AppSettings.Get("PublicKeyName"));
   PublicKey = (byte[]) cmd.ExecuteScalar();

   var rsa=new System.Security.Cryptography.RSACryptoServiceProvider(Convert.ToInt32(ConfigurationManager.AppSettings.Get("KeyLength")));
   rsa.ImportCspBlob(PublicKey);
   EncryptedData = rsa.Encrypt(Data,false);

I get the exception "Key not valid" in the last line.

Upvotes: 0

Views: 3441

Answers (1)

Eugen Rieck
Eugen Rieck

Reputation: 65274

Security.Cryptography.RSACryptoServiceProvider rsa=new Security.Cryptography.RSACryptoServiceProvider(512);
rsa.ImportCspBlob(keyblob)

with keyblob being a byte[] representation of the above key.

This comes directly from my shell:

eugen@lucidhome:~$ csharp
Mono C# Shell, type "help;" for help

Enter statements below.
csharp> using System.Security.Cryptography;                                                                                                                                                          
csharp> String keystring="06020000002400005253413100020000010001008B656455D4C56392C45EEC3563203635F5F42DDA57069E7A880BF0AF055174A2A165DED75BA4E73E2A09BCBFAA50042B4E27354C1FEB3361F81C381AFF59A6A7"; 
csharp> byte[] keybytes=new byte[keystring.Length/2];
csharp> for (int i=0;i<keystring.Length/2;i++) keybytes[i]=Convert.ToByte(keystring.Substring(2*i,2),16);
csharp> RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(512);
csharp> rsa.ImportCspBlob(keybytes);
csharp> byte[] cleartext=new byte[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
csharp> byte[] ciphertext=rsa.Encrypt(cleartext,false);
csharp> ciphertext;
{ 62, 80, 66, 63, 4, 132, 69, 249, 97, 164, 131, 34, 89, 117, 173, 177, 222, 77, 23, 177, 127, 236, 116, 121, 25, 63, 59, 159, 182, 235, 190, 44, 24, 101, 127, 61, 185, 72, 93, 69, 66, 248, 64, 249, 5, 183, 214, 189, 252, 75, 22, 123, 159, 50, 13, 90, 137, 187, 180, 181, 252, 174, 98, 247 }
csharp> keybytes;
{ 6, 2, 0, 0, 0, 36, 0, 0, 82, 83, 65, 49, 0, 2, 0, 0, 1, 0, 1, 0, 139, 101, 100, 85, 212, 197, 99, 146, 196, 94, 236, 53, 99, 32, 54, 53, 245, 244, 45, 218, 87, 6, 158, 122, 136, 11, 240, 175, 5, 81, 116, 162, 161, 101, 222, 215, 91, 164, 231, 62, 42, 9, 188, 191, 170, 80, 4, 43, 78, 39, 53, 76, 31, 235, 51, 97, 248, 28, 56, 26, 255, 89, 166, 167 }
csharp>  

Upvotes: 1

Related Questions