new line appending on my encrypted string

前端 未结 6 1541
长发绾君心
长发绾君心 2020-12-08 03:54

In Main:

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


        
相关标签:
6条回答
  • 2020-12-08 04:16

    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.

    0 讨论(0)
  • 2020-12-08 04:18

    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

    0 讨论(0)
  • 2020-12-08 04:22

    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);
    
    0 讨论(0)
  • 2020-12-08 04:22

    A cleaner option without trimming:

    String encryPass = BaseEncoding.base64().encode(hashPassword);
    
    0 讨论(0)
  • 2020-12-08 04:22

    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.

    0 讨论(0)
  • 2020-12-08 04:40

    Use:

    String encryPass = Base64.encodeBase64String(hashPassword).trim();
    
    0 讨论(0)
提交回复
热议问题