Note that there are some explanatory texts on larger screens.

plurals
  1. POBouncy castle error: Unable to cast object of type RsaPrivateCrtKeyParameters to type ElGamalKeyParameters
    text
    copied!<p>I am having difficulties with decryption of a GPG file using Bouncy Castle. I have the encrypted file and I have a private key and the password for the private key. I can successfully decrypt the file using the desktop software GPG4win Kleopatra so I have the correct private key and the gpg file is valid.</p> <p>However when our application reaches the line of code which attempts to decrypt the data with Bouncy Castle, I receive this error:</p> <pre><code>Unable to cast object of type 'Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters' to type 'Org.BouncyCastle.Crypto.Parameters.ElGamalKeyParameters'. </code></pre> <p>I am decrypting the same file using the same private key with Kleopatra so this has got to be something I can resolve by perhaps changing the private key file to the expected format or setting some options in Bouncy Castle.</p> <p>The private key file is a plain text file beginning with the lines:</p> <pre><code>-----BEGIN PGP PRIVATE KEY BLOCK----- Version: GnuPG v2.0.17 (MingW32) </code></pre> <p>Here is a flattened out version of the decryption code. Apologies if I have missed anything out:</p> <pre><code>PgpEncryptionKeys encryptionKeys = new PgpEncryptionKeys(publicKey, privateKey, passPhrase); Stream encryptedStream = new StreamReader(encryptedFileName).BaseStream; Stream encodedFile = PgpUtilities.GetDecoderStream(inputStream); PgpObjectFactory factory = new PgpObjectFactory(encodedFile); PgpObject pgpObject = factory.NextPgpObject(); PgpEncryptedDataList encryptedDataList; if (pgpObject is PgpEncryptedDataList) { encryptedDataList = (PgpEncryptedDataList)pgpObject; } else { encryptedDataList = (PgpEncryptedDataList)factory.NextPgpObject(); } PgpPublicKeyEncryptedData myEncryptedData = null; PgpPublicKeyEncryptedData publicKeyED = null; foreach (PgpPublicKeyEncryptedData encryptedData in encryptedDataList.GetEncryptedDataObjects()) { if (encryptedData != null) { myEncryptedData = encryptedData; break; } } Stream clearStream = myEncryptedData.GetDataStream(privateKey); PgpObjectFactory clearFactory = new PgpObjectFactory(clearStream); PgpObject message = clearFactory.NextPgpObject(); if (message is PgpCompressedData) { message = ProcessCompressedMessage(message); PgpLiteralData literalData = (PgpLiteralData)message; using (Stream outputFile = File.Create(outputFilePath)) { using (Stream literalDataStream = literalData.GetInputStream()) { Streams.PipeAll(literalDataStream, outputFile); } } } </code></pre> <p>The exception occurs on this line:</p> <pre><code>Stream clearStream = myEncryptedData.GetDataStream(privateKey); </code></pre> <p>I hope you can suggest something for me to try. I can provide any further details I might have missed.</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload