Search code examples
c#dllvisual-studio-2015bouncycastle

The type 'PgpPublicKey' exists in both 'BouncyCastle.Crypto, Version=1.9.0.0...' and thirdparty dll


Today I need to do some Open PGP encryption/decryption in My web application so I using nuget to add BouncyCastle to my solution, and add some sample code from stackoverflow (SOF) to test encrypt a file.

And the error appear:

Error CS0433 The type 'PgpPublicKey' exists in both 'BouncyCastle.Crypto, Version=1.9.0.0, Culture=neutral, PublicKeyToken=0e99375e54769942' and 'iTextV***, Version=5.6.140.0, Culture=neutral, PublicKeyToken=null'

So How to fix that?

After searching in SOF, I found some solution, but cannot fix it yet:

  1. Change all dll to reference the same version 1.9.0.0 of BouncyCastle.Crypto. But unluckily, we use 2 third-party dll iTextV***.dll and itext.v***.dll of same company, but we don't have code for them to change reference and rebuild dll.

  2. One post in SOF told about asembly "Aliases", but we use Web application and have no project reference dll like that to change alias. I tried adding this in web.config but not working, too:

<dependentAssembly>
  <assemblyIdentity name="BouncyCastle.Crypto" publicKeyToken="0e99375e54769942" culture="neutral"/>
  <bindingRedirect newVersion="1.9.0.0"/>

</ dependentAssembly>

enter image description here After


Solution

  • After extreme thinking or Whatever we could call, I found some possible solution (not best, but acceptable IMO (IN MY OPINION)) in case you couldn't fix it by other solutions found in the internet:

    1. Create another c# application project on my own call B, reference to BouncyCastle library so that no conflict could happen. Then write functions I want and then build project B to dll and add that B dll to A - my main project/ web application as an independence third-party library. So A only call functions in B, no need to call BouncyCastle, no more version conflict.

    2. As BouncyCastle is open-source project, We could go to their site, download code and then manually add (copy/ paste) necessary classes Cs to our main project/ web application A, so that all Cs become classes in our project. We use Cs instead of using Org.BouncyCastle.*. No more version conflict. P/S credit to BouncyCastle team.