我怎样才能进入使用Perl密码并将其替换字符“*”?

问题描述 投票:18回答:7

我有一个要求用户输入密码的Perl脚本。我怎么也只能随声附和“*”代替字符的用户类型,因为他们类型呢?

我使用的是Windows XP / Vista操作系统。

perl terminal passwords echo
7个回答
16
投票

你可以玩期限:: ReadKey。这是一个很简单的例子,一些检测退格键和删除键。我测试了它在Mac OS X 10.5,但根据它应该工作在Windows下的ReadKey manual。该manual表明,在Windows下使用非阻塞读(ReadKey(-1))将失败。这就是为什么我使用ReadKey(0)谁基本上getc(更多GETC在libc manual)。

#!/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";   

25
投票

在过去,我已经使用IO::Prompt这一点。

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

18
投票

如果你不希望使用任何套餐......仅用于UNIX

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

8
投票

你应该看看要么Term::ReadKeyWin32::Console。您可以使用这些模块来读取单次击键,并发出“*”或whathever。


1
投票

在Pierr - 吕克的计划的基础上,刚添加的反斜杠一些控制。有了这个,你不能永远保持按下反斜线:

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;
}

0
投票

使用Pierr - 吕克的计划

# 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

-5
投票

您是否尝试存储字符串(让你的程序仍然可以读取它),并找出它的长度,然后创建相同长度的字符串,但只能用“*”?

© www.soinside.com 2019 - 2024. All rights reserved.