【问题标题】:Base64 Decoding, output is not correct using UTF-8, UTF-16Base64解码,使用UTF-8、UTF-16输出不正确
【发布时间】:2019-05-23 16:54:24
【问题描述】:

我已经在我的电脑上建立了本地restAPI服务器,并且我有注册功能,将用户数据发送到mySQL数据库。

我已将密码字符串编码为 base64 字符串(想尝试加密我自己)。 当我尝试解码从数据库中获取的密码时,我得到了错误的输出。

密码例如(base64):MDQ1MTA0NTE=

这将输出- 04510451

解码器类 -

byte[] decodedValue = Base64.getDecoder().decode(password);  


return new String(decodedValue, StandardCharsets.UTF_8);

编码器类 -

byte[] passEncoded = Base64.getEncoder().encode(password.getBytes("UTF-8"));
System.out.println("encoded value is " + new String(passEncoded));
String finalPass = null;
finalPass = new String(passEncoded, "UTF-8");

return finalPass;

现在我得到的实际输出:“ӎuӎu”(需要是 04510451)

提前致谢, 乔纳森

【问题讨论】:

  • 无法复现,可能你输入的password不是UTF-8
  • @ernest_k 看编码器类,finalPass = new String(passEncoded, "UTF-8");
  • 看起来你在解码两次:MDQ1MTA0NTE= -> 04510451 -> ӎuӎu
  • @yonatanhornstein 很难理解,为什么需要解码,理想情况下,密码以编码形式存储,并且在验证登录密码时,用户提供的密码再次编码,仅编码 strings 是匹配,而不是实际的 password。如果你愿意,我可以详细解释。
  • 这甚至不是一种稍微安全的密码处理方式。

标签: java mysql spring utf-8 base64


【解决方案1】:

看起来您正在解码两次。这在您提供的代码 sn-ps 中不可见,但这是最合乎逻辑的解释:

MDQ1MTA0NTE= -> 04510451 -> ӎuӎu

【讨论】:

    猜你喜欢
    • 2012-04-12
    • 1970-01-01
    • 2019-03-05
    • 2014-04-02
    • 2015-09-03
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 2013-08-04
    相关资源
    最近更新 更多