Click here to Skip to main content
15,896,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have implemented a simple asymmetric key and used it.But not sure how private and public keys has to be generated and used.Here is my sql script which creates the asymmetric key and then uses it.

SQL
CREATE ASYMMETRIC KEY PacificSales09
    WITH ALGORITHM = RSA_512

    ENCRYPTION BY PASSWORD = 'MyPassword@Singhania@1';


Encryption--------------------
-----------------------
DECLARE @AsymID INT;

SET @AsymID = ASYMKEY_ID('PacificSales09');

--Encrypt the section Value with symmetric key MWSSymmetricConfigTableKey
UPDATE configuration_value
SET Section_value = ENCRYPTBYASYMKEY(@AsymID,'anodfsdfsdfnsmt')

WHERE Section_Name = 'MWSCommonConfig' AND Section_key = 'SMTPServer'


Decryption------------------
-------------------------------
DECLARE @AsymID INT;

SET @AsymID = ASYMKEY_ID('PacificSales09');
SELECT TOP 5000 CONVERT(CHAR(52), DECRYPTBYASYMKEY(@AsymID, Section_value, N'MyPassword@Singhania@1'))
FROM configuration_value WHERE Section_Name = 'MWSCommonConfig' AND Section_key = 'SMTPServer'
Posted

1 solution

You can have the public and private key pair generated for you :
C#
public static void GenerateKeys(int keySize, out string publicKey, out string publicAndPrivateKey)
{
    using (var provider = new RSACryptoServiceProvider(keySize))
    {
        publicKey = provider.ToXmlString(false);
        publicAndPrivateKey = provider.ToXmlString(true);
    }
}


The public key is sent to anywhere you want, you can share is. It's used as a key to encrypt stuff. The private key is held secret. Only the private key can be used to decrypt data.

Does this cover your question, of do you need more detail?
 
Share this answer
 
Comments
gsinghania2009 30-Sep-14 5:41am    
Hi Eduard,

Thanks but It will be great if you can give some more detail like how to use them while creating the Asymmetric key and also how to use them in the example i have given above.
Thanks in advance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900