2016-11-24 88 views
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容器對相同的字符串進行加密會導致不同的加密字符串,這在某種程度上是有意義的。這解釋了我看到的行爲。

+0

如果您將它導入機器2時導出它,它會有所作爲嗎? 'aspnet_regiis.exe -pi「MyKeys」keys.xml -exp' –

+0

@ MathiasR.Jessen:謝謝你的建議。不幸的是,它沒有造成任何影響。 – costa

回答

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); 
} 
相關問題