我正在编写一个Perl脚本,需要连接到SMTP服务器才能发送邮件,但我真的不喜欢这样的事情:
my $pass = '123456';
我找到了Data :: Encrypted,它应该允许用户第一次提示它然后加密存储它.
use Data::Encrypted file => ".passwd",qw(encrypted); my $password = encrypted('password');
但我无法使其工作,它会导致运行时错误:
Bad key file format at /Library/Perl/5.12/Data/Encrypted.pm line 78
有人有同样的问题,或者知道隐藏/保护密码的其他方法吗?
解决方法
Data::Encrypted模块最后一次发布于2001年.我想说这是一个不使用它的好兆头.
通常情况下,我认为存储密码甚至是加密的坏主意.但是,如果您必须存储密码以便与另一个系统联系使用,则可以对其进行加密.我这样做的方式是这样的:
# Rijndael is also known as AES,which is the encryption standard used by the NSA use Crypt::Rijndael; use IO::Prompter; # This secret is exactly 32 bytes long,you could prompt for this as a # passphrase or something and pad it with spaces or whatever you need my $app_secret = 'this_is_the_key_the_app_uses....'; # Setup the encryption system my $crypto = Crypt::Rijndael->new( $app_secret,Crypt::Rijndael::MODE_CBC() ); # Ask the user to enter the password the first time my $password = prompt "password: ",-echo => ''; # from IO::Prompter # Encrypt the password. You can save this off into a file however you need to my $enc_password = $crypto->encrypt($password); # Later load it from the file and decrypt it: my $password = $crypto->decrypt($password);
有关更多信息,请参阅Crypt::Rijndael和IO::Prompter.