@Nullable
  public Key readKey(BufferedReader reader) throws IOException {
    String line = reader.readLine();
    while (line != null && !line.startsWith(BEGIN)) {
      line = reader.readLine();
    }
    if (line == null) {
      return null;
    }

    line = line.trim().substring(BEGIN.length());
    int index = line.indexOf(MARKER);
    if (index < 0) {
      return null;
    }
    String type = line.substring(0, index);
    String endMarker = END + type + MARKER;
    StringBuilder base64key = new StringBuilder();

    while ((line = reader.readLine()) != null) {
      if (line.indexOf(":") > 0) {
        // header, ignore
        continue;
      }
      if (line.contains(endMarker)) {
        break;
      }
      base64key.append(line);
    }
    try {
      byte[] key = Base64.decode(base64key.toString(), Base64.DEFAULT);
      if (type.contains(PUBLIC_KEY)) {
        return getPublicKey(key);
      } else if (type.contains(PRIVATE_KEY)) {
        return getPrivateKey(key);
      }
    } catch (GeneralSecurityException | IllegalArgumentException ex) {
      return null;
    }
    return null;
  }
Reads a single key from {@code reader}.

@return a {@link Key} or null if the reader doesn't contain a valid PEM.
java_src/src/main/java/com/google/crypto/tink/PemKeyType.java