0

I'm trying to decode my Base64 code that encoded by my PHP server, It's work and will be decoded with PHP normally but when I trying to encode it through my Android phone I got the following error:

Base64DecoderException: encoded value has invalid trailing byte

My Base64 Code:

oLAwb6uSn2JXqAFTX+qJXaOawOYF3kDDK2HlCb7ItCeimVCsDE7OYH5OgsixKpIAM6KgkCktnB4HsLQtA5Ig1fQvDrRcct9dQi4m8wPpF7a3sFHSG29j2aItKeouflTtsSZgKWvSjg0gBBGM/7PlvkuK+8W4/GXS0QrqV1jcngWrspYmAdi0GiJbPm8b/zlscOIa1z1df11SuQH5+GiUzqZ4WDFOpoH0WWVW3KmbMQ2yifBmXnhn80qZct6KiN7aL8PHEczhNrRqAKfUuEwmsWOnEOyh7UOU6FcnW3VAo2BWd5dJRGgWb5Py09l0XmrdWdzin7klKtMqXOWQRcvEVT7PKtQxQotRpOa+2IQQirVfybyuMipY9YORuW1hqmc95Tdt1WHdIzVwEtq6NXx9AC5mSklbxrcOpINfS2RPFcK0UUMV2xQKAQ+u8PzTj/KBEmb04ObBbnX6y3uL1KT58lDecA9lIbNYuttlgRMzRdxFOvkk21wou2vtMBtIxk0XFJJGjazqqcxVeSxTvQ68wdNSkRmvteowkSq2Vi09CmOhToRHemFyZgKTxSBoNaFuVuYGVggEFIR9kHVrLxoK2Q==

Any ideas?

iSun
  • 1,714
  • 6
  • 28
  • 57
  • Well that's your base64 data - now how are you transferring it to the phone, and what does your Java code look like? – Jon Skeet Sep 10 '13 at 21:19
  • I'll bet my bottom dollar that the 'invalid trailing byte' is a `\n`. post your code. – Sammitch Sep 10 '13 at 21:23
  • What exactly is in that base64 string? When I copy/pasted it into 3 different decoders online I get gibberish back. – Jonathan Kuhn Sep 10 '13 at 21:26
  • @JonathanKuhn That's because it's `AES` encrypted bytes, check my updated questions please. – iSun Sep 10 '13 at 21:34
  • 1
    I'm sure all your encryption/encoding is fine, it's the actual *output* of the data that's where the problem likely lies. See @jay-sheth's answer below, and try a simple `s.trim()` on the input in your Java code to remove any offending whitespace. – Sammitch Sep 10 '13 at 21:48
  • You are not passing any offset to `doFinal` method, doesn't returned string end with some null bytes? – dev-null-dweller Sep 10 '13 at 21:49
  • Why are you converting your string into bytes at all? Use `Base64.decode(s, Base64.DEFAULT)`. (And trim the line break, of course.) And *always* specify an encoding when you convert bytes to a string (the `String` constructor call). – Jon Skeet Sep 10 '13 at 22:05

1 Answers1

4

if the invalid trailing byte is a carriage return (\n), the easiest solution would be to leave out the closing PHP tag (on all of your PHP files):

<?php
//Code goes here
//Leave out the closing tag: ?>

Otherwise, you may be able to trim surrounding whitespace and carriage returns (in your Java code), before you base64 decode it.

Jay Sheth
  • 1,738
  • 16
  • 15
  • \n isn't the carriage return character. \r is the carriage return character. \n is the line feed character. https://stackoverflow.com/questions/3091524/what-are-carriage-return-linefeed-and-form-feed – Achille Dec 28 '17 at 16:49