如何在 RSA 上设置 KeySize?
How to set KeySize on RSA?
如何设置 RSA
class 上的密钥大小?
RSA.Create()
没有密钥大小选项,在创建 RSA
后设置 KeySize
没有任何效果。
RSA 只是 RSA 实现的抽象 class。你应该使用 RSACryptoServiceProvider
.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(bitSize);
请注意,在您尝试使用它之前不会生成密钥,因此不要将构造函数本身放在后台工作程序等中。
如果您使用的是 .NET Framework:
没有提供商不知道的解决方案。您必须使用 RSACryptoServiceProvider(int)
构造函数或有意创建一个 RSACng
对象。
如果您使用的是 .NET Core:
RSA rsa = RSA.Create();
rsa.KeySize = someValue
是正确的方法,它适用于 RSA.Create() 的所有可能答案。
如果您使用的是单声道:
我不知道它匹配的是哪种行为。
如果你来自未来:
https://github.com/dotnet/corefx/issues/8688 正在跟踪未来添加 RSA.Create(int)
(和 RSA.Create(RSAParameters)
)以帮助解决此问题。
需要交叉编译的作用域方法:
(为您的构建正确定义 NETFX 并将其排列在 nuget 包中是留给 reader 的练习)
internal static RSA RsaCreate(int keySize)
{
#if NETFX
// If your baseline is .NET 4.6.2 or higher prefer RSACng
// or 4.6+ if you are never giving the object back to the framework
// (4.6.2 improved the framework's handling of those objects)
// On older versions RSACryptoServiceProvider is the only way to go.
return new RSACng(keySize);
#else
RSA rsa = RSA.Create();
rsa.KeySize = keySize;
if (rsa.KeySize != keySize)
throw new Exception("Setting rsa.KeySize had no effect");
return rsa;
#endif
}
当然,如果您来自未来,您可以直接以更高的优先级调用新的 Create 重载#if.
如何设置 RSA
class 上的密钥大小?
RSA.Create()
没有密钥大小选项,在创建 RSA
后设置 KeySize
没有任何效果。
RSA 只是 RSA 实现的抽象 class。你应该使用 RSACryptoServiceProvider
.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(bitSize);
请注意,在您尝试使用它之前不会生成密钥,因此不要将构造函数本身放在后台工作程序等中。
如果您使用的是 .NET Framework:
没有提供商不知道的解决方案。您必须使用 RSACryptoServiceProvider(int)
构造函数或有意创建一个 RSACng
对象。
如果您使用的是 .NET Core:
RSA rsa = RSA.Create();
rsa.KeySize = someValue
是正确的方法,它适用于 RSA.Create() 的所有可能答案。
如果您使用的是单声道:
我不知道它匹配的是哪种行为。
如果你来自未来:
https://github.com/dotnet/corefx/issues/8688 正在跟踪未来添加 RSA.Create(int)
(和 RSA.Create(RSAParameters)
)以帮助解决此问题。
需要交叉编译的作用域方法:
(为您的构建正确定义 NETFX 并将其排列在 nuget 包中是留给 reader 的练习)
internal static RSA RsaCreate(int keySize)
{
#if NETFX
// If your baseline is .NET 4.6.2 or higher prefer RSACng
// or 4.6+ if you are never giving the object back to the framework
// (4.6.2 improved the framework's handling of those objects)
// On older versions RSACryptoServiceProvider is the only way to go.
return new RSACng(keySize);
#else
RSA rsa = RSA.Create();
rsa.KeySize = keySize;
if (rsa.KeySize != keySize)
throw new Exception("Setting rsa.KeySize had no effect");
return rsa;
#endif
}
当然,如果您来自未来,您可以直接以更高的优先级调用新的 Create 重载#if.