title: Can an RSA OpenSSL key generated with C/C++ be decrypted with PHP?
body: <p>In a number of situations security software involves the interaction between (desktop) applications and web interfaces. For (RSA) asymmetric encryption we use the <a href="https://openssl.org" rel="nofollow">C library</a> of OpenSSL (version 1.0.2d at present) and we use the openssl <a href="http://php.net/manual/en/book.openssl.php" rel="nofollow">PHP library</a>.</p>
  
  <p>All the standard stuff:</p>
  
  <ul>
  <li>private encrypt (C++) data and public decrypt (PHP)</li>
  <li>public encrypt (C++) data and private decrypt (PHP)</li>
  </ul>
  
  <p>works. And also the other way around: PHP -> C++ and C++ -> PHP</p>
  
  <p>In addition generating an encrypted private key with PHP and decrypting that key with C/C++ also works. But not the other way around: I want to decrypt with PHP a private key that is generated and encrypted with C++. Note: this is different from encrypting/decrypting data with the keys.</p>
  
  <p>The problem seems to be the way C OpenSSL encrypts a private key. It is not enough to know the algorithm with which the key is encrypted (aes-256-cbc in our case). The password is also stretched. The way the key is encrypted by OpenSSL has changed over time. I will give an example how it looks now.</p>
  
  <p>Part of the the header of the private key generated with OpenSSL C/C++ looks like: </p>
  
  <pre><code>-----BEGIN ENCRYPTED PRIVATE KEY-----
  MIISnzBJBgkqhkiG9w0BBQ0wPDAbBgkqhkiG9w0BBQwwDgQI/epYUO8+LygCAggA
  </code></pre>
  
  <p>Whereas part of the header of a pem-encoded private key generated with openssl PHP looks like: </p>
  
  <pre><code>-----BEGIN RSA PRIVATE KEY-----
  Proc-Type: 4,ENCRYPTED
  DEK-Info: DES-EDE3-CBC,372DA3A61BEB36AA
  
  bc9NsTtdfcMW9t9isDPgl86aME223ockk9pIGDduEyrTS7zh1gwHWSWnD2efbxLd
  </code></pre>
  
  <p>I have tried to use some PHP extensions, like <a href="http://phpseclib.sourceforge.net/" rel="nofollow">phpseclib</a>, but I still cannot use a private key generated with C OpenSSL in PHP programs. The public keys are not encrypted and they can be exchanged between the two languages without any problem.</p>
  
  <p>For generating RSA keys with C OpenSSL we use the EVP_PKEY structure. </p>
  
  <p><strong>Edited by author on August 12, 2015</strong> <br/></p>
  
  <p>In reply to neubert  I have generated an encrypted private RSA key with the OpenSSL utility "openssl genpkey":</p>
  
  <pre><code>OpenSSL&gt; genpkey -algorithm RSA -out c:\temp\test512.pem -aes-256-cbc -pass pass:1234 -pkeyopt rsa_keygen_bits:512
  </code></pre>
  
  <p>So key length is 512 bits and password "1234".</p>
  
  <p>To get the unencrypted form of the key I used "openssl pkey":</p>
  
  <pre><code>OpenSSL&gt; pkey -in c:\temp\test512.pem  -out c:\temp\test512naked.pem
  </code></pre>
  
  <p>Note on Windows the openssl executable is called "openssl.exe".</p>
  
  <p>Here are the keys:<br/></p>
  
  <pre><code>-----BEGIN ENCRYPTED PRIVATE KEY-----
  MIIBrzBJBgkqhkiG9w0BBQ0wPDAbBgkqhkiG9w0BBQwwDgQITePvOJ8u8lECAggA
  MB0GCWCGSAFlAwQBKgQQ9gg/UzRleUGcOGK9P18fiASCAWCIo5c7q/HT7IcdtpiJ
  y1bTj+SsqAilQIPIf1wtN2VjuVDQMSN35neI2X9TL3H9dNd6BVwJnzkfKbEAKK1+
  ipj2KjOIVipctul6QIh9TS+MkGO0ZI+TaMJX4TaoPanLkQ00bOhnFod9W5UHZvVU
  EdVx1+9bvvEngFqqweKjAfSySQ6Y9JD3E/ZSg1Bja3c9uLTlYFuMSs6S9iVUimlw
  BCJXlfeHL5o331qwpAPjzOFD4ztTsOpnpXIt3y9l53u6UThHMWiTon5NpJgeQGny
  GXSWvfZ4mhOjpUixrgFC/VqLjAHNvG9mqC4xoufNK3/QPCMNBsGpJ2gUSoX4/SkA
  edUaPFhKRja8f9cvBW6vs67/lvAYjQ2tZOR8l7Jgj5AL3mKi8wyD/QISrJqFDrq6
  dYLqyofv+5OJRWtAE4KJEASTVIJktnvTaxTI7gB+cRp/BHdDLvrTmfZ7UbI9Zx+K
  ZLia
  -----END ENCRYPTED PRIVATE KEY-----
  </code></pre>
  
  <p>and </p>
  
  <pre><code>-----BEGIN PRIVATE KEY-----
  MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqxGy/AXwTEvx+moN
  eRMNO/bWYBE+dX7kNROzswC2SzO6+NgYqKKIkYb8+Iho2ssuoYVWc62Gk37gxEhi
  QrIDRQIDAQABAkEAlsA1I0S0evfeGNfGYbC5U+N1DRmFGhOlVWS0UgVJn8BYIpQI
  fjseZ3xXhtfZypzTzc9VZKUJedi3cv7Ju9gqYQIhANzE+XbiytUzBFTzKuRanMEn
  o6noJDGiaVktWvbbZEkpAiEAxl5VedZ1PCU5Qpd+1u7agIZfSBwWnkI0nnxO5Slo
  AL0CIEWc0rCbGKwbVx1WQ8sXi2AYmLHFokwIU0GsXIeEbF3pAiALvbOjdX0U5UMh
  XOQmBpcqOknTc84m6dZBdywYRj4gpQIgfg/KLv7cv/mGoe8tvh3geYTpnZ2HRwDN
  O9Kal0WOaiw=
  -----END PRIVATE KEY-----
  </code></pre>