简体   繁体   中英

java concatenate two strings error

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM