【问题标题】:Decrypting using a imported key from one server doesn't work on another server使用从一台服务器导入的密钥解密在另一台服务器上不起作用
【发布时间】:2016-11-24 23:30:36
【问题描述】:

我有两台服务器 Windows 2008 R2,服务器 1 和服务器 2。我在 Windows 帐户 1 下的服务器 1 上生成了 RSA 密钥对:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -pc "MyKeys" -exp
C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -px "MyKeys" keys.xml -pri

我将 keys.xml 复制到服务器 2。在 Windows 帐户 2 下的服务器 2 上,我运行了:

    C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -pi "MyKeys" keys.xml

我用下面的c#代码加解密:

private static RSACryptoServiceProvider CreateRsaCypher(string containerName)
{
  // Create the CspParameters object and set the key container 
  // name used to store the RSA key pair.
  CspParameters cp = new CspParameters();
  cp.KeyContainerName = containerName;

  // Create a new instance of RSACryptoServiceProvider that accesses
  // the key container MyKeyContainerName.
  return new RSACryptoServiceProvider(cp);
}

public static string AsymmetricEncrypt(byte[] data, string containerName)
{
  RSACryptoServiceProvider cipher = CreateRsaCypher(containerName);
  byte[] cipherText = cipher.Encrypt(data, false);
  return Convert.ToBase64String(cipherText);
}

public static string AsymmetricEncrypt(string str, string containerName )
{
  return AsymmetricEncrypt(Encoding.UTF8.GetBytes(str), containerName);
}

public static byte[] AsymmetricDecrypt(string data, string containerName)
{
  RSACryptoServiceProvider cipher = CreateRsaCypher(containerName);
  return cipher.Decrypt(Convert.FromBase64String(data), false);
}

public static string AsymmetricDecryptToString(string data, string containerName)
{
  return Encoding.UTF8.GetString(AsymmetricDecrypt(data, containerName));
}

现在,当我使用帐户 1 下的同一容器在服务器 1 上加密字符串时,如果我尝试在帐户 2 下的服务器 2 上解密它,我会得到:

Unhandled Exception: System.Security.Cryptography.CryptographicException: Bad Data.

   at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
   at System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle pKeyContext, Byte[] pbEncryptedKey, Int32 cbEncryptedKey, Boolean fOAEP, ObjectHandleOnStack ohRetDecryptedKey)
   at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP)

我用来加密/解密的应用程序是一个 C# 控制台应用程序。

我注意到服务器 1 和服务器 2 没有相同版本的 aspnet_regiis.exe,一个是:

Microsoft (R) ASP.NET RegIIS version 4.0.30319.0

另一个是:

Microsoft (R) ASP.NET RegIIS version 4.0.30319.18408

我期望我可以使用服务器 2 上的相同密钥(即在服务器 1 上导出并在服务器 2 上导入的密钥对)解密在服务器 1 上加密的文本是否不正确? windows 是否会在导入时以某种方式改变公钥/私钥?

提前谢谢你!

只是一个更新。基于其他测试,我注意到在不同的 Windows 帐户下使用相同的 RSA 容器加密相同的字符串会导致不同的加密字符串,这在某种程度上是有意义的。这解释了我看到的行为。

【问题讨论】:

  • 如果您在机器 2 上将其导入为可导出,会有所不同吗? aspnet_regiis.exe -pi "MyKeys" keys.xml -exp
  • @MathiasR.Jessen:感谢您的建议。不幸的是,这并没有什么不同..

标签: c# aspnet-regiis.exe


【解决方案1】:

好的,我让它在两个帐户之间保持一致,关键是设置 UseMachineKeyStore 标志。

private static RSACryptoServiceProvider CreateRsaCypher(string containerName = DefaultContainerName)
{
  // Create the CspParameters object and set the key container 
  // name used to store the RSA key pair.
  CspParameters cp = new CspParameters();
  cp.KeyContainerName = containerName;
  cp.Flags = CspProviderFlags.UseExistingKey | CspProviderFlags.UseMachineKeyStore;
  // Create a new instance of RSACryptoServiceProvider that accesses
  // the key container MyKeyContainerName.
  return new RSACryptoServiceProvider(cp);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 2020-04-14
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多