tag:blogger.com,1999:blog-3582699899544832081.post2433320193628723138..comments2022-06-27T12:58:54.240+00:00Comments on Raphael's Blog: PGP Decryption with C#Unknownnoreply@blogger.comBlogger15125tag:blogger.com,1999:blog-3582699899544832081.post-34428234245555102312015-04-21T12:22:04.053+00:002015-04-21T12:22:04.053+00:00Excellent post, Raphael. I had been looking out f...Excellent post, Raphael. I had been looking out for this all over the web. Each and every link i stumbled upon took me to FileEncryption using BouncyCastle. The only link which talked about FileDecryption was yours. You are a saviour.<br /><br />Thanks a lot.chootihttps://www.blogger.com/profile/02876142977446532320noreply@blogger.comtag:blogger.com,1999:blog-3582699899544832081.post-28427683856098290792014-02-17T11:08:58.162+00:002014-02-17T11:08:58.162+00:00This is great post,but i want to know Bouncy Castl...This is great post,but i want to know Bouncy Castle is freeware library or any charges.Please Help me Anonymoushttps://www.blogger.com/profile/17783028871498518362noreply@blogger.comtag:blogger.com,1999:blog-3582699899544832081.post-76777518665021832152012-07-07T20:24:03.196+00:002012-07-07T20:24:03.196+00:00This was a great post. Thanks. I have taken the li...This was a great post. Thanks. I have taken the liberty (as a poster above asked for it) and modified the decrypt method so that if message is not compressed or zipped then it will still decrypt. Again thanks for this post.<br /><br />public void decrypt(Stream input, string outputpath){<br /> input = PgpUtilities.GetDecoderStream(input);<br /> try {<br /> PgpObjectFactory pgpObjFactory = new PgpObjectFactory(input);<br /> PgpEncryptedDataList pgpEncryptedDataList;<br /> PgpObject pgpObj = pgpObjFactory.NextPgpObject();<br /> if (pgpObj is PgpEncryptedDataList){<br /> pgpEncryptedDataList = (PgpEncryptedDataList)pgpObj;<br /> }else {<br /> pgpEncryptedDataList = (PgpEncryptedDataList)pgpObjFactory.NextPgpObject();<br /> }<br /> PgpPrivateKey privKey = pgpKeys.PGPPrivateKey;<br /> PgpPublicKeyEncryptedData pgpPublicKeyEncryptedData = null;<br /> foreach (PgpPublicKeyEncryptedData pked in pgpEncryptedDataList.GetEncryptedDataObjects()){<br /> if (privKey != null){<br /> pgpPublicKeyEncryptedData = pked;<br /> break;<br /> }<br /> }<br /> <br /> Stream clear = pgpPublicKeyEncryptedData.GetDataStream(privKey); <br /> PgpObjectFactory plainFact = new PgpObjectFactory(clear);<br /> PgpObject message = plainFact.NextPgpObject();<br /> Stream encDataInputStream = null;<br /><br /> if (message is PgpCompressedData){<br /> PgpCompressedData compData = (PgpCompressedData)message; <br /> encDataInputStream = compData.GetDataStream(); <br /> PgpObjectFactory pgpObjFactory2 = new PgpObjectFactory(encDataInputStream);<br /> message = pgpObjFactory2.NextPgpObject(); <br /> if (message is PgpOnePassSignatureList){<br /> message = pgpObjFactory2.NextPgpObject();<br /> }<br /> }else{<br /> message = plainFact.NextPgpObject();<br /> }<br /> <br /> PgpLiteralData pgpLiteralData = null;<br /> pgpLiteralData = (PgpLiteralData)message;<br /> Stream output = File.Create(outputpath + "\\" + pgpLiteralData.FileName);<br /> Stream unc = pgpLiteralData.GetInputStream();<br /> Streams.PipeAll(unc, output);<br /> output.Close();<br /> <br /> <br /> }catch (Exception e) {<br /> throw new Exception(e.Message);<br /> }<br /> }Khalid Alihttps://www.blogger.com/profile/08186301877201784896noreply@blogger.comtag:blogger.com,1999:blog-3582699899544832081.post-62249612994870699452011-08-27T02:01:42.744+00:002011-08-27T02:01:42.744+00:00Thanks for this article helped me finalize my gpg ...Thanks for this article helped me finalize my gpg project... do add a part to handle uncompressed gpg files as it seems to fail when it goes through that partPablo Hernandezhttps://www.blogger.com/profile/00811025918759805632noreply@blogger.comtag:blogger.com,1999:blog-3582699899544832081.post-69961653634038893952010-08-11T15:59:54.044+00:002010-08-11T15:59:54.044+00:00No worries, I am glad that people find my work hel...No worries, I am glad that people find my work helpful :)Raphaelhttps://www.blogger.com/profile/06536270919684640704noreply@blogger.comtag:blogger.com,1999:blog-3582699899544832081.post-2464942682080014152010-08-10T09:11:50.182+00:002010-08-10T09:11:50.182+00:00Thanks Thanks Thanks THanks THanks a ton for your ...Thanks Thanks Thanks THanks THanks a ton for your great help !!!!OotyMisthttps://www.blogger.com/profile/03443970523518789322noreply@blogger.comtag:blogger.com,1999:blog-3582699899544832081.post-12888812291788732312009-08-12T16:20:08.458+00:002009-08-12T16:20:08.458+00:00This is a great post. Is there a way to decrypt a ...This is a great post. Is there a way to decrypt a file encrypted with a DSA key, using BouncyCastle?<br>I will appreciate any directions on how to go about it.<br><br> I have come across this question on several forums while trying to figure it and hope my this can help some.Saurabhhttp://www.blogger.com/profile/15015735830124198822noreply@blogger.comtag:blogger.com,1999:blog-3582699899544832081.post-84967085565385860952009-08-20T21:49:44.912+00:002009-08-20T21:49:44.912+00:00Glad you liked it :). As far as I know, DSA is onl...Glad you liked it :). As far as I know, DSA is only used for signing and not for encryption.<br><br>I remember when I was working on this, I had to make the distinction between signing and encrypting data.<br><br>But, in saying that, if you have encrypted with DSA, then they encryption should be the same as the example above (if using keys). Note that the example to decrypt doesnt speciy an encryption algorythm.Raphaelnoreply@blogger.comtag:blogger.com,1999:blog-3582699899544832081.post-81158499678284881102009-08-21T20:11:17.902+00:002009-08-21T20:11:17.902+00:00Thanks for the reply. I expected the same but am ...Thanks for the reply. I expected the same but am stuck and will appreciate if you can share your opinion on the following observation:<br><br>The private key constructor accepts an asymmetric key in the PrivateKey implementation for C#.<br><br><b>Constructor:</b><br><i>public PgpPrivateKey (AsymmetricKeyParameter privateKey, long keyId);</i><br><br>My private key is Dsa and is symmetric – <i>{Org.BouncyCastle.Crypto.Parameters.DsaPrivateKeyParameters}</i><br><br>and upon the execution of the statement - <i>Stream clear = pbe.GetDataStream(privKey);</i> the run time exception is -<i>{"Unable to cast object of type 'Org.BouncyCastle.Crypto.Parameters.DsaPrivateKeyParameters' to type 'Org.BouncyCastle.Crypto.Parameters.ElGamalKeyParameters'."}</i><br><br>Should it default to ElGamal? Is there a way to specify it to use DsaKeyParameters?<br><br>Thanks again,<br>SaurabhSaurabhhttp://www.blogger.com/profile/15015735830124198822noreply@blogger.comtag:blogger.com,1999:blog-3582699899544832081.post-33090839464672060922009-09-02T11:15:59.002+00:002009-09-02T11:15:59.002+00:00I am trying to compile your code but it is giving ...I am trying to compile your code but it is giving error as it couldn't find the class "PGPKeys". Am I missing something?xbosshttp://www.blogger.com/profile/09363406609655702640noreply@blogger.comtag:blogger.com,1999:blog-3582699899544832081.post-78043000085330535802009-09-12T09:25:38.604+00:002009-09-12T09:25:38.604+00:00@Saurabh:I am not sure right now, but it could be ...@Saurabh:<br><br>I am not sure right now, but it could be that you have a mismatch with your keys.<br><br>DSA and El Gamal are two distinct things, one is an encryption method and the other is a signing method. This could be the problem you are facing, as I mentioned previously.<br><br>It is an intruiging problem from my point of view, but probably not from yours!Raphaelnoreply@blogger.comtag:blogger.com,1999:blog-3582699899544832081.post-85509108775494061392009-09-14T17:00:40.519+00:002009-09-14T17:00:40.519+00:00Raphael,You are absolutely correct (and I missed t...Raphael,<br><br>You are absolutely correct (and I missed the fact – it is not evident in PGP documentation -) PGP uses El Gamal for encryption and DSA for signing. I instantiated the keys for encrypting and signing the file and was trying to use the same (keys) object for decrypting the file, which led to the exception.<br><br>Again, cryptography can be quite perplexing and your blog is a great help to those who use this once in a blue moon. <br><br>Thanks againSaurabhhttp://www.blogger.com/profile/15015735830124198822noreply@blogger.comtag:blogger.com,1999:blog-3582699899544832081.post-10982520062108361212009-12-10T16:40:00.876+00:002009-12-10T16:40:00.876+00:00In your example call you list a public and secret ...In your example call you list a public and secret key file. I can see how to export the public key however what is secring.gpg and how can i export this. i'm using gnupg.<br><br>Thanks<br>SeanSean Rocknoreply@blogger.comtag:blogger.com,1999:blog-3582699899544832081.post-83046866402547847472009-12-10T16:48:48.132+00:002009-12-10T16:48:48.132+00:00Sean Rock, if you use the following it should expo...Sean Rock, if you use the following it should export the secret key for you:<br><br>gpg --export-secret-key -a > secret.gpg<br><br>Should do the trick for you.Raphaelnoreply@blogger.comtag:blogger.com,1999:blog-3582699899544832081.post-22766847700189963802009-12-11T08:36:02.230+00:002009-12-11T08:36:02.230+00:00cool thanks!cool thanks!Sean Rocknoreply@blogger.com