new line appending on my encrypted string

JavaEncryptionBase64

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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);

Solution 3 - Java

Use:

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

Solution 4 - Java

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.

Solution 5 - Java

A cleaner option without trimming:

String encryPass = BaseEncoding.base64().encode(hashPassword);

Solution 6 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRajesh NarravulaView Question on Stackoverflow
Solution 1 - JavaDoryView Answer on Stackoverflow
Solution 2 - Javasea catView Answer on Stackoverflow
Solution 3 - Javauser2978869View Answer on Stackoverflow
Solution 4 - JavaSujeetView Answer on Stackoverflow
Solution 5 - JavatashuhkaView Answer on Stackoverflow
Solution 6 - JavaericksonView Answer on Stackoverflow