Warm tip: This article is reproduced from serverfault.com, please click

其他-C#中大数据的RSA加密

(其他 - RSA Encryption of large data in C#)

发布于 2011-12-07 15:37:55

这是我的第一篇文章,所以希望我没有错过任何重要的事情。我正在C#中做一个项目,我需要使用公用/专用密钥加密来加密消息,然后通过SSL连接发送消息。

RSACryptoService根据文档,我选择使用,这是用于加密数据的唯一非对称加密方案。问题是我对此有很多问题。(我想做对称加密,但这不是我的老师想要我做的,对他来说,确定一块大小然后应该为你完成所有工作应该很容易。)运气不好,我尝试了一些不同的方法,但是现在我回到基础知识,然后再试一次,这是我当前的代码:

    public string[] GenerateKeysToStrings(string uniqueIdentifier)
    {
        string[] keys;
        using (var rsa = new RSACryptoServiceProvider(4096))
        {
            try
            {
                string privateKey = rsa.ToXmlString(true);
                string publicKey = rsa.ToXmlString(false);

                this.pki.StoreKey(publicKey, uniqueIdentifier);

                keys = new string[2];
                keys[0] = privateKey;
                keys[1] = publicKey;
            }
            finally
            {
                //// Clear the RSA key container, deleting generated keys.
                rsa.PersistKeyInCsp = false;
            }
        }
        return keys;
    }

如你所见,我生成了密钥,并通过将公钥发送到存储公钥的简单类来模仿PKI,然后将私钥写入文件中(注意,我还有另一种方法可以做到这一点,但是而是将其存储到数组中,只是因为我想No such key exceptions按示例中所示的方式测试并简化获取的内容,有时还需要加密异常,因此我想通过简单地将rsa.ToXmlString字符串存储为字符串来简化它。数组,但没有运气。)

现在,我有一个加密和解密方法,如下所示:

    public string Encrypt(string keyString, string message)
    {
        string encryptedMessage;
        using (var rsa = new RSACryptoServiceProvider())
        {
            try
            {
                //// Load the key from the specified path
                var encryptKey = new XmlDocument();
                encryptKey.Load(@"C:\Test\PrivateKeyInfo.xml");
                rsa.FromXmlString(encryptKey.OuterXml);


                //// Conver the string message to a byte array for encryption
                //// var encoder = new UTF8Encoding();
                ASCIIEncoding byteConverter = new ASCIIEncoding();
                byte[] dataToEncrypt = byteConverter.GetBytes(message);

                byte[] encryptedData = rsa.Encrypt(dataToEncrypt, false);

                //// Convert the byte array back to a string message
                encryptedMessage = byteConverter.GetString(encryptedData);
            }
            finally
            {
                //// Clear the RSA key container, deleting generated keys.
                rsa.PersistKeyInCsp = false;
            }
        }
        return encryptedMessage;
    }

解密:

    public string Decrypt(string keyString, string message)
    {
        string decryptedText;
        using (var rsa = new RSACryptoServiceProvider())
        {
            try
            {
                //// Loads the keyinfo into the rsa parameters from the keyfile
                /*
                var privateKey = new XmlDocument();
                privateKey.Load(keyString);
                 */
                rsa.FromXmlString(keyString);

                //// Convert the text from string to byte array for decryption
                ASCIIEncoding byteConverter = new ASCIIEncoding();
                var encryptedBytes = byteConverter.GetBytes(message);

                //// Create an aux array to store all the encrypted bytes
                byte[] decryptedBytes = rsa.Decrypt(encryptedBytes, false);

                decryptedText = byteConverter.GetString(decryptedBytes);
            }
            finally
            {
                //// Clear the RSA key container, deleting generated keys.
                rsa.PersistKeyInCsp = false;
            }
        }
        return decryptedText;
    }

我知道这是一堵文字墙,但是我希望你能为我提供帮助,因为我已经将头撞墙了很长时间了,这并不好笑:)

问题是,我该如何使用RSA(或其他任何公/私钥加密)加密消息

这是测试客户端:

    public static void Main(string[] args)
    {
        PublicKeyInfrastructure pki = new PublicKeyInfrastructure();
        Cryptograph crypto = new Cryptograph();
        string[] keys = crypto.GenerateKeysToStrings("simonlanghoff@gmail.com");


        string plainText = "Hello play with me, please";
        string publicKey = crypto.GetPublicKey("simonlanghoff@gmail.com");

        string encryptedText = crypto.Encrypt(keys[0], plainText);


        string decryptedText = crypto.Decrypt(keys[1], encryptedText);

    }

如前所述,字符串数组之所以存在,是因为我想消除XML文档中的错误解析错误...

当运行测试客户端时,如果我使用私钥进行加密,而使用公钥进行解密,则会得到“密钥不存在异常”,如果反之,则会得到错误的数据异常。

请大家帮帮我,如果你知道任何好的指南,或者可以告诉我如何对字符串消息进行一些直截了当的公共/私有密钥加密,请帮助我。

感谢你的帮助。

Questioner
Simon Langhoff
Viewed
1
143k 2016-06-24 16:41:09

这不是应该执行RSA加密的方式。

RSA与数学有关。你加密的是一个数字,因此它必须是有限的长度,并且必须与你使用的RSA密钥对长度匹配。所使用的填充(PKCS#1或OAEP)会进一步限制长度。

如果要使用RSA加密大数据,则需要间接进行-即使用对称密钥来加密大数据,并使用RSA公钥对该密钥进行加密。

你可以在我的博客上阅读有关实现此方法的信息