title: How to check if a file is pkcs#8 DER format in Java?
body: <p>I need to check if a file is pkcs#8 DER format in Java when uploading the file, I think maybe PKCS8EncodedKeySpec and getFormat() can be used.</p>
  <pre><code>class FileFormatePkcs8{
      public static void main(String[] args) {
      String filename = args[0];
          try {
              File f = new File(filename);
              byte[] encodedKey = new byte[(int)f.length()];
              PKCS8EncodedKeySpec pkcs8Key = new PKCS8EncodedKeySpec(encodedKey);
              if(pkcs8Key.getFormat().equals(&quot;PKCS#8&quot;)) {
                  System.out.println(&quot;It's pkcs8.&quot;);
              }
              else {
                  System.out.println(&quot;It's not pkcs8.&quot;);
              }
          }
          catch (Exception ex) {
              System.out.println(&quot;exception:&quot;+ex.getMessage());
          }
      }
  
  }
  </code></pre>
  <p>All the files input will get the &quot;It's pkcs8.&quot; result. I realize &quot;PKCS8EncodedKeySpec&quot; will create the pkcs#8 key, but I don't know using which class to replace it.</p>
  <p>Note that: both of PKCS#8 and DER need to be check, so I think org.bouncycastle.openssl.PEMParser can be ignored. Or am I on the wrong track?</p>