gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:17:52 25 4
gpt4 key购买 nike

我正在创建一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com