在搜索RSACypthyServiceProvider.I时,我在MSDN上找到了下面的代码示例,在注释的帮助下,我无法理解代码的某些部分。
有谁能解释一下吗?
代码示例:
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);
}
}发布于 2012-05-23 17:00:49
RSA非常慢,并且有填充开销。因此,通常会生成一个随机对称密钥,用RSA加密它,然后用对称密钥对消息进行加密。这种方法被称为混合密码体制。
如果单个密钥用于加密多条消息,则IVs很重要,但是由于这段代码为每条消息创建了一个新密钥,所以IV在这里并不重要。仍然使用IV可以防止多目标攻击,所以使用唯一的密钥并不是完全无用的,特别是当密钥只有128位时。
这段代码也非常低效:它分别加密IV和密钥,而不是将它们连接起来。这是RSA开销的两倍。
模和指数是RSA公钥的两个部分。查找维基百科的详细信息。指数通常被选择为2^16 + 1 = 65537,它对应于此代码中的{1,0,1}。
https://stackoverflow.com/questions/10724509
复制相似问题