473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to make the console hide passwords that are typed in

Hi,

I need to make a command line application that I have written support
an option that allows the user to use the /p:* switch to specify that
they would like to type a password but have it hidden so that each key
stroke generates '*' as its output, hiding the what was typed by the
user. If anyone knows how to do this in C# your help would be greatly
appreciated.

Thanks in advanced,

Ryan

Feb 6 '06 #1
10 6642
Ryan,

Assuming you are using a TextBox for the password entry, just set the
'PasswordChar' property to a non-zero value...

Regards,
John Bendiksen

Feb 6 '06 #2
On 6 Feb 2006 15:02:44 -0800, "JohnAtAphelion"
<jb********@aphelion.net> wrote:
Ryan,

Assuming you are using a TextBox for the password entry, just set the
'PasswordChar' property to a non-zero value...

Regards,
John Bendiksen


There's no GUI in a console.

Ken Wilson
Seeking viable employment in Victoria, BC
Feb 7 '06 #3
On 6 Feb 2006 13:42:22 -0800, ry*********@netiq.com wrote:
Hi,

I need to make a command line application that I have written support
an option that allows the user to use the /p:* switch to specify that
they would like to type a password but have it hidden so that each key
stroke generates '*' as its output, hiding the what was typed by the
user. If anyone knows how to do this in C# your help would be greatly
appreciated.

Thanks in advanced,

Ryan


Forgive me, it's late here so I won't be typing any sample code but
I'll get you started on what I would attempt as the solution. Others
my approach this differently so don't be surprised to see the right
answer in here with mine, hehehe ...

Read the character input one character at a time and put it in a char
array (computationally cheap) or append it to a string
(computationally expensive). As each char is read in output an '*' to
the console.

Ken Wilson
Seeking viable employment in Victoria, BC
Feb 7 '06 #4
Thanks Ken,

That was the approach that I was trying to take but I am not sure how
to do that using C#. I was playing around with streams and the
Console.In and Console.Out properties but can't figure out how to
prompt the user for input but have that input redirected directly to me
before it pops up on the screen. If anyone is more familiar with
streams and System.Console please post up.

Ryan

Feb 7 '06 #5

"CodeSlayer" <ry*********@netiq.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
| Thanks Ken,
|
| That was the approach that I was trying to take but I am not sure how
| to do that using C#. I was playing around with streams and the
| Console.In and Console.Out properties but can't figure out how to
| prompt the user for input but have that input redirected directly to me
| before it pops up on the screen. If anyone is more familiar with
| streams and System.Console please post up.
|
| Ryan
|

You'll have to disble the console "Echo" and "Line" mode, there is no way to
do this using the framework so you have to PInvoke some Kernel32 API's.
API's you will need to call are:

1. GetStdHandle: This is needed to get the handle to the console input file
handle.
2. GetConsoleMode: used to get the current mode of the console input device.
and
3. SetConsoleMode: to change the mode (disable Echo mode and Line input
mode).
Now you can read character by character from keyboard input and display a *
for each char received.
When done you'll have to restore the previous mode, so you have to save the
mode in 2.

Please check MSDN for more details about the API's mentioned above.

Willy.

Feb 7 '06 #6
Thanks Willy. I can't believe the Console object doesn't have a
property for this type of thing.

Feb 7 '06 #7

"CodeSlayer" <ry*********@netiq.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
| Thanks Willy. I can't believe the Console object doesn't have a
| property for this type of thing.
|

It's very uncommon to do this in console style applications, don't forget
that when adding features to a class, you are automatically extending it's
memory consumption (both code and data), that means there is a tradeoff to
make - features and resource usage. Don't let people pay for what they don't
need in most cases.

Willy.
Feb 7 '06 #8
Your concern is much appreciated but there is definitely a need and I
can't imagine any performance trade offs that would override that need.
Also, my experience has been very different with applications needing
this feature. Without this type functionality how do you allow someone
to discretely enter their password in a console application?

Ryan

Feb 7 '06 #9
On Tue, 7 Feb 2006 17:07:00 +0100, "Willy Denoyette [MVP]"
<wi*************@telenet.be> wrote:

