新行附加在我的加密字符串 [英] new line appending on my encrypted string
问题描述
在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 encryptPass = Base64.encodeBase64String(hashPassword);
返回encryptPass;
}
我得到这个输出:
encrypt:C66i8K4gFQ23j1jN2sRCqQ ==:
但是当我在应用程序中实现同样的事情时,我得到以下输出:
加密:C66i8K4gFQ23j1jN2sRCqQ ==
:
注意:添加我加密字符串的新行。 p>
应用代码:
public boolean authenticateUsernamePasswordInternal(UsernamePasswordCredentials credentials){
try {
System.out.println(encrypt:+ getHash(superuser)+:);
} catch(Exception e){
logger.error(e.getMessage(),e);
抛出新的BadCredentialsAuthenticationException(ErrorConstants.CONNECTION_FAILED);
}
}
private String getHash(String password)throws NoSuchAlgorithmException,UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance(MD5);
byte [] hashPassword = md.digest(password.getBytes());
String encryptPass = Base64.encodeBase64String(hashPassword);
返回encryptPass;
}
我如何删除这个额外的新行。
为什么会发生这种情况,请帮助我原因是什么?
可能迟到回答这个问题,但遇到同样的问题。实际问题在这里
Base64.encodeBase64String(hashPassword)
更改该行看起来像这样应该工作:
Base64.encodeBase64String(hashPassword,Base64.NO_WRAP)
默认情况下,Android Base64 util将一个换行符添加到编码字符串的末尾。
Base64.NO_WRAP标志告诉util创建没有换行字符的编码字符串。
检查 here
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?
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
这篇关于新行附加在我的加密字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!