0

I have one function that returns me String :

public String getString(String password){
 ......
 try {
 .......
 encodedPassword = Base64.encodeToString(msgDigest,1 );
 } catch (UnsupportedEncodingException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (NoSuchAlgorithmException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 return encodedPassword;
 }

I want to add (concatenate) "=" String to returning string from function

I try using this:

encrptdPassword = getString("1234");
encrptdPassword = encrptdPassword+"=";

Or:

encrptdPassword = encrptdPassword .concat("=");

but I get result like two different objects (space or brake between)

I think problem is in Base64.encodeToString , but I must use 64 based string


Function getString returns me:

A6xnQhbz4Vx2HuGl4lXwZ5U2I8iziLRFnhP5eNfIRvQ

I want to add = to the returning string as:

A6xnQhbz4Vx2HuGl4lXwZ5U2I8iziLRFnhP5eNfIRvQ=

but I receive this on output

A6xnQhbz4Vx2HuGl4lXwZ5U2I8iziLRFnhP5eNfIRvQ = 

Or:

A6xnQhbz4Vx2HuGl4lXwZ5U2I8iziLRFnhP5eNfIRvQ
=

...like 2 different strings.

Where I'm wrong?

Wayne
60.5k15 gold badges135 silver badges129 bronze badges
asked Mar 21, 2011 at 18:57
3
  • 1
    Please post an example of your input and of the wrong result that you are getting. Otherwise it is not clear what is the problem Commented Mar 21, 2011 at 18:59
  • 1
    I don't understand. Please paste exactly what output you're getting and exactly what output you're expecting. Commented Mar 21, 2011 at 19:00
  • Is this Android? You'd get a lot better answers if you pointed that out or what library you're using for the Base64 class. Commented Mar 22, 2011 at 18:46

2 Answers 2

1

I assume you're using Base64 from Apache Commons Codec.

The default constructor for this class uses "\r\n" as a line separator, which it adds to the end of every encoded line. If you don't want this, construct the object as:

new Base64(76, '');

If this isn't the class you're calling (it looks like from your code sample you're calling a static method), check the API and see if you can set a line separator for the conversion.

answered Mar 21, 2011 at 19:07
1
  • can you please post me code ? msgDigest is byte array (byte[]) Commented Mar 21, 2011 at 19:11
0

Isn't the 1 in Base64.encodeToString(msgDigest,1 ) padding?

If it's not, then you could just trim() the string to remove the whitespace.

answered Mar 21, 2011 at 19:10
1
  • 1
    @Jovan, maybe trim worked, but I wouldn't rely on it. If the space or carriage return were there before the concatenation, I'd want to understand why. What implementation of base64 are you using? Commented Mar 21, 2011 at 20:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.