如何使用Perl输入密码并用'*'替换字符?

时间:2009-03-31 13:54:16

标签: perl terminal passwords echo

我有一个Perl脚本,要求用户输入密码。我怎样才能回复'*'来代替用户输入的字符,因为他们输入了它?

我正在使用Windows XP / Vista。

7 个答案:

答案 0 :(得分:25)

过去我曾使用IO::Prompt

use IO::Prompt;
my $password = prompt('Password:', -e => '*');
print "$password\n";

答案 1 :(得分:18)

如果您不想使用任何包...仅适用于UNIX

system('stty','-echo');
chop($password=<STDIN>);
system('stty','echo');

答案 2 :(得分:16)

您可以使用Term :: ReadKey。这是一个非常简单的示例,有一些检测退格键和删除键。我在Mac OS X 10.5上测试了它,但根据ReadKey manual,它应该在Windows下运行。 manual表示在Windows下使用非阻塞读取(ReadKey(-1))将失败。这就是为什么我使用的ReadKey(0)基本上是getc(更多关于libc manual中的getc。)

#!/usr/bin/perl                                                                                                                                                                                                

use strict;                                                                                                                                                                                                    
use warnings;                                                                                                                                                                                                  
use Term::ReadKey;                                                                                                                                                                                             

my $key = 0;                                                                                                                                                                                                   
my $password = "";                                                                                                                                                                                             

print "\nPlease input your password: ";                                                                                                                                                                        

# Start reading the keys                                                                                                                                                                                       
ReadMode(4); #Disable the control keys                                                                                                                                                                         
while(ord($key = ReadKey(0)) != 10)                                                                                                                                                                            
# This will continue until the Enter key is pressed (decimal value of 10)                                                                                                                                      
{                                                                                                                                                                                                              
    # For all value of ord($key) see http://www.asciitable.com/                                                                                                                                                
    if(ord($key) == 127 || ord($key) == 8) {                                                                                                                                                                   
        # DEL/Backspace was pressed                                                                                                                                                                            
        #1. Remove the last char from the password                                                                                                                                                             
        chop($password);                                                                                                                                                                                       
        #2 move the cursor back by one, print a blank character, move the cursor back by one                                                                                                                   
        print "\b \b";                                                                                                                                                                                         
    } elsif(ord($key) < 32) {                                                                                                                                                                                  
        # Do nothing with these control characters                                                                                                                                                             
    } else {                                                                                                                                                                                                   
        $password = $password.$key;                                                                                                                                                                            
        print "*(".ord($key).")";                                                                                                                                                                              
    }                                                                                                                                                                                                          
}                                                                                                                                                                                                              
ReadMode(0); #Reset the terminal once we are done                                                                                                                                                              
print "\n\nYour super secret password is: $password\n";   

答案 3 :(得分:8)

您应该查看Term::ReadKeyWin32::Console。您可以使用这些模块读取单键击并发出'*'或者其他内容。

答案 4 :(得分:1)

在Pierr-Luc的程序基础上,刚刚在反斜杠上添加了一些控件。有了这个,你不能永远按下反斜杠:

sub passwordDisplay() {
    my $password = "";
    # Start reading the keys
    ReadMode(4); #Disable the control keys
    my $count = 0;
    while(ord($key = ReadKey(0)) != 10) {
            # This will continue until the Enter key is pressed (decimal value of 10)
            # For all value of ord($key) see http://www.asciitable.com/
            if(ord($key) == 127 || ord($key) == 8) {
                    # DEL/Backspace was pressed
                    if ($count > 0) {
                            $count--;
                            #1. Remove the last char from the password
                            chop($password);
                            #2 move the cursor back by one, print a blank character, move the cursor back by one
                            print "\b \b";
                    }
            }
            elsif(ord($key) >= 32) {
                    $count++;
                    $password = $password.$key;
                    print "*";
            }
    }
    ReadMode(0); #Reset the terminal once we are done
    return $password;
}

答案 5 :(得分:0)

使用Pierr-Luc的程序

# Start reading the keys                                                                                                                                                                                       
ReadMode(4); #Disable the control keys                                                                                                                                                                         
while(ord($key = ReadKey(0)) != '13' )                                                                                                                                                                            
# This will continue until the Enter key is pressed (decimal value of 10)                                                                                                                                      
{                                                                                                                                                                                                              
    # For all value of ord($key) see http://www.asciitable.com/                                                                                                                                                
    if(ord($key) == 127 || ord($key) == 8 && (length($password) > 0)) {                                                                                                                                                                   
        # DEL/Backspace was pressed                                                                                                                                                                            
        #1. Remove the last char from the password                                                                                                                                                             
        chop($password);                                                                                                                                                                                       
        #2 move the cursor back by one, print a blank character, move the cursor back by one                                                                                                                   
        print "\b \b";                                                                                                                                                                                         
    } elsif(ord($key) > 32) {                                                                                                                                                                                  
        $password = $password.$key;                                                                                                                                                                            
        print "*";                                                                                                                                                                              
    }                                                                                                                                                                                                         
}                                                                                                                                                                                                              
ReadMode(0); #Reset the terminal once we are done

答案 6 :(得分:-5)

您是否尝试过存储字符串(以便程序仍可以读取它)并查找其长度,然后创建一个长度相同的字符串,但只使用“*”?