c# - 通过提供模数和指数进行 RSA 加密

标签 c# winforms encryption rsa

我正在创建一个 C# Winforms 应用程序,它通过 HTTPS 将数据发布到服务器。

登录机制应该是这样的:

  1. 我将用户名发送到服务器,它以 rsa-modulus 和 rsa-exponent 响应

  2. 我使用这些给定的参数对密码进行加密,然后将用户名+密码发送到服务器进行身份验证

我试过 RSACryptoServiceProvider 类,但我找不到样本或任何关于我们如何使用给定模数和指数进行加密的内容?

我认为在不指定任何值的情况下,它会执行默认加密参数..

所以如果有人以前这样做过,他们能给我一些提示吗?谢谢

更新:根据 Carsten Konig 先生的建议,.我已尝试使用 RSAParameters 和 RSA.ImportParameters 执行此操作,但它会返回带有加密异常的“BAD DATA”错误。我的代码如下。

我也尝试过 RSA.FromXmlString(mykey); (其中 mykey 包含一个带有模数和 exp 的 xml 字符串)但我也得到一个带有加密异常的“BAD DATA”错误......有人知道吗?或者如果它是一些微软错误,任何人都可以建议其他一些像样的库来轻松地做到这一点吗?

RSAParameters rsaparam = new RSAParameters(); 
rsaparam.Modulus = modbytes; 
rsaparam.Exponent = expbytes; 
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider() ; 
RSA.ImportParameters(rsaparam); 
byte[] encryptedData = RSA.Encrypt(dataToEncrypt, false)

最佳答案

您可以使用 RSACryptoServiceProvider.Encrypt 来做到这一点方法。您还需要使用 RSACryptoServiceProvider.ImportParameters方法并传递给它 RSAParameters结构(这是您设置指数、模数等的地方)。

请查看 RSAParameters 链接中的文档 - 它很好地记录了您必须为什么结构字段传递什么参数 - 如果您现在使用算法应该没问题。

编辑:这里是直​​接来自 MSDN-site 的示例:

class RSACSPSample
{

    static void Main()
    {
        try
        {       //initialze the byte arrays to the public key information.
            byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56,
                                   74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222,
                                   207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175,
                                   108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194,
                                   240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139,
                                   168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171,
                                   38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93,
                                   106,99,179,68,175,211,164,116,64,148,226,254,172,147};

            byte[] Exponent = {1,0,1};

            //Values to store encrypted symmetric keys.
            byte[] EncryptedSymmetricKey;
            byte[] EncryptedSymmetricIV;

            //Create a new instance of RSACryptoServiceProvider.
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

            //Create a new instance of RSAParameters.
            RSAParameters RSAKeyInfo = new RSAParameters();

            //Set RSAKeyInfo to the public key values. 
            RSAKeyInfo.Modulus = PublicKey;
            RSAKeyInfo.Exponent = Exponent;

            //Import key parameters into RSA.
            RSA.ImportParameters(RSAKeyInfo);

            //Create a new instance of the RijndaelManaged class.
            RijndaelManaged RM = new RijndaelManaged();

            //Encrypt the symmetric key and IV.
            EncryptedSymmetricKey = RSA.Encrypt(RM.Key, false);
            EncryptedSymmetricIV = RSA.Encrypt(RM.IV, false);

            Console.WriteLine("RijndaelManaged Key and IV have been encrypted with RSACryptoServiceProvider."); 

        }
        //Catch and display a CryptographicException  
        //to the console.
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

请注意只有 key /iv 被加密——不是任意字节——这些字节的长度也很重要!

允许的长度在 MSDN 中有描述,具体取决于操作系统!

关于c# - 通过提供模数和指数进行 RSA 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9839274/

相关文章:

vb.net - MouseHover 事件中的鼠标坐标?

javascript - 使用 Diffie-Hellman key 交换和 AES 通过 HTTP 进行客户端加密

c++ - 无法从 AES-128-GCM 获得正确的输出

c# - 获取 HttpPostedFileBase 的文件信息

c# - MVC 编辑存储在隐藏字段中的复杂对象

vb.net - ContextMenuStrip 不显示附近的光标

c# - app.config 中的 maxReceivedMessageSize 和 maxBufferSize

java - .NET 系统加密到 Bouncy CaSTLe Java 解密抛出错误

c# - 如何在 Prism 框架中的模块之间正确发送事件消息?

c# - 如何使用mysql在datagridview中的两个不同列中显示具有相同id的两行?