2

I encrypt password with RSA method, I set the padding mode "OPENSSL_NO_PADDING", it returns false. But if I remove the padding mode parameter, keep it default, encrypt successfully.

$pass = 'gmwtwjfws';
$encrypetd_pass = '';
$pub_key = '-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs+JvfyTOMHqvjxHJyDZG
HZpz3atV7qcOT8mijXGGG3S+8Bb2p2kREGJwrzC2IIErCQUcZ3Wa3wTugKQDxqXE
SPt76HN2353ufegbvTI9kYgK0MLFpY8OZAMsaTytVrvUEVHjqGXZO4z7oVTqByuB
wcZAvK+sN39+MqisS6ZejACbbQLkWZgcSgt5wBAaDaEa2lvRYcVbNyO/mqTU6SSf
d+w78uM07BpmxhimOMwf+l/qs+Z04LUm4Ay7b+AHHAwbaHeehC1wInzNDfipgR0H
0FCa/LOnEblj2HVpptB/NY4XNG+CDHTBKkxzEw92D/Nj1JIlr1oP0l+/VdAnxxiW
uQIDAQAB
-----END PUBLIC KEY-----';
$result = openssl_public_encrypt($pass, $encrypetd_pass, $pub_key);
var_dump($result);  //return false
exit;

What's more, I am confused about the public key's format. If I keep the default padding mode, and the format of public key is like above, it return true. But when I modify the key format as follow(remove one new line), it return false. Would anyone explain the reason? Thanks very mush.

$pub_key = '-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs+JvfyTOMHqvjxHJyDZGHZpz3atV7qcOT8mijXGGG3S+8Bb2p2kREGJwrzC2IIErCQUcZ3Wa3wTugKQDxqXE
SPt76HN2353ufegbvTI9kYgK0MLFpY8OZAMsaTytVrvUEVHjqGXZO4z7oVTqByuB
wcZAvK+sN39+MqisS6ZejACbbQLkWZgcSgt5wBAaDaEa2lvRYcVbNyO/mqTU6SSf
d+w78uM07BpmxhimOMwf+l/qs+Z04LUm4Ay7b+AHHAwbaHeehC1wInzNDfipgR0H
0FCa/LOnEblj2HVpptB/NY4XNG+CDHTBKkxzEw92D/Nj1JIlr1oP0l+/VdAnxxiW
uQIDAQAB
-----END PUBLIC KEY-----';
1
  • Warning: openssl_public_encrypt(): key parameter is not a valid public key. function openssl_error_string will show "error:0906D06C:PEM routines:PEM_read_bio:no start line".
    – SuperBear
    Mar 15, 2016 at 6:16

1 Answer 1

1

I hit the same problem and found a bug report at https://bugs.php.net/bug.php?id=61203

The answer to the bug report was that when using OPENSSL_NO_PADDING you have to provide data of the correct size, i.e. by appending \0's.

Now all I need to do is figure out the correct size :)

5
  • Size appears to be 256 byte boundaries.
    – TrophyGeek
    Jan 25, 2017 at 19:50
  • But from an API standpoint, what's the point? NOT having padding would seem to save you space, so OPENSSL_NO_PADDING sounds attractive; however, you just have to pad it anyways.
    – TrophyGeek
    Jan 25, 2017 at 19:57
  • Yes, you have to put Padding in. What NO_PADDING does is to allow you to do the padding (to 256 or whatever bytes) with your own padding scheme. I was trying to match PHP encrypt with Java decrypt, and it turns out that there is no common padding scheme between PHP and Java. But by doing my own padding I could emulate the Java PKCS1 padding scheme in PHP, and then decrypt using the standard Java utilities. Jan 30, 2017 at 2:14
  • So, the padding flag OPENSSL_PKCS1_PADDING didn't work for that purpose?
    – TrophyGeek
    Jan 30, 2017 at 17:52
  • 1
    No, I found a mention somewhere that PHP changed from PKCS1 to PKCS7 because PKCS1 can be insecure. But they did not change the flag name. So even though you use the flag OPENSSL_PKCS1_PADDING, it is doing PKCS7, for which I could not find Java decryption. Jan 31, 2017 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.