<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ajit Vishwakarma</title>
    <description>The latest articles on DEV Community by Ajit Vishwakarma (@ajitvishwakarma).</description>
    <link>https://dev.to/ajitvishwakarma</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4250545%2F79c1e8ae-d762-4d6d-b23e-6a5565498825.jpg</url>
      <title>DEV Community: Ajit Vishwakarma</title>
      <link>https://dev.to/ajitvishwakarma</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ajitvishwakarma"/>
    <language>en</language>
    <item>
      <title>Fix OpenSSL bad decrypt &amp; Passphrase Errors in PHP JWT Keys</title>
      <dc:creator>Ajit Vishwakarma</dc:creator>
      <pubDate>Tue, 17 Nov 2026 20:36:05 +0000</pubDate>
      <link>https://dev.to/ajitvishwakarma/fix-openssl-bad-decrypt-passphrase-errors-in-php-jwt-keys-195b</link>
      <guid>https://dev.to/ajitvishwakarma/fix-openssl-bad-decrypt-passphrase-errors-in-php-jwt-keys-195b</guid>
      <description>&lt;p&gt;You push a fresh PHP or Laravel build to staging, trigger a login request, and your error log fills up with &lt;code&gt;openssl_pkey_get_private(): key values mismatch&lt;/code&gt;, &lt;code&gt;Unable to read key from file&lt;/code&gt;, or &lt;code&gt;error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt&lt;/code&gt;. Everything worked on your local machine — so why is OpenSSL suddenly rejecting your private key?&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Debugging Key Errors via Random Online Tools Is Risky
&lt;/h2&gt;

&lt;p&gt;When a production deployment breaks at midnight, it is tempting to paste your &lt;code&gt;private.pem&lt;/code&gt; and &lt;code&gt;.env&lt;/code&gt; passphrase into the first online PEM checker that pops up. Most of those sites post both your key and passphrase directly to a backend server. Even a single leak forces an emergency key rotation and invalidates every active user session.&lt;/p&gt;

&lt;p&gt;Bramhify's &lt;strong&gt;Encrypted Private Key Decryptor&lt;/strong&gt; lets you isolate passphrase, encoding, and key-pair mismatches &lt;strong&gt;100% locally in your browser&lt;/strong&gt; via the native Web Crypto API — zero uploads, zero cookies, and instant memory wiping.&lt;/p&gt;

&lt;h3&gt;
  
  
  Encrypted Private Key Decryptor:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsqaalkud7r9e4x3an8nl.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsqaalkud7r9e4x3an8nl.webp" alt="Bramhify Encrypted Private Key Decryptor verifying a PHP OpenSSL PEM passphrase and derived public key fingerprint in dark theme" width="800" height="513"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Verify whether your PEM passphrase actually decrypts the key and inspect its derived SHA-256 public key fingerprint offline.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Try the 100% browser-local tool:&lt;/strong&gt; &lt;a href="https://bramhify.com/tools/private-key-decryptor" rel="noopener noreferrer"&gt;Encrypted Private Key Decryptor on Bramhify&lt;/a&gt; — zero uploads, zero tracking, runs entirely in your browser.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  The 5 Most Common Causes of &lt;code&gt;bad decrypt&lt;/code&gt; in PHP, Laravel &amp;amp; Symfony
