在我的加密字符串新行追加 [英] new line appending on my encrypted string

查看:196
本文介绍了在我的加密字符串新行追加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在主营:

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.

应用程序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;
}

如何删除额外的新行。
这是为什么发生了,请帮我的的原因是什么?

推荐答案

我可能会回答这个较晚,但具有同样的问题碰上了。其实问题就出在这里
Base64.en codeBase64String(hashPassword)

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

更改该行看起来像这样它应该工作:
Base64.en codeBase64String(hashPassword,Base64.NO_WRAP)

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

默认情况下,Android版的Base64 UTIL增加了一个换行符的EN codeD字符串的结尾。
该Base64.NO_WRAP标志告诉UTIL没有换行符来创建连接codeD字符串。

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.

这篇关于在我的加密字符串新行追加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