本文介绍了如何在sql server 2008中实现非对称密钥加密(使用公钥和私钥)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个简单的非对称密钥并使用它。但不确定如何生成和使用私钥和公钥。这是我的sql脚本,它创建非对称密钥然后使用它。



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.

CREATE ASYMMETRIC KEY PacificSales09
    WITH ALGORITHM = RSA_512

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





加密--------------------

-------------- ---------

DECLARE @AsymID INT;



SET @AsymID = ASYMKEY_ID('PacificSales09');



- 使用对称密钥加密部分值MWSSymmetricConfigTableKey

UPDATE configuration_value

SET Section_value = ENCRYPTBYASYMKEY(@ AsymID,'anodfsdfsdfnsmt')



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





解密------------------

--------------- ----------------

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'



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

SET @AsymID = ASYMKEY_ID('PacificSales09');

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

WHERESection_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 WHERESection_Name = 'MWSCommonConfig' AND Section_key = 'SMTPServer'

推荐答案

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?


这篇关于如何在sql server 2008中实现非对称密钥加密(使用公钥和私钥)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 09:43