Hallo!

Hier mal eine Nuß von der ich glaube, dass sie schwer zu knacken sein wird. Ich möchte einen WebService schreiben, der eine RSA-Verschlüsselung nutzt. Lokal funktioniert folgender Code einwandfrei

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
[SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#008080]RSACryptoServiceProvider[/COLOR][/SIZE][SIZE=2] rsa;[/SIZE]
 
[SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] AssignParameter()[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff]cons t[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] PROVIDER_RSA_FULL = 1;[/SIZE]
[SIZE=2][COLOR=#0000ff]const [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2] CONTAINER_NAME = [/SIZE][SIZE=2][COLOR=#800000]"SpiderContainer"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#008080]CspParameters[/COLOR][/SIZE][SIZE=2] cspParams;[/SIZE]
[SIZE=2]cspParams = [/SIZE][SIZE=2][COLOR=#0000ff]new [/COLOR][/SIZE][SIZE=2][COLOR=#008080]CspParameters[/COLOR][/SIZE][SIZE=2](PROVIDER_RSA_FULL);[/SIZE]
[SIZE=2]cspParams.KeyContainerName = CONTAINER_NAME;[/SIZE]
[SIZE=2]cspParams.Flags = [/SIZE][SIZE=2][COLOR=#008080]CspProviderFlags[/COLOR][/SIZE][SIZE=2].UseMachineKeyStore;[/SIZE]
[SIZE=2]cspParams.ProviderName = [/SIZE][SIZE=2][COLOR=#800000]"Microsoft Strong Cryptographic Provider"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]rsa = [/SIZE][SIZE=2][COLOR=#0000ff]new [/COLOR][/SIZE][SIZE=2][COLOR=#008080]RSACryptoServiceProvider[/COLOR][/SIZE][SIZE=2](cspParams);[/SIZE]
[SIZE=2]}[/SIZE]
 
[SIZE=2][SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2] DecryptData([/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2] data2Decrypt)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]AssignParameter();[/SIZE]
[SIZE=2][COLOR=#0000ff]byte[/COLOR][/SIZE][SIZE=2][] getpassword = [/SIZE][SIZE=2][COLOR=#008080]Convert[/COLOR][/SIZE][SIZE=2].FromBase64String(data2Decrypt);[/SIZE]
[SIZE=2][COLOR=#008080]StreamReader[/COLOR][/SIZE][SIZE=2] reader = [/SIZE][SIZE=2][COLOR=#0000ff]new [/COLOR][/SIZE][SIZE=2][COLOR=#008080]StreamReader[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#800000]@"C:\Inetpub\wwwroot\privatekey.xml"[/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2] publicPrivateKeyXML = reader.ReadToEnd();[/SIZE]
[SIZE=2]rsa.FromXmlString(publicPrivateKeyXML);[/SIZE]
[SIZE=2]reader.Close();[/SIZE]
[SIZE=2][COLOR=#008000]//read ciphertext, decrypt it to plaintext[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]byte[/COLOR][/SIZE][SIZE=2][] plain = rsa.Decrypt(getpassword, [/SIZE][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][SIZE=2] System.Text.[/SIZE][SIZE=2][COLOR=#008080]Encoding[/COLOR][/SIZE][SIZE=2].UTF8.GetString(plain);[/SIZE]
[SIZE=2]}[/SIZE]
[/SIZE]

Doch wenn nun mein WebService an die Stelle "new RSACryptoServiceProvider(cspParams);" kommt, schmeißt er folgende Exception:

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
System.Security.Cryptography.CryptographicException: Das Objekt ist bereits vorhanden.
 
   at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr)
 
   at System.Security.Cryptography.Utils._CreateCSP(CspParameters param, Boolean randomKeyContainer, SafeProvHandle& hProv)
 
   at System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)
 
   at System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle)
 
   at System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair()
 
   at System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize, CspParameters parameters, Boolean useDefaultKeySize)
 
   at System.Security.Cryptography.RSACryptoServiceProvider..ctor(CspParameters parameters)
 
   at Service.AssignParameter() in c:\Inetpub\wwwroot\DummyWS\App_Code\Service.cs:line 36
 
   at Service.DecryptData(String data2Decrypt) in c:\Inetpub\wwwroot\DummyWS\App_Code\Service.cs:line 44
 
   at Service.identClient(String clearText, String cryptedText) in 
c:\Inetpub\wwwroot\DummyWS\App_Code\Service.cs:line 20

Hat vielleicht jemand eine Idee, was da schief läuft?

Gruß, Peter