Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base64::decode() gives Incorrect Padding error - base64_decode() works just fine #4

Closed
terrafrost opened this issue Jun 12, 2016 · 5 comments

Comments

@terrafrost
Copy link

In the following code I have one string that I test against Base64::decode() and against base64_decode() and each one behaves differently.

First, my code:

<?php
require __DIR__ . '/vendor/autoload.php';

use ParagonIE\ConstantTime\Base64;

$str = 'MIIFzzCCBLegAwIBAgIDAfdlMA0GCSqGSIb3DQEBBQUAMHMxCzAJBgNVBAYTAlBM
MSgwJgYDVQQKDB9LcmFqb3dhIEl6YmEgUm96bGljemVuaW93YSBTLkEuMSQwIgYD
VQQDDBtDT1BFIFNaQUZJUiAtIEt3YWxpZmlrb3dhbnkxFDASBgNVBAUTC05yIHdw
aXN1OiA2MB4XDTExMTEwOTA2MDAwMFoXDTEzMTEwOTA2MDAwMFowgdkxCzAJBgNV
BAYTAlBMMRwwGgYDVQQKDBNVcnrEhWQgTWlhc3RhIEdkeW5pMRswGQYDVQQFExJQ
RVNFTDogNjEwNjA2MDMxMTgxGTAXBgNVBAMMEEplcnp5IFByemV3b3Jza2kxTzBN
BgNVBBAwRgwiQWwuIE1hcnN6YcWCa2EgUGnFgnN1ZHNraWVnbyA1Mi81NAwNODEt
MzgyIEdkeW5pYQwGUG9sc2thDAlwb21vcnNraWUxDjAMBgNVBCoMBUplcnp5MRMw
EQYDVQQEDApQcnpld29yc2tpMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCM
m5vjGqHPthJCMqKpqssSISRos0PYDTcEQzyyurfX67EJWKtZj6HNwuDMEGJ02iBN
ZfjUl7r8dIi28bSKhNlsfycXZKYRcIjp0+r5RqtR2auo9GQ6veKb61DEAGIqaR+u
LLcJVTHCu0w9oXLGbRlGth5eNoj03CxXVAH2IfhbNwIDAQABo4IChzCCAoMwD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xjN8pxzxiUx6bJAgturnIMEfRNesxwghdr1dkUjOhGL
f3kHVzgM6j3VAM7oFmMUb5y5s96Bzl10DodWitjOEH0vvnIcsppSxH1C1dCAi0o9
f/1y2XuLNhBNHMAyTqpYPX8Yvav1c+Z50OMaSXHAnTa20zv8UtiHbaAhwlifCelU
Mj93S';

$str = preg_replace('#[\r\n]#', '', $str);

echo strlen(base64_decode($str)) . "\n";
echo strlen(Base64::decode($str)) . "\n";

When I run that code I get the following output:

1491

Fatal error: Uncaught RangeException: Incorrect padding in /path/to/vendor/paragonie/constant_time_encoding/src/Base64.php:105
Stack trace:
#0 /path/to/test.php(42): ParagonIE\ConstantTime\Base64::decode('MIIFzzCCBLegAwI...')
#1 {main} thrown in /path/to/vendor/paragonie/constant_time_encoding/src/Base64.php on line 105

Any ideas? I was under the impression Base64::decode() was supposed to be a drop in replacement for base64_decode.

@paragonie-scott
Copy link
Member

Padding probably. I'll make it no-padding-tolerant when the power turns
back on
On Jun 12, 2016 7:36 PM, "terrafrost" [email protected] wrote:

In the following code I have one string that I test against
Base64::decode() and against base64_decode() and each one behaves
differently.

First, my code:

@terrafrost
Copy link
Author

Thanks! Good luck with the power!

@paragonie-scott
Copy link
Member

paragonie-scott commented Jun 13, 2016

This string appears to deviate from the standard.

https://tools.ietf.org/html/rfc4648

The minimum size of an unpadded output sequence is two characters. The last S should be followed by one more. It looks like PHP's just discards it.

@paragonie-scott
Copy link
Member

https://github.com/paragonie/constant_time_encoding/releases/tag/v1.0.1 should no longer encounter this error. If anyone needs strict mode, the RFC4648 API will use strict padding.

@paragonie-scott
Copy link
Member

v1.0.1 and v2.0.2 should both handle these edge cases now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants