0
我有两台服务器的Windows 2008 R2,服务器1个&服务器2.我产生的窗口下,服务器1的RSA密钥对不起作用解密使用占到1:采用进口关键从一台服务器另一台服务器
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.服务器2下的Windows帐户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相同的容器,如果我尝试解密服务器2帐户下2服务器1的加密字符串,我得到:
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:谢谢你的建议。不幸的是,它没有造成任何影响。 – costa