Java 在我的加密字符串上附加新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20040539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
new line appending on my encrypted string
提问by Rajesh Narravula
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?
我如何删除额外的新行。为什么会这样,请帮我看看是什么原因?
采纳答案by Dory
I may be late in answering this, but came across with same problem. Actually problem lies here
Base64.encodeBase64String(hashPassword)
我可能会迟到回答这个问题,但遇到了同样的问题。其实问题出在这里
Base64.encodeBase64String(hashPassword)
Change that line to look like this it should work:
Base64.encodeBase64String(hashPassword,Base64.NO_WRAP)
将该行更改为这样它应该可以工作:
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.
默认情况下,Android Base64 实用程序会在编码字符串的末尾添加一个换行符。Base64.NO_WRAP 标志告诉实用程序创建没有换行符的编码字符串。
Check here
在这里查看
回答by erickson
It depends on the implementation of Base64.encodeBase64String()
. What is that method?
这取决于Base64.encodeBase64String()
. 那是什么方法?
If it's from Apache commons, be aware that there are a few different classes that handle whitespace differently.
如果它来自 Apache 公共资源,请注意有几个不同的类以不同的方式处理空格。
For example, org.apache.commons.net.util.Base64
chunks output, and it probably adds a CR-LF sequence to the final chunk.
例如,org.apache.commons.net.util.Base64
chunks 输出,它可能会在最后一个chunk 中添加一个 CR-LF 序列。
The more common version, org.apache.commons.codec.binary.Base64
, does not add whitespace.
更常见的版本org.apache.commons.codec.binary.Base64
不添加空格。
回答by erickson
Use:
用:
String encryPass = Base64.encodeBase64String(hashPassword).trim();
回答by tashuhka
A cleaner option without trimming:
无需修剪的更清洁选项:
String encryPass = BaseEncoding.base64().encode(hashPassword);
回答by sea cat
In case anyone needs this for any libraries using OkHttp, there's a Credentials
class you can use for Base64 encoding your username/pass
如果有人需要使用 OkHttp 的任何库,有一个Credentials
类可以用于 Base64 编码您的用户名/密码
String credentials = Credentials.basic("username", "password");
request.header(HttpHeaders.AUTHORIZATION, credentials);