Windows 8.1 但不是 Windows 7 上的 CryptographicException(参数不正确)
CryptographicException (The parameter is incorrect) on Windows 8.1 but not Windows 7
我有一些代码在 Windows 7 上运行良好,但现在我已经开始使用 Windows 8.1 开发箱(请参阅下面的重现代码)后失败了。
Windows 8.1 与 Windows 7 上的 RSA 是否不同?
using System.Security.Cryptography;
using System.Text;
namespace RsaBug
{
class Program
{
static void Main()
{
var modulus = Encoding.UTF8.GetBytes("rvco6d27bsw2fw5qx7okdcu5jahd1ifh22is76k5xyau3wjv7plo0rom66h2434tvm29cmq2ov6mbjo30bymb14j2dst5fzy7pd");
var exponent = Encoding.UTF8.GetBytes("1ekh");
using (var rsa = new RSACryptoServiceProvider())
{
//Get an instance of RSAParameters from ExportParameters function.
var rsaKeyInfo = rsa.ExportParameters(false);
//Set RSAKeyInfo to the public key values.
rsaKeyInfo.Modulus = modulus;
rsaKeyInfo.Exponent = exponent;
//Import key parameters into RSA.
rsa.ImportParameters(rsaKeyInfo); // on Windows 8.1, this throws
}
}
}
}
这是我得到的异常:
System.Security.Cryptography.CryptographicException was unhandled
_HResult=-2147024809
_message=The parameter is incorrect.
HResult=-2147024809
IsTransient=false
Message=The parameter is incorrect.
Source=mscorlib
StackTrace:
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.Utils._ImportKey(SafeProvHandle hCSP, Int32 keyNumber, CspProviderFlags flags, Object cspObject, SafeKeyHandle& hKey)
at System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters)
at RsaBug.Program.Main() in d:\sandbox15\RsaBug\RsaBug\Program.cs:line 24
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
根据目前的字母表,模数和指数很可能是 base-36 编码的。
我有一些代码在 Windows 7 上运行良好,但现在我已经开始使用 Windows 8.1 开发箱(请参阅下面的重现代码)后失败了。
Windows 8.1 与 Windows 7 上的 RSA 是否不同?
using System.Security.Cryptography;
using System.Text;
namespace RsaBug
{
class Program
{
static void Main()
{
var modulus = Encoding.UTF8.GetBytes("rvco6d27bsw2fw5qx7okdcu5jahd1ifh22is76k5xyau3wjv7plo0rom66h2434tvm29cmq2ov6mbjo30bymb14j2dst5fzy7pd");
var exponent = Encoding.UTF8.GetBytes("1ekh");
using (var rsa = new RSACryptoServiceProvider())
{
//Get an instance of RSAParameters from ExportParameters function.
var rsaKeyInfo = rsa.ExportParameters(false);
//Set RSAKeyInfo to the public key values.
rsaKeyInfo.Modulus = modulus;
rsaKeyInfo.Exponent = exponent;
//Import key parameters into RSA.
rsa.ImportParameters(rsaKeyInfo); // on Windows 8.1, this throws
}
}
}
}
这是我得到的异常:
System.Security.Cryptography.CryptographicException was unhandled
_HResult=-2147024809
_message=The parameter is incorrect.
HResult=-2147024809
IsTransient=false
Message=The parameter is incorrect.
Source=mscorlib
StackTrace:
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.Utils._ImportKey(SafeProvHandle hCSP, Int32 keyNumber, CspProviderFlags flags, Object cspObject, SafeKeyHandle& hKey)
at System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters)
at RsaBug.Program.Main() in d:\sandbox15\RsaBug\RsaBug\Program.cs:line 24
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
根据目前的字母表,模数和指数很可能是 base-36 编码的。