加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#System.Security.Cryptography – 为什么要指定键和IV两次?

发布时间:2020-12-16 10:08:21 所属栏目:百科 来源:网络整理
导读:大多数显示如何使用System.Security.Cryptography中的AES代码进行加密的示例如下所示: using (Aes aes = Aes.Create()){ aes.Key = Key; aes.IV = IV; ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key,aes.IV); 是否有必要指定键和IV两次 – 一
大多数显示如何使用System.Security.Cryptography中的AES代码进行加密的示例如下所示:

using (Aes aes = Aes.Create())
{
   aes.Key = Key;
   aes.IV = IV;
   ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key,aes.IV);

是否有必要指定键和IV两次 – 一次到Aes对象,一次到CreateEncryptor()函数?或者在这种情况下,因为我已经指定了键和IV,我可以调用不带参数的CreateEncryptor()形式吗?

解决方法

你不必.如果使用属性指定值,则可以使用 the CreateEncryptor() overload(不带参数):

public virtual ICryptoTransform CreateEncryptor() {
    return CreateEncryptor(Key,IV);
}

这是the doc所说的:

Creates a symmetric encryptor object with the current Key property and initialization vector (IV).

或者,如果之后不使用aes对象创建其他加密器/解密器,则可以省略设置属性并使用带参数的重载.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读