"CodeSlayer" <ry*********@netiq.com> wrote in message
news:11*********************@g47g2000cwa.googlegr oups.com...
| Thanks Ken,
|
| That was the approach that I was trying to take but I am not sure how
| to do that using C#. I was playing around with streams and the
| Console.In and Console.Out properties but can't figure out how to
| prompt the user for input but have that input redirected directly to me
| before it pops up on the screen. If anyone is more familiar with
| streams and System.Console please post up.
|
| Ryan
|

You'll have to disble the console "Echo" and "Line" mode, there is no way to
do this using the framework so you have to PInvoke some Kernel32 API's.
API's you will need to call are:

1. GetStdHandle: This is needed to get the handle to the console input file
handle.
2. GetConsoleMode: used to get the current mode of the console input device.
and
3. SetConsoleMode: to change the mode (disable Echo mode and Line input
mode).
Now you can read character by character from keyboard input and display a *
for each char received.
When done you'll have to restore the previous mode, so you have to save the
mode in 2.

Please check MSDN for more details about the API's mentioned above.

Willy.

Thanks for the further input as I suspected I might not have the whole
picture. I haven't had to do any console programming for a while. It
makes me wonder why we couldn't have a mechanism as simple as
'cin'/'cout' though. [8-)

Ken Wilson
Seeking viable employment in Victoria, BC
Feb 7 '06 #10
Well, I have to disagree, it's not because YOU need something that there is
a common need for it, especialy when you have the possibility to add the
feature through PInvoke (which is an important tool in managed code after
all). Also, keep in mind, you're not the only one which may have some
obscure feature it would like to see added to the framework, if you don't
take care you'll end with an unmanageable framework (too many API's),
containing a lot of features that are only needed by very few applications,
that would account for a large part of the already (too) large working set
of a managed application. Notice that the System.Console (mscorlib) class is
loaded into all types of managed applications.

Willy.
"CodeSlayer" <ry*********@netiq.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
| Your concern is much appreciated but there is definitely a need and I
| can't imagine any performance trade offs that would override that need.
| Also, my experience has been very different with applications needing
| this feature. Without this type functionality how do you allow someone
| to discretely enter their password in a console application?
|
| Ryan
|
Feb 7 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mvdevnull | last post by:
static void Main(string args) { DoSomething(); } static void DoSomething() { for (int i=0; i<=10; i++) { CallAsyncMethod(); } } my problem is when i run the app console exists without really...
2
by: nick | last post by:
I am using C# do write a console program. Can I do the following tasks: 1. Detect if a key on keyboard is pressed? and get the typed ASC II code? 2. Catch and handle the event of the Control-C?...
3
by: Freeon | last post by:
Hi, I am looking for a way to sort a strong typed dataset. It would seem the most straightforward way is to use a dataview. The only problem is when I use the dataview I seem to loose the strong...
3
by: James Stroud | last post by:
Hello, Does anyone know of the most straightforward way to get rid of the intensely annoying "console" window that py2app feels so compelled to create? On a related but less important note,...
4
by: Ronald S. Cook | last post by:
I've always used untyped datasets. In a Microsoft course, it walks through creating typed datasets and harps on the benefits. It has you drag all these things around ..wizard, wizard, wizard......
3
by: John Passaniti | last post by:
Hopefully someone can point out what I'm doing wrong. I find myself having to dynamically create HTML code, and have found that the usual way you see to do this is an unreadable mess, like this:...
1
by: Allen Maki | last post by:
Hi everybody, I need your help. I am using Visual C++ .NET How can I make Console::ReadLine accepts int? In other words. I know I can do this:
15
by: Scott M. | last post by:
Hi, I'm an old VB6 guy just starting out in VB.Net using Visual Studio Express. I want to build a simple console app that will create a simple text file every 10 minutes. I can create the file...
5
topher23
by: topher23 | last post by:
I've seen a lot of questions about how to make secure database passwords. I'm going to go over a method of encrypting a password using the MD5 encryption algorithm for maximum security. First,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.