在C#中大量数据的RSA加密 [英] RSA Encryption of large data in C#

查看:170
本文介绍了在C#中大量数据的RSA加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一篇,所以希望我没有错过任何重要的东西。我做在C#中,我需要使用公钥/私钥加密对消息进行加密,然后把它通过SSL连接的项目。

This is my first post, so hope I haven't missed anything important. I'm doing a project in C# where I need to use public/private key encryption to encrypt a message and then send it over an SSL connection.

我选择使用在 RSACryptoService ,因为根据文档,这是用于加密数据的唯一非对称加密方案。问题是,我有很多这种问题。 (我想做对称加密,但是这不是我的老师希望我做什么,并根据他应该很容易,只是确定一个块的大小,然后它应该做的所有的工作适合你。)好了,到目前为止,没有运气,我已经尝试了一些不同的方法,但现在我回到基本知识,并尝试再次,这是我当前的代码:

I chose to use the RSACryptoService, as according to the documentation, that was the only asymmetric encryption scheme used for encrypting data. The problem is that I am having a lot of problems with this. (I wanted to do symmetric encryption, but that is not what my teacher want me to do, and according to him it should be easy to just determine a block size and then it should do all the work for you.) Well, so far no luck and I've tried some different approaches, but now I'm back to basics and trying again, this is my current code:

    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;
    }



正如你所看到的,我生成密钥和我通过发送mimmick一个PKI公共密钥存储,然后私钥被写入到文件
一个简单的类(请注意,我也有另一种不一样的,但其存储为一个数组的方法,只是因为我想测试和简化的东西,因为我得到没有这样的键例外有时加密例外,当我做示例中所示的方式,所以我想通过简单地存储<简化它code> rsa.ToXmlString 字符串,如数组字符串,但没有运气。)

As you can see, I generate the keys and I mimmick a PKI by sending the public key to a simple class that stores it, and then the private key is written to a file (Notice that I also have another method that does the same but stores it to an array instead, just because I wanted to test and simplify things as I get No such key exceptions and sometimes cryptographic exceptions when I do it the way shown in the example, so I wanted to simplify it by simply storing the rsa.ToXmlString string, as a string in an array, but no luck.)

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

Now I have an encrypt and decrypt method as follows:

    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;
    }



解密:

Decryption:

    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;
    }



我知道,这是文字的墙,但我希望你能帮助我,因为我一直在敲打我的头撞墙了这么久,现在,它并不好笑:)

I know that this is a wall of text, but I hope you can help me out because I've been banging my head against the wall for so long now it's not funny :)

现在的问题是,我怎么去用<加密消息code> RSA (或任何其他公共/私人密钥加密)

The problem is, how do I go about encrypting messages with RSA (or any other public/private key encryption)

下面是测试客户端:

    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文档...

As I mentioned, the string arrays are there because I wanted to eliminate bad parsing error from XML documents...

当我运行测试客户端,如果我用私钥加密和公钥来解密,我得到一个关键不存在异常,如果我周围做其他的方式,我得到一个坏数据的异常。

When I run the test client, if I use the private key to encrypt and public key to decrypt, I get a "Key does not exist exception" and if I do it the other way around, I get a bad data exception.

请帮我出人,如果你知道任何好导游,或者可以告诉我怎么有点straightfoward实施串消息的公钥/私钥加密,请帮助我。

Please help me out guys, if you know of any good guide, or can tell me how to somewhat straightfoward implement public/private key encryption on string messages, please help me out.

我感谢所有帮助。

推荐答案

这是不是如何RSA加密应该做的。

This is not how RSA encryption should be done.

RSA是所有关于数学。你加密的是一个号码,以便它必须是有限的长度和匹配您正在使用的RSA密钥长度。另外长度的限制被填充征收使用(或PKCS#1或OAEP)

RSA is all about math. What you encrypt is a number so it has to be of finite length and matching the RSA keypair length you're using. Further length limitations are imposed by the padding used (either PKCS#1 or OAEP).

如果要加密与RSA大数据需要间接地做到这一点 - 即。使用对称密钥加密大数据和加密使用RSA公钥这个密钥

If you want to encrypt large data with RSA you need to do it indirectly - i.e. use a symmetric key to encrypt the large data and encrypt this key using the RSA public key.

您可以阅读我的​​博客

这篇关于在C#中大量数据的RSA加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