gpt4 book ai didi

c# - RijndaelManaged 实例的用途?

转载 作者:太空宇宙 更新时间:2023-11-03 14:00:58 24 4
gpt4 key购买 nike

我在搜索 RSACypthyServiceProvider 时在 MSDN 上找到了以下代码示例。在评论的帮助下我无法理解代码的某些部分。

  1. 什么是模数和指数?

  2. 什么是IV?

  3. 为什么他们使用 RijndaelManagedclass 进行非对称加密?根据我的搜索,RSACryptoServiceProvider 提供了非对称加密功能,它会在我们创建对象时自动创建私钥和公钥。那么这里 RijndaelManaged 实例的用途是什么?

谁能解释一下?

代码示例:

class Class1
{

static void Main()
{

//Initialize the byte arrays to the public key information.
byte[] PublicKey = {Somethink in byte}

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

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

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

//Create a new instance of the RSAParameters structure.
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);
}
}

最佳答案

RSA 非常慢,并且有填充开销。所以通常生成一个随机对称 key ,用 RSA 加密它,然后用对称 key 加密消息。这种方法被称为 hybrid cryptosystem .

如果使用单个 key 加密多条消息,则 IV 很重要,但由于此代码为每条消息创建一个新 key ,因此 IV 在这里并不重要。仍然使用 IV 可以防止多目标攻击,因此它对唯一 key 并非完全无用,尤其是当 key 只有 128 位时。

此代码也非常低效:它分别加密 IV 和 key ,而不是将它们连接起来。这会使 RSA 开销加倍。

模数和指数是 RSA 公钥的两个部分。查阅维基百科了解详情。指数通常选择2^16 + 1 = 65537,对应这段代码中的{1,0,1}

关于c# - RijndaelManaged 实例的用途?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10724509/

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