将 PinField.getPassword() 的输出转换为字符串

发布于 2024-09-12 04:51:10 字数 976 浏览 3 评论 0原文

        if ("Submit".equals(cmd)) { //Process the password.
    String UserNameInput=UserName.getText();
    ///////////////////////////////ERROR Pin when printed shows error/////////////////////////////
    char[] PinInput = PinField.getPassword();
    String pinInput=PinInput.toString();
            //for debugging , print PinInput , but it prints garbage
    System.out.println("Pin entered is "+PinInput);
            //pinInput has garabage instead of the Pin that was entered
            //so the function isPasswordCorrect fails to verify UserName & Pin
    if (isPasswordCorrect(UserNameInput,pinInput)) 
              {
               //some tasks
              }
         }
            boolean isPasswordCorrect(String Username,String Pin)
            {
             //verify username & pin 
            }

我需要将 PinInput 从字符数组转换为字符串,以便我可以使用函数 isPasswordCorrect() 。当我使用 toString() 方法时,它会产生垃圾值,我应该做什么来转换 PinInput 的值到 String ?

        if ("Submit".equals(cmd)) { //Process the password.
    String UserNameInput=UserName.getText();
    ///////////////////////////////ERROR Pin when printed shows error/////////////////////////////
    char[] PinInput = PinField.getPassword();
    String pinInput=PinInput.toString();
            //for debugging , print PinInput , but it prints garbage
    System.out.println("Pin entered is "+PinInput);
            //pinInput has garabage instead of the Pin that was entered
            //so the function isPasswordCorrect fails to verify UserName & Pin
    if (isPasswordCorrect(UserNameInput,pinInput)) 
              {
               //some tasks
              }
         }
            boolean isPasswordCorrect(String Username,String Pin)
            {
             //verify username & pin 
            }

I need to convert the PinInput from character array to String, so that I can use the function isPasswordCorrect() . When I use toString() method , it produces garbage value , what should I do to convert
the value of PinInput to String ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒冷纷飞旳雪 2024-09-19 04:51:10

查看 String API,有一个接受 char 数组的构造函数。

Look at the String API, there is a constructor that accepts a char array.

花间憩 2024-09-19 04:51:10

@camickr 是对的。那不是垃圾;那是垃圾。 pinField.getPassword().toString() 显示char[] 的地址,例如[C@fd54d6

附录:String.valueOf(array)new String(array) 将转换 getPassword()String,但这会妨碍返回 char[] 的安全优势。考虑将值保留在本地并遵循 API 指南:“建议在使用后通过将每个字符设置为零来清除返回的字符数组。”

@camickr is right. That's not garbage; pinField.getPassword().toString() shows the address of the char[], e.g. [C@fd54d6.

Addendum: String.valueOf(array) or new String(array) will convert the result of getPassword() to a String, but this thwarts the security benefit of returning a char[]. Consider keeping the value local and following the API guidelines: "it is recommended that the returned character array be cleared after use by setting each character to zero."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文