title: "The specified network password is not correct" exception in X509Certificate2 constructor
body: <p>I have a console application which loads an X509 Certificate from a byte array as follows:</p>
  
  <pre><code>var cert = new X509Certificate2(certificateContent,      // byte[]
                                  password,                // string
                                  X509KeyStorageFlags.PersistKeySet);
  </code></pre>
  
  <p><code>certificateContent</code> is a <code>byte[]</code> representing the contents of a pfx file.  This code works fine for a number of certificates I've tested.  There is one certificate I'm testing, though, that causes this line to throw a <code>CryptographicException</code> with the message "The specified network password is not correct.", even though the password provided is correct.</p>
  
  <p>The weird part is that I can use the same code in LinqPad to create a certificate from the same pfx file with the same password, and it works okay.</p>
  
  <p>I've checked the call site in the console application in the debugger, and verified that the correct values are being passed in.</p>
  
  <p>What could cause this constructor to throw this exception in a console app, but not in LinqPad using the same data, and work fine in both places for other certificates?</p>
  
  <p><strong>More Details</strong></p>
  
  <p>The certificates are stored in a database in Base64.  The Console app reads the certificate from the DB, converts it from Base64 to a byte[], and then tries to create the <code>X509Certificate2</code> object as above.</p>
  
  <p>There are three certificates I've been testing with:</p>
  
  <ol>
  <li>My personal Client Authentication certificate provided by my employer's CA.</li>
  <li>A test certificate created by a colleague using his own self-signed CA.</li>
  <li>My own test certificate created by myself using a self-signed CA.</li>
  </ol>
  
  <p>Certificates 1 and 2 work as expected in both the console app and LinqPad.</p>
  
  <p>Certificate 3 loads fine in LinqPad, but generates the error above if I try to use it in the console app.</p>
  
  <p>There are two significant differences between certs 2 &amp; 3.</p>
  
  <ol>
  <li>Cert2 expires in 2016 and Cert3 expires in 2039</li>
  <li>The private key associated with cert2 is 2048 bit.  Cert3 is 1024 bits.</li>
  </ol>
  
  <p>Could either of these differences result in the "specified network password is not correct" error?  And why would all 3 certs work fine in LinqPad, but only 1 throw the error in the Console app?</p>