c++ - C++ 中 ubuntu 中的密码屏蔽

标签 c++ linux ubuntu passwords masking

我最近制作了一个程序,它使用 getch() 来屏蔽输入的密码。但只要输入正确的代码,它就会立即提供访问权限,而无需等待按下回车键。应该做哪些改变?另外,在 Ubuntu 中是否允许 getch() ?如果不是,应使用什么替代方案?

我的代码如下所示。我已经在不同的功能中给出了默认密码。

char pass[4];
cout << "\nEnter Administrator Password: ";
for (i = 0; i < 4; i++)
{
  pass[i] = getch();
  cout << "*";
}
for (i = 0; i < 4; i++)
{
  if(admin_pass[i] == pass[i])
    return 1;
  else
    return 0;
}

最佳答案

您可以按如下方式使用getpass()

#include<stdio.h>
#include<unistd.h>
#include<string.h>


char *pass=getpass("\nEnter Administrator Password: "); 

if(strcmp(admin_pass,pass)==0) 
  return 1;
else 
  return 0;    

函数getpass()定义在头文件unistd.h中。

关于c++ - C++ 中 ubuntu 中的密码屏蔽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16221900/

相关文章:

c++ - 这是一个有效的 Copy Ctor 吗?

c++ - 对这个简单的官方 boost::iostream 代码给出了一堆警告?

linux - 除非我使用奇怪的 .xinitrc,否则 GNOME 会卡住

linux - virtio中guest notifier和host notifier为什么分别使用ioeventfd和irqfd?

在共享库构造函数中创建线程

Ubuntu boost b2 错误 : Cannot use --layout=system with --build-type complete

regex - 使用带有 sed 的正则表达式删除 html 标记

c++ - 用 new 分配大块内存

c++ - 这个条件检查语句在 for 循环中合法吗?

linux - 如何生成 fullchain.pem 和 privkey.pem?