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

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

问题描述

在主要:

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

我得到这个输出:

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.

应用代码:

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?

推荐答案

我可能会迟到回答这个问题,但遇到了同样的问题.其实问题出在这里Base64.encodeBase64String(hashPassword)

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

将该行更改为这样它应该可以工作:Base64.encodeBase64String(hashPassword,Base64.NO_WRAP)

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

默认情况下,Android Base64 实用程序会在编码字符串的末尾添加一个换行符.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.

检查这里

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

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