Java Base64解码,使用UTF-8、UTF-16输出不正确

Java Base64解码,使用UTF-8、UTF-16输出不正确,java,mysql,spring,utf-8,base64,Java,Mysql,Spring,Utf 8,Base64,我已经在我的计算机上建立了本地restAPI服务器,并且我有注册功能,将用户数据发送到mySQL数据库 我已将密码字符串编码为base64字符串,希望尝试加密我自己。 当我试图解码我从数据库获取的密码时,我得到了错误的输出 密码,例如base64:MDQ1MTA0NTE= 哪个将输出-04510451 解码器类- byte[] decodedValue = Base64.getDecoder().decode(password); return new String(decodedVal

我已经在我的计算机上建立了本地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;
编码器类-

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

提前感谢,,
乔纳森

看起来你解码了两次。这在您提供的代码段中不可见,但将是最符合逻辑的解释:


MDQ1MTA0NTE=->04510451->ӎuӎu

无法复制,您的输入密码可能不是UTF-8@ernest_k看看编码器类,finalPass=newStringPassEncoded,UTF-8;看起来您正在解码两次:MDQ1MTA0NTE=->04510451->uӎu@yonatanhornstein很难理解,为什么需要解码,理想情况下,密码是以编码的形式存储的,在验证登录密码时,用户提供的密码会再次编码,并且只匹配编码字符串,而不是实际密码。如果你愿意的话,我可以详细解释一下。这甚至不是一种处理密码的温和安全的方法。