AsymmetricAlgorithm.KeySize 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置非对称算法所用密钥模块的大小(以位为单位)。
public:
virtual property int KeySize { int get(); void set(int value); };
public virtual int KeySize { get; set; }
member this.KeySize : int with get, set
Public Overridable Property KeySize As Integer
属性值
非对称算法所用密钥模块的大小(以位为单位)。
例外
密钥模块大小无效。
示例
下面的代码示例演示如何重写 属性, KeySize 以验证它是否属于局部 keySizes
成员变量中标识的范围。 此代码示例是为 AsymmetricAlgorithm 类提供的一个更大示例的一部分。
public:
property int KeySize
{
virtual int get() override
{
return KeySizeValue;
}
virtual void set(int value) override
{
for (int i = 0; i < customValidKeySizes->Length; i++)
{
if (customValidKeySizes[i]->SkipSize == 0)
{
if (customValidKeySizes[i]->MinSize == value)
{
KeySizeValue = value;
return;
}
}
else
{
for (int j = customValidKeySizes[i]->MinSize;
j <= customValidKeySizes[i]->MaxSize;
j += customValidKeySizes[i]->SkipSize)
{
if (j == value)
{
KeySizeValue = value;
return;
}
}
}
}
// If the key does not fall within the range identified
// in the keySizes member variable, throw an exception.
throw gcnew CryptographicException("Invalid key size.");
}
}
public override int KeySize
{
get { return KeySizeValue; }
set
{
for (int i=0; i < keySizes.Length; i++)
{
if (keySizes[i].SkipSize == 0)
{
if (keySizes[i].MinSize == value)
{
KeySizeValue = value;
return;
}
}
else
{
for (int j = keySizes[i].MinSize;
j <= keySizes[i].MaxSize;
j += keySizes[i].SkipSize)
{
if (j == value)
{
KeySizeValue = value;
return;
}
}
}
}
// If the key does not fall within the range identified
// in the keySizes member variable, throw an exception.
throw new CryptographicException("Invalid key size.");
}
}
Public Overrides Property KeySize() As Integer
Get
Return KeySizeValue
End Get
Set(ByVal Value As Integer)
For i As Int16 = 0 To keySizes.Length - 1 Step i
If (keySizes(i).SkipSize.Equals(0)) Then
If (keySizes(i).MinSize.Equals(Value)) Then
KeySizeValue = Value
Return
End If
Else
For j As Integer = keySizes(i).MinSize _
To keySizes(i).MaxSize _
Step keySizes(i).SkipSize
If (j.Equals(Value)) Then
KeySizeValue = Value
Return
End If
Next
End If
Next
' If the key does not fall within the range identified
' in the keySizes member variable, throw an exception.
Throw New CryptographicException("Invalid key size.")
End Set
End Property
注解
有效密钥大小由非对称算法的特定实现指定,并在 属性中 LegalKeySizes 列出。