0

I would like the input on the scanf to show as asterisks opposed to the actual letters. For example, when the program asks for a password, I would like it to show "******" instead of "PRG255".

Please let me know what you think and if it is possible with the current format of the code. This way, the password can stay safe if someone is looking at the user input the password.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>


int main()
{

    int  stop = 0, length, i, key=55;
    char decider, data[50], password[]="PRG255", passwordCheck[7];
    
    while (stop != 1)
    {
        decider = NULL;
        printf("\n(E)nter and encrypt a message \n(V)iew encrypted message\n(D)ecrypt and view message (password protected)\n(Q)uit\n\n");
        scanf(" %c", &decider);

        switch (decider)
        {
        case 'e':
        case 'E':
            flushall();
            printf("\nEnter data to encrypt: ");
            scanf("%s", data);

            length = strlen(data);

            for (i = 0; i < length; i++)
            {
                data[i] = data[i] ^ key;
            }

            
            break;
        case 'v':
        case 'V':
            printf("\n%s\n", data);
            break;
        case 'd':
        case 'D':
            
            for(i = 0; i < 3; i++)
            {
                printf("please enter password(three attempts)\n");
                scanf("%s", passwordCheck);
                if (strcmp(password, passwordCheck) == 0)
                {
                    for (i = 0; i < length; i++)
                    {
                        data[i] = data[i] ^ key;
                    }
                    printf("\n%s\n\n", data);
                }
                
            }
            if (strcmp(password, passwordCheck) != 0)
            {
                printf("invalid password enetered three times! program will terminate now!\n\n");
                return 0;
            }   
            break;

        case 'q':
        case 'Q':

            return 0;
            break;
        default:
            printf("An invalid option was selected!\n\n");


        }
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
babySteps
  • 39
  • 8
  • @kaylum: That question is tagged `linux`, whereas the OP of this question did not specify any platform tag. Therefore, I don't believe it is a duplicate (unless the OP provides more information). – Andreas Wenzel Oct 13 '20 at 00:36
  • 1
    This is not possible using only ISO C features. There's *some* way to do it on every OS that allows for interactive keyboard input, but they're all different; to help, we need to know which one(s) you care about. – zwol Oct 13 '20 at 00:47
  • By the way, [you should never use `scanf` for interactive input](https://stackoverflow.com/q/58403537). In fact, you shouldn't use it at all. – zwol Oct 13 '20 at 00:49
  • By the way, this is trivially breakable encryption you've coded here. – zwol Oct 13 '20 at 00:50
  • can someone help me apply that concept to this code – babySteps Oct 13 '20 at 02:08
  • also if i use fgets instead of scanf i cant seem to get it to work – babySteps Oct 13 '20 at 02:24
  • You will probably need to put the terminal into 'raw' mode with 'no echo', read the characters typed (with `read()`), up to the newline, echoing an asterisk for each character read, and processing the 'delete character' (and maybe 'kill line' and 'kill word' and other control sequences too) as it comes. All of which is not trivial. And you need to set the terminal back to the original mode after you've finished. There are libraries that'll support this. Since you've still not divulged which operating system(s) you're working on, it's not easy to be more specific than this. – Jonathan Leffler Oct 13 '20 at 03:12

0 Answers0