Расшифровать зашифрованный ключ с помощью сертификата, который не работает в .NET, но работает в Openssql - PullRequest
0 голосов
/ 03 мая 2018

Итак, в течение нескольких недель я пытался выяснить, почему я не могу расшифровать зашифрованный ключевой элемент из мыльного ответа. Я выбрал другой путь, чтобы посмотреть, смогу ли я расшифровать зашифрованный ключевой элемент другими способами. В этом случае я использовал openssl и смог заставить его работать.

"C:\Program Files (x86)\GnuWin32\bin\openssl" enc -base64 -d -in encryptedKeyElement.txt > result.txt
"C:\Program Files (x86)\GnuWin32\bin\openssl" rsautl -decrypt -inkey myPrivateKey.pem -in encryptedKeyElement.txt -oaep -out decryptedkey.txt

С помощью расшифрованного ключа я смог расшифровать элемент мыльного тела.

Теперь вопрос в том, что это не работает в .NET?

var encryptedKey = "AnQtlNmAapLaX862hobD/p/RaPStJ+ygqrJMN9LM6i1548auXJ5wk0SVeQTdiOVMe3wv9lhkXKAZD6u6rOUCa3es+xyHrdRBplg0mJBfkprz84AWcI36TsoOhmntLZF9Yr+ZMyYzOCfzK7T0b1rTYh2X2Wjbg8xu/KGuNic9HmF9l5fk4y2mQeM8C3ST+HACPDSrvhodAmucj9X0sWPA/v0Y116mevpWvUsekc6iMuAhfBfP2UaVO9HZofvk3WDP0LdBWOdOoifop5Sbz+QT3dt0u2u96Oi3wRLagXv56sHVvHsiNGuHyEhzRKF8rAhnjh4VQSruuDrM/PfaH4OgHg=="
var clientCert = new X509Certificate2(@"C:\eSar\certificates\xxxxxx.pfx", "xxxxx");
var rsa = (RSACryptoServiceProvider)clientCert.PrivateKey;
byte[] key = rsa.Decrypt(Convert.FromBase64String(encryptedKey), false);

Ошибка, которую я получаю:

Параметр неверен. { EDITED . Я поставил неправильное сообщение об ошибке. Извините]

Трассировка стека

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)
   at Microsoft.Austria.WcfHelpers.SoapWithAttachments.SwaEncoder.ReadMessage(Stream stream, Int32 maxSizeOfHeaders, String contentType) in C:\Crap\Stuff\CARES\Microsoft.Austria.WcfHelpers.SoapWithAttachments\SwaEncoder.cs:line 503
   at Microsoft.Austria.WcfHelpers.SoapWithAttachments.SwaEncoder.ReadMessage(Stream stream, Int32 maxSizeOfHeaders, String contentType) in C:\Crap\Stuff\CARES\Microsoft.Austria.WcfHelpers.SoapWithAttachments\SwaEncoder.cs:line 458
   at Microsoft.Austria.WcfHelpers.SoapWithAttachments.SwaEncoder.ReadMessage(ArraySegment`1 buffer, BufferManager bufferManager, String contentType) in C:\Crap\Stuff\CARES\Microsoft.Austria.WcfHelpers.SoapWithAttachments\SwaEncoder.cs:line 126
   at System.ServiceModel.Channels.HttpInput.DecodeBufferedMessage(ArraySegment`1 buffer, Stream inputStream)
   at System.ServiceModel.Channels.HttpInput.ReadBufferedMessage(Stream inputStream)
   at System.ServiceModel.Channels.HttpInput.ParseIncomingMessage(HttpRequestMessage httpRequestMessage, Exception& requestException)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.SecurityChannelFactory`1.SecurityRequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
...