Data Protection
·IdenticAPI

How to Detect Private Keys and Tokens in Text

Identify private key blocks, bearer tokens, and cloud API key formats in text before they reach logs, models, or third-party services.

Detecting private keys and tokens in text means scanning unstructured strings for PEM-encoded key blocks, high-entropy API key prefixes, bearer authorization headers, and vendor-specific token formats before that text reaches logs, LLM providers, vector indexes, or support dashboards. These patterns are among the highest-severity findings in LLM pipelines because a single leaked private key or production token can grant immediate infrastructure access—and users paste them into chat interfaces with surprising frequency when debugging deployments or sharing curl examples.

High-risk patterns to scan

TypeSynthetic exampleAPI category
PEM private key-----BEGIN PRIVATE KEY----------END PRIVATE KEY-----private_key
OpenSSH private key-----BEGIN OPENSSH PRIVATE KEY-----private_key
Stripe-style keysk-test_abcdefghijklmnopqrstuvwxyz123456stripe_key
AWS access key IDAKIA0000000000000000aws_access_key
GitHub PATghp_abcdefghijklmnopqrstuvwxyz1234567890ABgithub_token
Slack tokenxoxb-000000000000-0000000000000-SyntheticSlackTokenslack_token
Google API keyAIzaSySyntheticExampleKey1234567890ABCgoogle_api_key
OpenAI-style keysk-abcdefghijklmnopqrstuvwxT3BlbkFJabcdefghijklmnopqrstopenai_key
Bearer JWT-likeBearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.testbearer_token
IdenticAPI keyidapi_test_synthetickey1234567890abcdefapi_key

All secret categories map to verdict: "unsafe" and risk: "high" in IdenticAPI responses.

Overview: Secrets Detection for LLM Applications. API keys in prompts: How to Prevent API Keys from Leaking into AI Prompts.

API scanning workflow

Endpoint: POST /api/v1/security/pii-secrets

Example — private key block (synthetic):

{
  "text": "Here is the key:\n-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC7SyntheticExample\n-----END PRIVATE KEY-----",
  "redact": true
}

Response excerpt:

{
  "verdict": "unsafe",
  "risk": "high",
  "findings": [
    {
      "category": "private_key",
      "reason": "Detected private key (secret)",
      "confidence": 0.99
    }
  ],
  "redacted_text": "Here is the key:\n[PRIVATE_KEY]"
}

Example — mixed token and email:

{
  "text": "Use ghp_abcdefghijklmnopqrstuvwxyz1234567890AB and email user@example.com",
  "redact": false
}

Returns both github_token (secret) and email (PII); verdict remains unsafe.

Product page: PII & Secrets Detection. Docs: PII & Secrets Detection documentation. Try strings in PII Checker.

Private keys: special handling

Private keys differ from API tokens in rotation and blast radius:

  • Rotation — Revoke certificate or key pair; reissue credentials across all dependents
  • Scope — May grant TLS impersonation, SSH access, or code signing
  • Format — Multi-line PEM blocks; detectors must match header/footer markers

Policy: Block LLM forwarding on private_key findings. Redaction replaces the block in logs but does not undo exposure if raw text already transmitted—rotate immediately.

Never commit real keys to articles, tests checked into public repos, or LLM prompts used for content generation. Use synthetic PEM bodies clearly labeled as examples.

Bearer tokens and JWTs

Bearer headers often appear in pasted curl commands:

curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJleGFtcGxlIn0.test"

Detectors match Bearer + high-entropy suffix. JWTs are three base64url segments separated by dots—still treat as secrets even if payload is decodable, because the signature secret or session may remain valid.

Do not decode user JWTs in production scanners and log payloads—that creates additional PII exposure.

Token detection vs validation

Detection answers: "Does this string look like a GitHub PAT?" Validation answers: "Does GitHub accept this PAT right now?" Production LLM guards should detect and block, not call vendor APIs to verify keys—that risks leaking the secret to a third party and creates rate-limit abuse.

If you operate internal token formats, add custom pre-LLM rules or extend allowlists for known test fixtures.

Where to scan in LLM architectures

  1. User chat input — Primary gate
  2. File upload text extraction.pem, .key, .env files
  3. RAG retrieved chunks — Internal repos indexed without secret scanning
  4. Tool outputs — Search tools returning public gists
  5. Model completions — Models echoing keys from context (output scan)

Align with Building a Privacy Filter Before Your LLM API Call and PII and Secrets Leakage Checklist.

Response playbook for unsafe verdicts

StepAction
1Block upstream LLM/provider call
2Log request_id, category, timestamp
3Notify user with non-echoing message
4Open rotation ticket for affected credential type
5Add synthetic regression test mirroring pattern

For credential pairs (password=, api_key=), see How to Detect Credentials in User Input.

Multi-line PEM handling in application code

Private keys span multiple lines. When building your own pre-scan logic—or debugging API results—remember:

  • Do not truncate at the first newline before calling the scanner
  • File upload pipelines must extract full text from .pem and .key files before scanning
  • Some chat UIs collapse whitespace; preserve original newlines in the server payload

IdenticAPI matches PEM header markers such as -----BEGIN PRIVATE KEY----- within the full text field. Send the complete block in one request rather than line-by-line scans that lose context.

Limitations

  • Custom internal token formats may not match vendor regex libraries
  • Base64-encoded secrets inside JSON may evade until decoded
  • Short tokens below entropy thresholds may be missed
  • Example keys in documentation cause false positives—use dedicated test prefixes
  • Detection does not assess key permissions (read-only vs admin)
  • Multi-part secrets split across consecutive user messages may evade single-request scans

Obfuscation and split-across-messages attacks require behavioral monitoring beyond pattern matching. Combine token detection with rate limits, session anomaly alerts, and employee training on never pasting live credentials into AI chat tools.

Frequently asked questions

How are private keys detected in text?

Scanners match PEM header markers such as -----BEGIN PRIVATE KEY----- and related RSA, EC, or OpenSSH private key blocks within the full text payload.

What verdict do private keys and tokens produce?

Secret categories including private_key, github_token, bearer_token, and vendor API keys produce an unsafe verdict with high risk in IdenticAPI responses.

Should I validate tokens by calling vendor APIs?

No for production LLM guards. Live validation can leak secrets to third parties. Detect, block, and rotate per your incident runbook instead.

Do I need to send multi-line PEM blocks in one request?

Yes. Send the complete key block in a single text field. Line-by-line scanning loses PEM context and may miss private keys in uploaded files.

Related reading