A friend of mine made a public/ private key pair using DSA/ Elgamal. I can import his public and private keys, no problem. I can encrypt text for him, no problem. I cannot, however, decrypt a message encrypted for him. I get the error described in the subject line. As far as I can tell, my error must occur either in the importing of the private key, or the way I am decrypting, but there is no exception thrown when importing the private key (and I get a valid PrivateKey object), and the decryption method works just fine on any RSA-based key decryption. Can anyone suggest what might be wrong here?
My private key collection methods: public static PgpPrivateKey ImportPrivateKey( this Stream privateKeyIn, string passPhrase) { return privateKeyIn.ImportSecretKey().ExtractPrivateKey(passPhrase.ToCharArray()); } public static PgpSecretKey ImportSecretKey( this Stream secretIn) { var secRings = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(secretIn)).GetKeyRings().OfType<PgpSecretKeyRing>(); var pgpSecretKeyRings = secRings as PgpSecretKeyRing[] ?? secRings.ToArray(); if (!pgpSecretKeyRings.Any()) throw new ArgumentException("No secret keys found."); var secKeys = pgpSecretKeyRings.SelectMany(x => x.GetSecretKeys().OfType<PgpSecretKey>()); var secKey = secKeys.FirstOrDefault(x => x.IsSigningKey); return secKey; } My decryption method: public static void PgpDecrypt( this Stream encryptedIn, Stream clearOut, PgpPrivateKey decryptionKey) { var stream = PgpUtilities.GetDecoderStream(encryptedIn); var layeredStreams = new List<Stream> { stream }; //this is to clean up/ dispose of any layered streams. var dataObjectFactory = new PgpObjectFactory(stream); var dataObject = dataObjectFactory.NextPgpObject(); while (!(dataObject is PgpLiteralData) && dataObject != null) { try { var compressedData = dataObject as PgpCompressedData; var listedData = dataObject as PgpEncryptedDataList; //strip away the compression stream if (compressedData != null) { stream = compressedData.GetDataStream(); layeredStreams.Add(stream); dataObjectFactory = new PgpObjectFactory(stream); } //strip the PgpEncryptedDataList if (listedData != null) { var encrytedDataList = listedData.GetEncryptedDataObjects() .OfType<PgpPublicKeyEncryptedData>().First(); stream = encrytedDataList.GetDataStream(decryptionKey); layeredStreams.Add(stream); dataObjectFactory = new PgpObjectFactory(stream); } dataObject = dataObjectFactory.NextPgpObject(); } catch (Exception ex) { //Log exception here. throw new PgpException("Failed to strip encapsulating streams.", ex); } } } The exception is thrown on the "stream = encrytedDataList.GetDataStream(decryptionKey);" line. Does anyone have a good idea what I am doing wrong? extraneous extension methods: public static Stream Streamify( this string theString, Encoding encoding = null) { encoding = encoding ?? Encoding.UTF8; var stream = new MemoryStream(encoding.GetBytes(theString)); return stream; } public static string Stringify( this Stream theStream, Encoding encoding = null) { encoding = encoding ?? Encoding.UTF8; if (theStream.CanSeek) theStream.Position = 0; using (var reader = new StreamReader(theStream, encoding)) { return reader.ReadToEnd(); } } My test method looks like this: #region keys protected const string PrivateKey = @" -----BEGIN PGP PRIVATE KEY BLOCK----- Version: GnuPG v2.0.21 (MingW32) lQN5BFJVKr0RCADbajE5QV373vGPVO+5rFmEA+aQfzicdXg7jgT2U4tXOqwECmwZ VzHUxpkD20Ax4k27wEsi9mRlppc+jqQCCJVF3EULZq9x3p0ASYxQMWGW+WVAOEPU qjfjHsSOTydBcrhR6g9tGvO+XXS9D3vzO4D0oExxxmwv3yvj3it7senk7pR/F99S /S54y5AWRCG61qjVO5yXc2P2Pvcm/wn7M4cU3rgBNk6e13EBJ8afURcewEDsLzgU 2HmRPEqY72ojrdAxNbiLPoee+pGzWY/7Bn1zpjhwapI7S+zSJ/mvXcmoCy5InndE D/j34oUkTAh8Nlc3jx5NcfzjWYfHtp5HPJfXAQDzSLvPrXcPVBR8bYYY5QO2CMjg CfgkX/mI5JrhFeQJYwf+JxmxbXwBotCEu2jf+efhjN698Nk3gjJ7Vz7qO6fhGzwV 9YUBldS5RH3qUAfFE27eviyuuF5comuIrfhPSDgoZDGCGC4NABLfelRuQFFVkPBf zZjqKy3FHuPN28XLbZRvuEPeP0Bp0CJ1KjXkYygA441ogKBvZanRlGDpiYNmqjwQ Cpz15QsvSzNsHkq7tno7q7Qqwqvyg+r5BR2l6vVfQRo7lPZAyWcUqlqWxP0goH7z /SvisECbxoEznG5505nXlZKJzSGdyiIl5Ung0wnjbFZ8tzG6aQ69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4wwnP+TTp0VXf66VmFlOpRVuC4Qyb0FRivl0dlIqxRrAAMFCADbMrAwJnEu kacBvDUa/6ml0b9GabDo6PAZkyPJpKSI1km3rGDnp3TGp3u9ITwDjJJNepfiTJmW 0pomtdOSnlejkvym9tH4v8yXlg9+PMEOiDdt9Ut/b2iH4aPpEk9Z+Jh9jRQk2lNb l7n5PQxFWjoXYgo0vuy1VYdtSHLWx+i+Fg4vjjnDKtfD1E+lh+iXHetxDLElu1NK Ci7Y6CIhsW4pHlsEQZVf4XpPnHMgBtl7Wy7CdmHMfa4fY+1kmPawj8X8FTujyIRV aD74eP2bjTqfo6/guyb7xpbBrxaDAgBfAgurbxjsYhX1pLddI/5fnG/z3GFfO5M4 RKtA3Ww4td5z/gMDAl0UXCjugKtVs1YQSP4+Am44PWoqEHPZwtmPzXHSs6i/F153 UOvGX8/w8jDL7gVua6Gq5vM74PwZ2snvq3bdpZCZEarfdRMJqHQbASTefgdC3I2I ZwQYEQgADwUCUlUqvQIbDAUJACjq8wAKCRBDs9wdt2HgQI/pAP928CcJc364+bq/ 5PJ89qnY1Ge50qGdJGjoa4vS2xLxegEAiISjmVNpxTLBjVyIJXyfJ0PxSseg7Qkt U1khuLx/6fM= =FI4o -----END PGP PRIVATE KEY BLOCK-----"; protected const string PublicKey = @" -----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v2.0.21 (MingW32) mQMuBFJVKr0RCADbajE5QV373vGPVO+5rFmEA+aQfzicdXg7jgT2U4tXOqwECmwZ VzHUxpkD20Ax4k27wEsi9mRlppc+jqQCCJVF3EULZq9x3p0ASYxQMWGW+WVAOEPU qjfjHsSOTydBcrhR6g9tGvO+XXS9D3vzO4D0oExxxmwv3yvj3it7senk7pR/F99S /S54y5AWRCG61qjVO5yXc2P2Pvcm/wn7M4cU3rgBNk6e13EBJ8afURcewEDsLzgU 2HmRPEqY72ojrdAxNbiLPoee+pGzWY/7Bn1zpjhwapI7S+zSJ/mvXcmoCy5InndE D/j34oUkTA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wNTqT1g2Sk0Ib4YShlymvHEPeb gxBLDbtyOjTSLpl6v1wVRiFdCMihNiuxVURxnNqsg8PK2ZwtLK4dtYmQiTYCb/yH eABnLfGiaWiZ8dsMv1kv+jGlKhyNH7udmBWDBCi1PBzV9cIjDye3g+6KYbsbsp/P 3DqRiPJbRU4EIP92VQ23i6lkaFSaov5JtGq3LvnE8nmnaEruF5WJSA4wN9PrMjeK G8hMVJ/Dbq/59j6axpnL4dYKmZeJBg1iW4wwnP+TTp0VXf66VmFlOpRVuC4Qyb0F Rivl0dlIqxRrAAMFCADbMrAwJnEukacBvDUa/6ml0b9GabDo6PAZkyPJpKSI1km3 rGDnp3TGp3u9ITwDjJJNepfiTJmW0pomtdOSnlejkvym9tH4v8yXlg9+PMEOiDdt 9Ut/b2iH4aPpEk9Z+Jh9jRQk2lNbl7n5PQxFWjoXYgo0vuy1VYdtSHLWx+i+Fg4v jjnDKtfD1E+lh+iXHetxDLElu1NKCi7Y6CIhsW4pHlsEQZVf4XpPnHMgBtl7Wy7C dmHMfa4fY+1kmPawj8X8FTujyIRVaD74eP2bjTqfo6/guyb7xpbBrxaDAgBfAgur bxjsYhX1pLddI/5fnG/z3GFfO5M4RKtA3Ww4td5ziGcEGBEIAA8FAlJVKr0CGwwF CQAo6vMACgkQQ7PcHbdh4ECP6QD8CXgPHY1VxGBXzYP+RmgsIw+/VBqPn+v5VV/u /cRdKlMA/iffjRe0aegPGA+IlCAYUiSGJocvKXQdhv1nO8OJP5MD =zGSA -----END PGP PUBLIC KEY BLOCK-----"; protected const string PrivateKeyPassword = @"test"; protected const string TestString = "The quick brown fox jumped over the lazy dog."; protected const string EncryptedString = @" -----BEGIN PGP MESSAGE----- Version: BCPG C# v1.7.4137.9688 owEBWAKn/YUCDgP327MGtwWFQhAH/3vBNSpySTLxVrthJzBrp8CjTLA6NCJnBRmk c0fVHBaXwQepe3AL70qmZwzPpilQkMVlbTVfrwdneeya2K9FMvyaDxgzvFmTyrsR 0HCS5NwHhNt5kK0IKicNLhcKsd47Qy9eZFKhrhjZAZN84ymsu16cMcg/XumqhxDm ptPJhq4ar+baO/N6lC+EtPVoP4Fapx5EXds/5Ulq0BHzvu4nu4gH0+QvqgnN8Cpx kkz0t3znjSHnRY5Q9pKLUr7MFltvV8c/IK+m9M5tIGF788BLj4fc8XkhtKwLxY3E Lpp3g2nXxW7qT2GzzyOpgXTEdCGoxeTy3yUT+UhqbkAKMAOhijUIAMBpha1maJbw vh2WHdu868s9vQ+xcvadDH9VZbOOI6t0NyZWRmVzTqIy4wrtP/TchqAl7vaKeGUD N/BOH10d+VEJYc7B+7nOogpCO8lldilXnWLhJsCeQyVwEELI4QhQFYUBfWvloqT3 Y2KyX0r9rXHcyOro7RlX3bK1UER3eE7vKI6KLTEtbz5yM770WRJI81U5/i/3gfX+ gnTwiloKMYaR6a0K6fwbOc9J3mMiovRREweY1a5Pf2skuEMwFUro90ptQUTEdvli T7G1ayEGBtmmq0vm6hN4ccgB+wQGoKQclKeSGhW21Z+FiLtRm2RDxatiunrcwfY7 zjRlR/ZK1/jJRblDUoXikYVnZdO1/UyssBMgG1dbm+KYKRkSp08sdA2U+BWE3Bwx CfQHm6mXtuzcliLqF9B3tsnF506uU5utn6mvrIj+TA== =2/lG -----END PGP MESSAGE-----"; #endregion [Test] public void CanEncryptUsingDSAElgamalAlgorithm() //This works fine { var publicKey = OpenPgpUtility.ImportPublicKey(PublicKey); string encryptedText; using (var toEncrypt = TestString.Streamify()) using (var encrypted = new MemoryStream()) { toEncrypt.PgpEncrypt(encrypted, publicKey); encryptedText = encrypted.Stringify(); } Assert.IsNotNullOrEmpty(encryptedText); Console.WriteLine("{0}: \r\n{1}", "CanEncryptUsingDSAElgamalAlgorithm", encryptedText); } [Test] public void CanDecryptUsingDSAElgamalAlgorithm() //This fails { var privateKey = OpenPgpUtility.ImportPrivateKey(PrivateKey, PrivateKeyPassword); string decryptedText; using (var toDecrypt = EncryptedString.Streamify()) using (var decrypted = new MemoryStream()) { toDecrypt.PgpDecrypt(decrypted, privateKey); decryptedText = decrypted.Stringify(); } Assert.IsNotNullOrEmpty(decryptedText); Assert.AreEqual(decryptedText, TestString); Console.WriteLine("{0}: \r\n{1}", "CanDecryptUsingDSAElgamalAlgorithm", decryptedText); } -- This message may contain confidential and/or privileged information. If you are not the intended recipient or authorized to receive this for the intended recipient, you must not use, copy, disclose or take any action based on this message or any information herein. If you have received this message in error, please advise the sender immediately by sending a reply e-mail and delete this message. Thank you for your cooperation.