gpt4 book ai didi

java - 在java中加密字符串附加新行

转载 作者:行者123 更新时间:2023-12-01 09:42:08 24 4
gpt4 key购买 nike

在主要部分:

String password = "spK47@wF";
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
md.update(password.getBytes("UTF8"));
byte[] digestedPwdBytes = md.digest();
Base64 encoder = new Base64();
System.out.print(new String(encoder.encode(digestedPwdBytes)) +":");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

所需输出:ymm/nnotV+vzSKnXtjubqA==:

我得到的输出:

ymm/nnotV+vzSKnXtjubqA==
:

如何删除多余的新行。为什么这一新行会附加到加密字符串中?

最佳答案

使用 Apache Commons Net

行长度设置为0:

Base64 encoder = new Base64(0);
System.out.print(new String(encoder.encode(digestedPwdBytes)) +":");

输出:

ymm/nnotV+vzSKnXtjubqA==:

来自documentation

Line length: Default 76. Line length that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.

If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.

Apache Commons编解码器

或者使用Apache Commons Codec:org.apache.commons.codec.binary.Base64

Base64 encoder = new org.apache.commons.codec.binary.Base64();
System.out.print(new String(encoder.encode(digestedPwdBytes)) +":");

关于java - 在java中加密字符串附加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38369409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com