I have this RSA public key:
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtixUGzGpLXgZ7AV1HfmIHV/FEF+fww77FekRc2oLhUOd4HitwCPo76fjtdsQBEt8w9HZ3CXVphaAU2BA6MEZJ3ShVMsdAXb2ZA1C+lu7k1GV9M/BhucTg35HujSK647Sc5MwVLwFsN80dAnGsZF8gwb2TNUzXHwzbAb30T01zuqf8RCM75OwKZFYqzu7FOVrtk/w9mh92MOXG0l7WSqNIctu8Kxka/tEJJIA5nqMGNMocjwprXy66NS7FFy1GY+NnxfFLtODqq0tllc50UCDsnqSvNmj2wcnAcsCzNOoxPPgp7t8S+sQvOzgc5W3CDjIsYEiGD+vzSVNkGiRou577wIDAQAB
(Every other key I try gives the same error, too)
and this string:
$str = "VOTE\n" .
"server-list\n" .
"$user\n" .
"$userip\n" .
time()."\n";
I use this code to make the string become exactly 256 bytes so that it follows the standard for this specific application:
$leftover = (256 - strlen($str)) / 2;
while ($leftover > 0) {
$str .= "\x0";
$leftover--;
}
To format the key, I do this:
$key = wordwrap($key, 65, "\n", true);
$key = <<<EOF
-----BEGIN PUBLIC KEY-----
$key
-----END PUBLIC KEY-----
EOF;
When I do openssl_public_encrypt($str, $encrypted, $key);
I get the following warning:
Warning: openssl_public_encrypt() function.openssl-public-encrypt: key parameter is not a valid public key in ...
I'm not sure why this is happening. This is what the key looks like when I echo it:
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtixUGzGpLXgZ7AV1HfmIH
V/FEF
fww77FekRc2oLhUOd4HitwCPo76fjtdsQBEt8w9HZ3CXVphaAU2BA6MEZJ3ShVMsd
AXb2ZA1C
lu7k1GV9M/BhucTg35HujSK647Sc5MwVLwFsN80dAnGsZF8gwb2TNUzXHwzbAb30T
01zuqf8RCM75OwKZFYqzu7FOVrtk/w9mh92MOXG0l7WSqNIctu8Kxka/tEJJIA5nq
MGNMocjwprXy66NS7FFy1GY
NnxfFLtODqq0tllc50UCDsnqSvNmj2wcnAcsCzNOoxPPgp7t8S
sQvOzgc5W3CDjIsYEiGD vzSVNkGiRou577wIDAQAB
-----END PUBLIC KEY-----
Any help would be appreciated!
Edit: A working key should look like this:
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmHzD76i8DA25nC+Qsswi
OM0lW+gViiQD4tEm7suxBc2BGibtdlrsprVIId92hSjQKx4x8+XVWU6k89T5vy8Y
txpXN759OWdGkDi8uvZuYclMjW9Rao+oqSvbXH37R7oSY287I+6uOHclGhniQN3q
RyoXBkbhDk0/FTI/i549q/gGk1UZYv449KLrDOqmtohRcIyAYVnvvWtD1kIzourq
hMtEIrPqwoBqTaUA9kOIXw1jMovao2TN52j48KgOg9KjqtdwUwD9e6n7hJd/subF
6woc8L7zjJFOHH5gacUC7vtiMpBpnSyLQpjFLepYYwftjsRmg4xLdh+Zvgw3xqi4
lwIDAQAB
-----END PUBLIC KEY-----
It occured to me why it wasn't working, so I am here to answer my own question. Now that I think about it, it ties into what @sarnold originally said: the + signs are gone (and they got replaced by spaces)
it turns out that because i was getting the key via HTTP POST, it formatted the URL (and thus the key) turning all the + signs into spaces. That threw off the key and caused this conundrum.
Thanks for the help. :)
First, I would recommend using phpseclib, a pure PHP RSA implementation. It's a ton more portable and easier to use and all that jazz. Example:
<?php
$key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtixUGzGpLXgZ7AV1HfmIHV/FEF+fww77FekRc2oLhUOd4HitwCPo76fjtdsQBEt8w9HZ3CXVphaAU2BA6MEZJ3ShVMsdAXb2ZA1C+lu7k1GV9M/BhucTg35HujSK647Sc5MwVLwFsN80dAnGsZF8gwb2TNUzXHwzbAb30T01zuqf8RCM75OwKZFYqzu7FOVrtk/w9mh92MOXG0l7WSqNIctu8Kxka/tEJJIA5nqMGNMocjwprXy66NS7FFy1GY+NnxfFLtODqq0tllc50UCDsnqSvNmj2wcnAcsCzNOoxPPgp7t8S+sQvOzgc5W3CDjIsYEiGD+vzSVNkGiRou577wIDAQAB';
$rsa = new Crypt_RSA();
$rsa->loadKey($key);
$rsa->setPublicKey($key);
echo $rsa->getPublicKey();
If you insist on using the openssl extension... try this:
"-----BEGIN PUBLIC KEY-----\r\n" . chunk_split($whatever) . "\r\n-----END PUBLIC KEY-----"
Simply use str_replace(' ', '+', $string)
Worked for me!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With