63

In Main:

public static void main(String[] args) throws NoSuchAlgorithmException {
    System.out.println("encrypt:" + encryptPassword("superuser")+":" );
}

public static String encryptPassword(final String password) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] hashPassword = md.digest(password.getBytes());
    String encryPass = Base64.encodeBase64String(hashPassword);
    return encryPass;
}

I'm getting this output:

encrypt:C66i8K4gFQ23j1jN2sRCqQ==:

But when I implemented the same thing in my application I'm getting the output below:

encrypt:C66i8K4gFQ23j1jN2sRCqQ==
:

Note: new line appending on my encrypted string.

application code:

public boolean authenticateUsernamePasswordInternal(UsernamePasswordCredentials credentials) {
    try {
        System.out.println("encrypt:" + getHash("superuser")+":" );
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new BadCredentialsAuthenticationException(ErrorConstants.CONNECTION_FAILED);
    }
}

private String getHash(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException{  
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] hashPassword = md.digest(password.getBytes());
    String encryPass = Base64.encodeBase64String(hashPassword);
    return encryPass;
}

How I can remove that extra new line. why this is happened, please help me what is the reason?

Rajesh Narravula
  • 1,433
  • 3
  • 26
  • 54
  • Where is your new line space? – Masudul Nov 18 '13 at 05:17
  • what extra new line space?? can you please be clear – Kamlesh Arya Nov 18 '13 at 05:20
  • Just do encyPass.replaceAll("\n","") it will be right – Houcem Berrayana Nov 18 '13 at 05:21
  • above "note" output i pasted my output but is coming as space – Rajesh Narravula Nov 18 '13 at 05:21
  • Please pay more attention to the formatting of your code. You appear not to have even pasted the full code - your authenticateUsernamePasswordInternal method doesn't have a closing brace. Also, use spaces rather than tabs in Stack Overflow. – Jon Skeet Nov 18 '13 at 05:22
  • i added String encryPass = Base64.encodeBase64String(hashPassword).trim(); i getting correct output. but i surprised, same code when i executed in main() method I'm getting correct answer,same thing i implemented in application I'm getting extra new line. why this is happened, please help me **what is the reason?** – Rajesh Narravula Nov 18 '13 at 05:33
  • The correct answer is [Dory's answer](https://stackoverflow.com/questions/20040539/new-line-appending-on-my-encrypted-string#25011746), not the accepted one. – palindrom Feb 26 '15 at 12:02

6 Answers6

210

I may be late in answering this, but came across with same problem. Actually problem lies here Base64.encodeBase64String(hashPassword)

Change that line to look like this it should work: Base64.encodeBase64String(hashPassword,Base64.NO_WRAP)

By default the Android Base64 util adds a newline character to the end of the encoded string. The Base64.NO_WRAP flag tells the util to create the encoded string without the newline character.

Check here

Dory
  • 7,462
  • 7
  • 33
  • 55
5

In case anyone needs this for any libraries using OkHttp, there's a Credentials class you can use for Base64 encoding your username/pass

String credentials = Credentials.basic("username", "password");

request.header(HttpHeaders.AUTHORIZATION, credentials);
sea cat
  • 251
  • 5
  • 8
2

Use:

String encryPass = Base64.encodeBase64String(hashPassword).trim();
1

A cleaner option without trimming:

String encryPass = BaseEncoding.base64().encode(hashPassword);
tashuhka
  • 5,028
  • 4
  • 45
  • 64
1

You just need to Use Base64 encoding in following way

Base64.encodeBase64String("Your data to encrypt in base64", Base64.DEFAULT)

Change above line with the followings

Base64.encodeBase64String("Your data to encrypt in base64",Base64.NO_WRAP)

It worked for me.

Sujeet
  • 558
  • 8
  • 13
  • `Base64.encodeBase64String("Your data to encrypt in base64",Base64.NO_WRAP)` is the accepted answer. not sure what different in you answer. – Rajesh Narravula Sep 12 '20 at 13:48
0

It depends on the implementation of Base64.encodeBase64String(). What is that method?

If it's from Apache commons, be aware that there are a few different classes that handle whitespace differently.

For example, org.apache.commons.net.util.Base64 chunks output, and it probably adds a CR-LF sequence to the final chunk.

The more common version, org.apache.commons.codec.binary.Base64, does not add whitespace.

erickson
  • 265,237
  • 58
  • 395
  • 493
  • 1
    both places I'm using **org.apache.commons.codec.binary.Base64** – Rajesh Narravula Nov 18 '13 at 07:38
  • 1
    Can confirm that with `org.apache.commons.codec.binary.Base64` I'm also getting random trailing whitespace characters. (commons-codec-1.4.jar) – radimpe Feb 28 '14 at 05:38
  • @radimpe: version 1.4 of that class changed the behavior of the default constructor to do chunking and appends a newline character. v1.3 or v1.5 would not do this. https://issues.apache.org/jira/browse/CODEC-89 – patmortech Jun 24 '15 at 21:53
  • Luckily there's a well-specified and built-in core Java implementation finally (`java.util.Base64`). – erickson Jun 24 '15 at 21:56