Skip to content

Instantly share code, notes, and snippets.

@OlivierKobialka
Created December 25, 2023 12:44
Show Gist options
  • Save OlivierKobialka/9d3c6f90c22ce4b9629c31807b29bb7d to your computer and use it in GitHub Desktop.
Save OlivierKobialka/9d3c6f90c22ce4b9629c31807b29bb7d to your computer and use it in GitHub Desktop.
Private and Public Key Regex

Match Private and Public Keys using Regular Expressions

  1. Ethereum (ETH)
  2. Bitcoin (BTC)
  3. Monero (XMR)
  4. PuTTY SSH RSA Key
  5. PuTTY SSH DSA Key
  6. ECDSA Private Key
  7. SSL Certificate
  8. John the Ripper
  9. Password etc passwd
  10. Password etc shadow
  11. JWK
  12. PGP Private
  13. SSH DDS Public
  14. SSH RSA Public
  15. PKCS7 Encrypted Data
  16. PGP Header
  17. PGP Public

Private Keys 🔐

Ethereum (ETH)

/(^|\b)(0x)?[0-9a-fA-F]{64}(\b|$)/

Bitcoin (BTC)

/^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$/g

Monero (XMR)

/4[0-9AB][1-9A-HJ-NP-Za-km-z]{93}$/g

PuTTY SSH RSA Key

/^PuTTY-User-Key-File-2: ssh-rsa\s*Encryption: none(?:.|\s?)*?Private-MAC:$/

PuTTY SSH DSA Key

/^PuTTY-User-Key-File-2: ssh-dss\s*Encryption: none(?:.|\s?)*?Private-MAC:$/

ECDSA Private Key

/^-----BEGIN ECDSA PRIVATE KEY-----\s.*,ENCRYPTED(?:.|\s)+?-----END ECDSA PRIVATE KEY-----$/

SSL Certificate

/^-----BEGIN CERTIFICATE-----(?:.|\n)+?\s-----END CERTIFICATE-----$/

John the Ripper

/^[J,j]ohn\ [T,t]he\ [R,r]ipper|john-[1-9].[1-9].[1-9]|Many\ salts:|Only\ one\ salt:|openwall.com/john/|List.External:[0-9a-zA-Z]*|Loaded\ [0-9]*\ password hash|guesses:\ \d*\ \ time:\ \d*:\d{2}:\d{2}:\d{2}|john\.pot$/

Password etc passwd

/^[a-zA-Z0-9\-]+:[x|\*]:\d+:\d+:[a-zA-Z0-9/\- "]*:/[a-zA-Z0-9/\-]*:/[a-zA-Z0-9/\-]+$/

Password etc shadow

/^[a-zA-Z0-9\-]+:(?:(?:!!?)|(?:\*LOCK\*?)|\*|(?:\*LCK\*?)|(?:\$.*\$.*\$.*?)?):\d*:\d*:\d*:\d*:\d*:\d*:$/

JWK

/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/gm

PGP Private

/^(-----BEGIN PGP PRIVATE KEY BLOCK-----).*([a-zA-Z0-9//\n\/\.\:\+\ \=]+).*(-----END PGP PRIVATE KEY BLOCK-----)$/

Public Keys 🔑

Ethereum Public (ETH)

/(^|\b)(0x)?[0-9a-fA-F]{64}(\b|$)/

SSH DDS Public

/^ssh-dss [0-9A-Za-z+/]+[=]{2}$/

SSH RSA Public

/^ssh-rsa AAAA[0-9A-Za-z+/]+[=]{0,3} [^@]+@[^@]+$/

PKCS7 Encrypted Data

/^(?:Signer|Recipient)Info(?:s)?\ ::=\ \w+|[D|d]igest(?:Encryption)?Algorithm|EncryptedKey\ ::= \w+$/

PGP Header

/^-{5}(?:BEGIN|END)\ PGP\ MESSAGE-{5}$/

PGP Public

/^(-----BEGIN PGP PUBLIC KEY BLOCK-----).*([a-zA-Z0-9//\n\/\.\:\+\ \=]+).*(-----END PGP PUBLIC KEY BLOCK-----)$/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment