Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
[Bug] Error in RSA Load PublicKey in XML Format #148
I'm working the RSA connection between C# and PHP.
Here is what I'm tested
Here is a PrivateKey:
And a PublicKey:
You can use this simple app to generate
The following code has a bug
I got an error:
The bug goes here.
Finally, I Found a workaround on web.
Because the XML format of the PrivateKey contains PublicKey.
I use a simply string replace made publicKey to privateKey format
To a patched publicKey :)
then load key with privateKey format and explicitely said load publicKey.
Following is a working code:
<?php include('Crypt/RSA.php'); $rsa = new Crypt_RSA(); $private="<RSAKeyValue> <Modulus>AKoYq6Q7UN7vOFmPr4fSq2NORXHBMKm8p7h4JnQU+quLRxvYll9cn8OBhIXq9SnCYkbzBVBkqN4ZyMM4vlSWy66wWdwLNYFDtEo1RJ6yZBExIaRVvX/eP6yRnpS1b7m7T2Uc2yPq1DnWzVI+sIGR51s1/ROnQZswkPJHh71PThln</Modulus> <Exponent>AQAB</Exponent> <P>AN4DDp+IhBca6QEjh4xlm3iexzLajXYrJid6vdWmh4T42nar5nem8Ax39o3ND9b1Zoj41F9zFQmuZ8/AgabreKU=</P> <Q>AMQi+R0G9m0K+AcqK3DFpv4RD9jGc0Tle98heNYT7EQvZuuiq4XjvRz0ybqN//bOafrKhsTpRS9DQ7eEpKLI4Bs=</Q> <DP>FklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5kX6zk7S0ljKtt2jny2+00VsBerQ==</DP> <DQ>AJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2eplU9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhM=</DQ> <InverseQ>EaiK5KhKNp9SFXuLVwQalvzyHk0FhnNZcZnfuwnlCxb6wnKg117fEfy91eHNTt5PzYPpf+xzD1FnP7/qsIninQ==</InverseQ> <D>Fijko56+qGyN8M0RVyaRAXz++xTqHBLh3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxE=</D> </RSAKeyValue>"; // private key paste here $public="<RSAKeyValue> <Modulus>AKoYq6Q7UN7vOFmPr4fSq2NORXHBMKm8p7h4JnQU+quLRxvYll9cn8OBhIXq9SnCYkbzBVBkqN4ZyMM4vlSWy66wWdwLNYFDtEo1RJ6yZBExIaRVvX/eP6yRnpS1b7m7T2Uc2yPq1DnWzVI+sIGR51s1/ROnQZswkPJHh71PThln</Modulus> <Exponent>AQAB</Exponent> </RSAKeyValue>"; // public key paste here $rsa->loadKey($public, CRYPT_RSA_PRIVATE_FORMAT_XML); $plaintext = 'Hello, World'; $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1); $ciphertext = base64_encode($rsa->encrypt($plaintext)); //echo $ciphertext; $rsa->loadKey($private, CRYPT_RSA_PRIVATE_FORMAT_XML); echo $rsa->decrypt(base64_decode($ciphertext));
Works fine for me using the latest Git version. I'm pretty sure the following commit from three months ago fixed this issue:
I have similar issue @terrafrost on php 5.3.10 when is set setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_XML) if I take this out everything works.
I tried it with CRYPT_RSA_MODE_OPENSSL and CRYPT_RSA_MODE_INTERNAL with MATH_BIGINTEGER_MODE as MATH_BIGINTEGER_MODE_GMP, MATH_BIGINTEGER_MODE_BCMATH and MATH_BIGINTEGER_MODE_INTERNAL with PHP 5.3.27 and it worked in all cases.
Doesn't look like I can download 5.3.10 that easily, any more, for Windows. The earliest version available for download on 5.3.14 is as follows:
http://museum.php.net/php5/ doesn't have any 5.3 binaries for Windows either and I don't really feel like trying to compile PHP 5.3.10 on VirtualBox in the off chance that it's an issue specific to that version. Maybe you can try out different versions. Is it an issue on 5.3.27 for you? What about PHP 5.5, etc?
Are you using the latest Git version? If not could you also provide me with SSH access? Maybe email firstname.lastname@example.org or something?
Additionally, maybe post your phpinfo()?
This is my test environment for that code, if you switch branch from master -> broken then you should see exactly the same error I'm getting.
It is PHP 5.3.10 on ubuntu
If you have any problem with it please feel free to email me.