#1 2020-04-07 13:09:25

Hafedh TRIMECHE
Member
Registered: 2016-09-18
Posts: 32

Base64 decoding problem

This Base64 encoded message won't be decoded because of the LineBreak after each 76 characters!

MIIBuTCCASKgAwIBAgIQNdNhtuV5GbNHYZsf+LvM0zANBgkqhkiG9w0BAQUFADAb
MRkwFwYDVQQDExBFZGlkZXYgU21va2VUZXN0MB4XDTA4MTExMjE5NTEzNVoXDTM5
MTIzMTIzNTk1OVowGzEZMBcGA1UEAxMQRWRpZGV2IFNtb2tlVGVzdDCBnzANBgkq
hkiG9w0BAQEFAAOBjQAwgYkCgYEAm6zGzqxejwswWTNLcSsa7P8xqODspX9VQBuq
5W1RoTgQ0LNR64+7ywLjH8+wrb/lB6QV7s2SFUiWDeduVesvMJkWtZ5zzQyl3iUa
CBpT4S5AaO3/wkYQSKdI108pXH7Aue0e/ZOwgEEX1N6OaPQn7AmAB4uq1h+ffw+r
RKNHqnsCAwEAATANBgkqhkiG9w0BAQUFAAOBgQCZmj+pgRsN6HpoICawK3XXNAmi
cgfQkailX9akIjD3xSCwEQx4nG6tZjTz30u4NoSffW7pch58SxuZQDqW5NsJcQNq
Ngo/dMoqqpXdi2/0BYEcJ8pjsngrFm+fM2BnyGpXH7aWuKsWjVFGlWlF+yi8I35Q
8wFJt2Z/XGA7WWDjvw==

Please note that Base64ToBinSafe is used for decoding.

Offline

#2 2020-04-07 16:09:59

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,534
Website

Re: Base64 decoding problem

@ab - current Base64ToBin do not understand so called "transfer encoding for MIME (RFC 2045)" base64 as provided above.

We have such parser in SyNode here in SyNodeBinding_buffer.pas anyBase64ToBin

Implementation is copy-pasted from SynCommons and patched to support:
- optional padings
- RFC 2045
- 2 bytes representation of str (0xC1 0x0 0xC2 0x0 ...)
- estimate a length of result

I use it in production for many years - so implementation can be moved to SynCommons.pas

P.S
Not sure the original source is SynCommons - may be I translate this parser from Apache as noted in comments there

Last edited by mpv (2020-04-07 16:19:48)

Offline

#3 2020-04-07 16:11:25

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: Base64 decoding problem

As the new lines are encoded within the base64 string itself, I believe it must be safe to replace the breaks with nothing before decoding.

Offline

#4 2020-04-07 16:15:19

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,534
Website

Re: Base64 decoding problem

@macfly - as a simple solution - yes, but replace require an additional memory allocations

Offline

#5 2020-04-07 16:36:33

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: Base64 decoding problem

@mpv, Yes I agree with you.

My suggestion was for a workarround.

Offline

Board footer

Powered by FluxBB