Operating System - Linux
1748205 Members
4742 Online
108759 Solutions
New Discussion юеВ

Re: regarding password encryption in perl into *****

 
SOLVED
Go to solution
sowm18
Occasional Contributor

regarding password encryption in perl into *****

How can I write a program in Perl to prompt a user for his password such that it appears as *** when he enters it at the command line? It will then take in the password as a variable to carry on execution of the program. Also, the program should be able to execute in DOS. I have tried echo off but it does give the same effect as 'stty' in UNIX.

Thanks.
BS
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: regarding password encryption in perl into *****

Hi:

Use the 'Term::ReadKey' module. You will probably need to fetch it from CPAN.

Regards!

..JRF...
sowm18
Occasional Contributor

Re: regarding password encryption in perl into *****

Hi james,

Using that CPAN Module we can change the terminal settings but,there is no solution for my Question.
again my question is, when the user types the password from keyboard ,on the screen it should be encrpted as *****.
We can disable the display completely by using system("stty -echo"),But i donast want that ,instead it should display as **** form.
If possible please help me with the line of code which solves my question.
Thanks in Advance,

Regards,
BS
James R. Ferguson
Acclaimed Contributor

Re: regarding password encryption in perl into *****

Hi (again):

> Using that CPAN Module we can change the terminal settings but,there is no solution for my Question.

On the contrary:

# cat ./hidepw
#@(#)hidepw $ Hide password prompt - JRF $
use strict;
use warnings;
use Term::ReadKey;
$SIG{HUP} = 'IGNORE';
$SIG{INT} = 'IGNORE';
$SIG{QUIT} = 'IGNORE';
$SIG{TERM} = sub { ReadMode 'restore'; exit 0 };
my $char;
my @password;
print "Enter password: ";
ReadMode 'noecho';
ReadMode 'raw';
while ( $char = ReadKey 0 ) {
last if $char eq "\n";
next if ( ord($char) < 040 or ord($char) > 0176 );
push @password, $char;
print '*';
}
ReadMode 'restore';
print "\n";
print ">", @password, "<\n";
1;

...run as:

# ./hidepw

Regards!

...JRF...
sowm18
Occasional Contributor

Re: regarding password encryption in perl into *****

Hi James,

Thanks for ur Immediate reply.
I got to know.its like reading character by character and displaying as * for every character.


--BS
James R. Ferguson
Acclaimed Contributor

Re: regarding password encryption in perl into *****

Hi (again):

> I got to know. its like reading character by character and displaying as * for every character.

Yes, that is the underlying mechanism using getc().

By the way, welcome to the Forums. If you are happy with the responses you received, please read the following about assigning points:

http://forums.itrc.hp.com/service/forums/helptips.do?#33

Regards!

...JRF...
sowm18
Occasional Contributor

Re: regarding password encryption in perl into *****

Hi James,

Thanks for letting me know about the Points.

Regarding the above query i tried using your code .it was working with some abnormality.
i.e after entering the pwd if we hit "Enter Key" immediatly it will not come out from loop.again we need to hit the key for 3 to 4 times then its coming out from loop.
please find below chunk of code.


use strict;
use warnings;
use Term::ReadKey;
my $char;
my @password;
print "Enter password: ";
ReadMode 'noecho';
ReadMode 'cbreak';

while ( $char = ReadKey 0 ) {
if($char eq "\r") {
last;
}
push @password, $char;
print '*';
}
ReadMode 'restore';
print "\n";
print ">", @password, "<\n";


Regards,
BS.
James R. Ferguson
Acclaimed Contributor
Solution

Re: regarding password encryption in perl into *****

Hi (again):

Try this version:

# cat ./hidepw
#!/usr/bin/perl
#@(#)hidepw $ Hide password prompt - JRF $
use strict;
use warnings;
use Term::ReadKey;
$SIG{HUP} = 'IGNORE';
$SIG{INT} = 'IGNORE';
$SIG{QUIT} = 'IGNORE';
$SIG{TERM} = sub { ReadMode 'restore'; exit 0 };
my $char;
my @password;
print "Enter password: ";
ReadMode 'noecho';
ReadMode 'raw';
while ( $char = ReadKey 0 ) {
last if ( $char eq "\012" or $char eq "\015" );
next if ( ord($char) < 040 or ord($char) > 0176 );
push @password, $char;
print '*';
}
ReadMode 'restore';
print "\n";
print ">", @password, "<\n";
1;

Regards!

...JRF...