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
| Type | Synthetic example | API category |
|---|---|---|
| PEM private key | -----BEGIN PRIVATE KEY----- … -----END PRIVATE KEY----- | private_key |
| OpenSSH private key | -----BEGIN OPENSSH PRIVATE KEY----- | private_key |
| Stripe-style key | sk-test_abcdefghijklmnopqrstuvwxyz123456 | stripe_key |
| AWS access key ID | AKIA0000000000000000 | aws_access_key |
| GitHub PAT | ghp_abcdefghijklmnopqrstuvwxyz1234567890AB | github_token |
| Slack token | xoxb-000000000000-0000000000000-SyntheticSlackToken | slack_token |
| Google API key | AIzaSySyntheticExampleKey1234567890ABC | google_api_key |
| OpenAI-style key | sk-abcdefghijklmnopqrstuvwxT3BlbkFJabcdefghijklmnopqrst | openai_key |
| Bearer JWT-like | Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test | bearer_token |
| IdenticAPI key | idapi_test_synthetickey1234567890abcdef | api_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
- User chat input — Primary gate
- File upload text extraction —
.pem,.key,.envfiles - RAG retrieved chunks — Internal repos indexed without secret scanning
- Tool outputs — Search tools returning public gists
- 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
| Step | Action |
|---|---|
| 1 | Block upstream LLM/provider call |
| 2 | Log request_id, category, timestamp |
| 3 | Notify user with non-echoing message |
| 4 | Open rotation ticket for affected credential type |
| 5 | Add 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
.pemand.keyfiles 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.
Related reading
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
- Secrets Detection for LLM Applications
Detect private keys, bearer tokens, cloud credentials, and high-entropy secrets in LLM inputs and outputs before they ca…
- How to Prevent API Keys from Leaking into AI Prompts
API keys in prompts, retrieved documents, and chat history are a common leakage path. Learn detection, redaction, and ar…
- What Is PII Detection?
PII detection identifies personally identifiable information in text — emails, phone numbers, government IDs, and more. …
- How to Detect PII in Text with an API
Use a PII detection API to scan user input, logs, and LLM context. Request format, response fields, verdict semantics, a…
- How to Redact PII Before Sending Data to an LLM
Redact or mask sensitive data before it reaches an LLM. Learn preprocessing patterns, placeholder strategies, and when t…