0

我有两台服务器 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 容器加密相同的字符串会导致不同的加密字符串,这在某种程度上是有意义的。这解释了我看到的行为。

4

1 回答 1

0

好的,我让它在两个帐户之间始终如一地工作,关键是设置 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);
}
于 2016-11-25T18:22:02.717 回答