Windows 8.1上的CryptographicException(参数不正确),但不是Windows 7

时间:2015-02-26 22:42:40

标签: c# cryptography windows-8.1 rsa

我有一些代码在Windows 7上运行正常,但现在我已经开始使用Windows 8.1开发框了(见下面的repro代码)。

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:\sandbox\2015\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: 

1 个答案:

答案 0 :(得分:2)

根据现有的字母表,模数和指数很可能是基数为36的。