code: private static byte[] decodePem(File pemFile) throws IOException {
          BufferedReader reader = new BufferedReader(new FileReader(pemFile));
          try {
              String line;
              while ((line = reader.readLine()) != null) {
                  if (line.contains("-----BEGIN ")) {
                      return readBytes(pemFile, reader, line.trim().replace("BEGIN", "END"));
                  }
              }
              throw new IOException("PEM " + pemFile + " is invalid: no begin marker");
          } finally {
              reader.close();
          }
      }
docs: All credits to belong to them.