&lt;/h2&gt;
&lt;h3&gt;
  
  
  1. Unquoted &lt;code&gt;$&lt;/code&gt; Characters in &lt;code&gt;.env&lt;/code&gt; Files (The #1 Silent Killer)
&lt;/h3&gt;

&lt;p&gt;In &lt;code&gt;vlucas/phpdotenv&lt;/code&gt; (used by Laravel) and Symfony Dotenv, double-quoted or unquoted strings containing &lt;code&gt;$&lt;/code&gt; trigger variable interpolation. If your passphrase is &lt;code&gt;my$ecret#2026&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# BROKEN — $ecret is expanded to an empty string!
JWT_PASSPHRASE=my$ecret#2026

# FIXED — single quotes prevent variable &amp;amp; comment expansion
JWT_PASSPHRASE='my$ecret#2026'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also watch for &lt;code&gt;#&lt;/code&gt; characters: an unquoted &lt;code&gt;#&lt;/code&gt; starts an inline comment in &lt;code&gt;.env&lt;/code&gt;, silently truncating the rest of your passphrase!&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Escaped &lt;code&gt;\n&lt;/code&gt; Line Breaks in Docker / Cloud Secrets
&lt;/h3&gt;

&lt;p&gt;When injecting an entire PEM key through an environment variable (&lt;code&gt;OAUTH_PRIVATE_KEY&lt;/code&gt;), CI/CD pipelines often escape newlines as literal &lt;code&gt;\n&lt;/code&gt; characters. OpenSSL rejects single-line PEM strings unless you restore real line breaks first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$pem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;str_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"\n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$_ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'JWT_PRIVATE_KEY'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;openssl_pkey_get_private&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$pem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$_ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'JWT_PASSPHRASE'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Mismatched Public Key &amp;amp; Private Key Pairs
&lt;/h3&gt;

&lt;p&gt;Sometimes the passphrase &lt;em&gt;is&lt;/em&gt; right, but &lt;code&gt;public.pem&lt;/code&gt; on the API gateway came from an older key rotation. Token signing succeeds, yet every downstream verification fails with &lt;code&gt;Invalid signature&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Legacy 3DES / OpenSSL 3.0 Legacy Provider Errors
&lt;/h3&gt;

&lt;p&gt;PHP 8.2+ compiled against &lt;strong&gt;OpenSSL 3.x&lt;/strong&gt; disables older ciphers like &lt;code&gt;RC2&lt;/code&gt; or &lt;code&gt;3DES&lt;/code&gt; by default. If your key was generated years ago with &lt;code&gt;-des3&lt;/code&gt;, OpenSSL 3 throws &lt;code&gt;unsupported&lt;/code&gt; or &lt;code&gt;bad decrypt&lt;/code&gt; even with the right passphrase.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Trailing Spaces or Windows CRLF (&lt;code&gt;\r\n&lt;/code&gt;) Inside Secret Managers
&lt;/h3&gt;

&lt;p&gt;Copying a passphrase from a password manager or spreadsheet often sneaks a trailing space or newline into Kubernetes/AWS Secrets Manager.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step-by-Step: Isolate the Bug in 30 Seconds Offline
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Test the Key + Passphrase Locally
&lt;/h3&gt;

&lt;p&gt;Open the &lt;a href="https://bramhify.com/tools/private-key-decryptor" rel="noopener noreferrer"&gt;Encrypted Private Key Decryptor&lt;/a&gt;, paste your &lt;code&gt;private.pem&lt;/code&gt;, and type the exact passphrase from your secret manager. Tap &lt;strong&gt;Decrypt Private Key&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Interpret the Result Immediately
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Decryption succeeds:&lt;/strong&gt; Your key and passphrase are 100% valid — the bug is inside &lt;code&gt;.env&lt;/code&gt; quoting (&lt;code&gt;$&lt;/code&gt;/&lt;code&gt;#&lt;/code&gt;), literal &lt;code&gt;\n&lt;/code&gt; escaping, or file permissions (&lt;code&gt;chmod 600&lt;/code&gt; on Laravel Passport keys).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decryption fails:&lt;/strong&gt; Double-check for trailing spaces, wrong environment secret, or legacy 3DES encoding (Bramhify explicitly tells you if the key uses unsupported 3DES or OpenSSH format).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 3: Verify the Derived Public Key Matches Your Config
&lt;/h3&gt;

&lt;p&gt;Compare Bramhify's &lt;strong&gt;Derived Public Key (SPKI PEM)&lt;/strong&gt; and &lt;strong&gt;SHA-256 Fingerprint&lt;/strong&gt; against the &lt;code&gt;public.pem&lt;/code&gt; deployed on your server. If they differ, replace your stale public key immediately or generate a clean new pair with our &lt;a href="https://bramhify.com/tools/key-pair-generator" rel="noopener noreferrer"&gt;Public &amp;amp; Private Key Generator&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Re-Encrypt to Modern PBES2 AES-256-CBC (Optional)
&lt;/h3&gt;

&lt;p&gt;If your key uses legacy &lt;code&gt;Proc-Type: 4,ENCRYPTED&lt;/code&gt; headers, copy the decrypted PKCS#8 PEM, switch to &lt;strong&gt;Encrypt Key (Lock)&lt;/strong&gt; mode, and export a fresh OpenSSL 3-compatible &lt;code&gt;BEGIN ENCRYPTED PRIVATE KEY&lt;/code&gt; (PBKDF2-SHA256 + AES-256-CBC). Then tap &lt;strong&gt;Wipe Memory&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  OpenSSL &amp;amp; PHP Error Code Diagnostic Matrix
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Error Message in PHP / Laravel Logs&lt;/th&gt;
&lt;th&gt;Exact Cause&lt;/th&gt;
&lt;th&gt;Fastest Fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;EVP_DecryptFinal_ex:bad decrypt&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Wrong passphrase, unquoted &lt;code&gt;$&lt;/code&gt;/&lt;code&gt;#&lt;/code&gt; in &lt;code&gt;.env&lt;/code&gt;, or trailing whitespace&lt;/td&gt;
&lt;td&gt;Single-quote &lt;code&gt;JWT_PASSPHRASE='...'&lt;/code&gt; and verify offline in Bramhify&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;PEM routines:get_name:no start line&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Literal &lt;code&gt;\n&lt;/code&gt; in env var, truncated PEM, or missing &lt;code&gt;-----BEGIN&lt;/code&gt; line&lt;/td&gt;
&lt;td&gt;Replace &lt;code&gt;\n&lt;/code&gt; with actual newlines or mount the &lt;code&gt;.pem&lt;/code&gt; file directly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;OSSL_DECODER_from_bio:unsupported&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Legacy &lt;code&gt;3DES&lt;/code&gt; or &lt;code&gt;RC2&lt;/code&gt; encrypted key on PHP 8.2+ / OpenSSL 3.x&lt;/td&gt;
&lt;td&gt;Re-encrypt to modern PBES2 &lt;code&gt;AES-256-CBC&lt;/code&gt; with &lt;code&gt;PBKDF2-SHA256&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Key path is not readable / permissions&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;File permissions too open (&lt;code&gt;0644&lt;/code&gt;) or wrong owner for &lt;code&gt;www-data&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Run &lt;code&gt;chmod 600 private.pem &amp;amp;&amp;amp; chown www-data:www-data private.pem&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;Invalid signature&lt;/code&gt; on token verify&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;public.pem&lt;/code&gt; comes from a different key pair than &lt;code&gt;private.pem&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Derive fresh SPKI &lt;code&gt;public.pem&lt;/code&gt; in Bramhify and compare SHA-256 fingerprints&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Production Key Hygiene Checklist
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Never commit &lt;code&gt;.pem&lt;/code&gt; or &lt;code&gt;.key&lt;/code&gt; files into Git — add &lt;code&gt;*.pem&lt;/code&gt; and &lt;code&gt;*.key&lt;/code&gt; to &lt;code&gt;.gitignore&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Store the &lt;code&gt;.pem&lt;/code&gt; file outside &lt;code&gt;public/&lt;/code&gt; (for example, &lt;code&gt;storage/keys/&lt;/code&gt; or mounted read-only via Docker/Kubernetes secrets).&lt;/li&gt;
&lt;li&gt;Keep the passphrase in a separate secret store from the key file itself, and clear any cached Laravel config (&lt;code&gt;php artisan config:clear&lt;/code&gt;) immediately after updating &lt;code&gt;.env&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Still seeing &lt;code&gt;401 Unauthorized&lt;/code&gt; after fixing the key? Inspect the signed token claims in our &lt;a href="https://bramhify.com/tools/jwt-decoder" rel="noopener noreferrer"&gt;JWT &amp;amp; SD-JWT Decoder&lt;/a&gt;, or read &lt;a href="https://bramhify.com/guides/decrypt-encrypted-private-key-pem-passphrase" rel="noopener noreferrer"&gt;How to Decrypt Encrypted PEM Private Keys Offline&lt;/a&gt; and &lt;a href="https://bramhify.com/guides/pkcs8-vs-pkcs1-vs-jwk-private-key-formats-explained" rel="noopener noreferrer"&gt;PKCS#8 vs PKCS#1 vs JWK Key Formats&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions (FAQs)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why does &lt;code&gt;openssl_pkey_get_private()&lt;/code&gt; return &lt;code&gt;false&lt;/code&gt; with no exception?
&lt;/h3&gt;

&lt;p&gt;PHP's OpenSSL extension returns &lt;code&gt;false&lt;/code&gt; on key load failures instead of throwing by default. Always call &lt;code&gt;openssl_error_string()&lt;/code&gt; immediately afterward in a loop to print the exact OpenSSL error queue (&lt;code&gt;bad decrypt&lt;/code&gt;, &lt;code&gt;no start line&lt;/code&gt;, or &lt;code&gt;unsupported&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Does Laravel Passport require &lt;code&gt;chmod 600&lt;/code&gt; on &lt;code&gt;oauth-private.key&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;Yes. On Linux servers, &lt;code&gt;league/oauth2-server&lt;/code&gt; (used by Laravel Passport) checks file permissions on &lt;code&gt;storage/oauth-private.key&lt;/code&gt; and refuses overly permissive files like &lt;code&gt;0644&lt;/code&gt; or &lt;code&gt;0777&lt;/code&gt;. Set &lt;code&gt;chmod 600 storage/oauth-private.key&lt;/code&gt; owned by your PHP-FPM user.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can this tool detect legacy OpenSSL &lt;code&gt;Proc-Type: 4,ENCRYPTED&lt;/code&gt; keys generated by PHP?
&lt;/h3&gt;

&lt;p&gt;Yes. Bramhify supports both modern PKCS#8 (&lt;code&gt;BEGIN ENCRYPTED PRIVATE KEY&lt;/code&gt;) and legacy OpenSSL PEM (&lt;code&gt;BEGIN RSA PRIVATE KEY&lt;/code&gt; / &lt;code&gt;BEGIN EC PRIVATE KEY&lt;/code&gt; encrypted with &lt;code&gt;AES-128-CBC&lt;/code&gt; or &lt;code&gt;AES-256-CBC&lt;/code&gt;) and displays the exact detected cipher and KDF.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is my production &lt;code&gt;.env&lt;/code&gt; passphrase safe when tested in Bramhify?
&lt;/h3&gt;

&lt;p&gt;Yes. Decryption runs completely inside your browser's volatile memory using Web Crypto with zero network requests and zero persistent storage.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://bramhify.com/guides/fix-openssl-bad-decrypt-php-laravel-jwt-keys" rel="noopener noreferrer"&gt;Bramhify Guides&lt;/a&gt;. Try the interactive 100% offline browser utility directly at &lt;a href="https://bramhify.com/tools/private-key-decryptor" rel="noopener noreferrer"&gt;bramhify.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>security</category>
      <category>jwt</category>
    </item>
  </channel>
</rss>
