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?

7
  • Well that's your base64 data - now how are you transferring it to the phone, and what does your Java code look like? Commented Sep 10, 2013 at 21:19
  • I'll bet my bottom dollar that the 'invalid trailing byte' is a \n. post your code. Commented Sep 10, 2013 at 21:23
  • What exactly is in that base64 string? When I copy/pasted it into 3 different decoders online I get gibberish back. Commented Sep 10, 2013 at 21:26
  • @JonathanKuhn That's because it's AES encrypted bytes, check my updated questions please. Commented Sep 10, 2013 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. Commented Sep 10, 2013 at 21:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

\n isn't the carriage return character. \r is the carriage return character. \n is the line feed character. stackoverflow.com/questions/3091524/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.