【问题标题】:How do I display star(*) instead of plain text for password c++如何显示星号(*)而不是密码c ++的纯文本
【发布时间】:2014-12-13 15:22:25
【问题描述】:

如何才能在 C++ 中显示星号 (*) 而不是纯文本密码。

我要求输入密码,它在屏幕上是通行证。

如何将它们转换为星号(*),以便用户在输入时看不到密码。

这是我目前拥有的

        char pass[10]={"test"};
        char pass1[10];
        textmode(C40);
        label:
        gotoxy(10,10);
        textcolor(3);
        cprintf("Enter password :: ");
        textcolor(15);
        gets(pass1);
        gotoxy(10,11);
        delay(3000);
        if(!(strcmp(pass,pass1)==0))
        {
          gotoxy(20,19);
          textcolor(5);
          cprintf("Invalid password");
          getch();
          clrscr();
          goto label;
        }

谢谢

【问题讨论】:

标签: c++ passwords


【解决方案1】:

您需要使用无缓冲的输入函数,例如 curses 库提供的getch (),或者您的操作系统的控制台库。调用此函数将返回按下的键字符,但不会回显。使用getch () 读取每个字符后,您可以手动打印*。如果按退格键,还需要编写代码,并适当更正插入的密码。

这是我曾经用诅咒编写的代码。用gcc file.c -o pass_prog -lcurses编译

#include <stdio.h>
#include <stdlib.h>
#include <curses.h>

#define ENOUGH_SIZE 256

#define ECHO_ON 1
#define ECHO_OFF 0

#define BACK_SPACE 127

char *my_getpass (int echo_state);

int main (void)
{
  char *pass;

  initscr ();

  printw ("Enter Password: ");
  pass = my_getpass (ECHO_ON);

  printw ("\nEntered Password: %s", pass);
  refresh ();
  getch ();
  endwin ();
  return 0;
}


char *my_getpass (int echo_state)
{
  char *pass, c;
  int i=0;

  pass = malloc (sizeof (char) * ENOUGH_SIZE);
  if (pass == NULL)
  {
    perror ("Exit");
    exit (1);
  }

  cbreak ();
  noecho ();

  while ((c=getch()) != '\n')
  {
    if (c == BACK_SPACE)
    {
      /* Do not let the buffer underflow */
      if (i > 0)
      { 
        i--;
        if (echo_state == ECHO_ON)
               printw ("\b \b");
      }
    }
    else if (c == '\t')
      ; /* Ignore tabs */
    else
    {
      pass[i] = c;
      i = (i >= ENOUGH_SIZE) ? ENOUGH_SIZE - 1 : i+1;
      if (echo_state == ECHO_ON)
        printw ("*");
    }
  }
  echo ();
  nocbreak ();
  /* Terminate the password string with NUL */
  pass[i] = '\0';
  endwin ();
  return pass;
}

【讨论】:

  • 当我包含curses.h 文件unable to open include file 时会引发错误。能否请您告诉我为什么无法包含该文件
  • 因为您的系统中可能没有安装 curses 库。你在什么系统中工作?哪个操作系统?
【解决方案2】:

C++ 本身没有任何东西支持这一点。您的示例代码中的函数表明您正在使用curses,或类似的东西;如果是这样,请检查 cbreak 和 nocbreak 函数。一旦你调用了cbreak,你就可以回显字符了,你可以回显任何你喜欢的东西(或者不回显,如果你愿意的话)。

【讨论】:

    【解决方案3】:
    #include<iostream.h>
    #include<conio.h>
    #include<stdio.h>
    void main()
    {
     clrscr();
     char a[10];
     for(int i=0;i<10;i++)
     {
      a[i]=getch();     //for taking a char. in array-'a' at i'th place 
      if(a[i]==13)      //cheking if user press's enter 
      break;            //breaking the loop if enter is pressed  
      printf("*");      //as there is no char. on screen we print '*'
     }
     a[i]='\0';         //inserting null char. at the end
     cout<<endl;
     for(i=0;a[i]!='\0';i++)  //printing array on the screen
     cout<<a[i];
     sleep(3);                //paused program for 3 second
    }
    

    【讨论】:

    • getch()获取您在键盘上按下的键,但不会在屏幕上打印任何内容,因此我们使用 printf("*") 打印一个“*”,而不是用户输入的字符,如果用户按下回车键(13 - 回车的 ascii 代码)循环中断,行 -a[i]='\0',在数组末尾放置一个空字符。sleep(3) 将程序终止延迟三秒。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 2013-04-24
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多