đây là mã đầy đủ sử dụng một phần mã được đề cập ở trên và một phần được lấy từ Perlmonk. Kịch bản đầu tiên yêu cầu tên người dùng và mật khẩu từ người dùng, mã hóa và lưu trữ nó trong tệp .crypt. Sau đó đọc từ nó, giải mã và hiển thị văn bản gốc. Vào lần thứ hai, nó sẽ sử dụng các thông tin người dùng hiện có.
use Crypt::Rijndael;
use IO::Prompter;
use Crypt::CBC;
#keys
my $key = "a" x 32;
my $cipher = Crypt::CBC->new(-cipher => 'Rijndael', -key => $key);
my @plaintext;
my @ciphertext;
#keys
#filefield
#password file name
#my $file_name = ".crypt";
my $file_name = ".crypt";
#File Handler
my $file;
#If we cannot open the password file we initiate a new one
unless (open ($file, '<:encoding(UTF-8)', $file_name)) { #<:encoding(UTF-8)
#Create a new file in write mode
open ($file, '>', $file_name);
$plaintext[0]= prompt "Username:";
$plaintext[1]= prompt "Password:", -echo => '';
print "#################################################################################\n";
print "# User credentials will be encrypted and stored in .crypt file and same is #\n";
print "# reused next time. If you need to add new user credentials delete the .crypt #\n";
print "# file and re run the same script. #\n";
print "#################################################################################\n";
$plaintext[0]=~ s/^\s*(.*?)\s*$/$1/;
$plaintext[1]=~ s/^\s*(.*?)\s*$/$1/;
while($plaintext[0] =~ /^\s*$/){
$plaintext[0]= prompt "Username is mandatory:";
$plaintext[0]=~ s/^\s*(.*?)\s*$/$1/;
}
while($plaintext[1] =~ /^\s*$/){
$plaintext[1]= prompt "Password is mandatory:";
$plaintext[1]=~ s/^\s*(.*?)\s*$/$1/;
}
$ciphertext[0] = $cipher->encrypt($plaintext[0]);
$ciphertext[1] = $cipher->encrypt($plaintext[1]);
#we save the password in a file
print $file $ciphertext[0];
#print $file "\n";
#we save the password in a file
print $file $ciphertext[1];
#we close the file (Writing mode)
close $file;
#Reopen the file in reading mode
open ($file, '<', $file_name)
}
my @holder;
my $content;
if (open($file, '<', $file_name)) {
#chomp(@holder = <$file>);
local $/;
$content = <$file>;
} else {
warn "Could not open file '$filename' $!";
}
@holder = split(/(?=Salted__)/, $content);
print "Encrypted username:",$holder[0];
print "\n";
print "Encrypted password:",$holder[1],"\n";
#Loading the password en decrypt it
$plaintext[0] = $cipher->decrypt($holder[0]);
$plaintext[1] = $cipher->decrypt($holder[1]);
print "\n\n";
print 'Username is:',"$plaintext[0]\n";
print 'Password is:',"$plaintext[1]\n";
#Close the file
close $file;
#filefield
Nguồn
2016-11-22 10:20:18
ngay cả khi nó hoạt động, bạn sẽ giải mã nó như thế nào? – pavel
có thể 'md5' sẽ giúp bạn, kiểm tra tổng và tất cả ... – gaussblurinc
Vâng, tôi cũng thấy nó. Vấn đề nằm trong [Crypt :: RSA :: Key :: Private :: SSH :: deserialize line 68] (https://metacpan.org/source/VIPUL/Crypt-RSA-1.99/lib/Crypt/RSA/Key /Private/SSH.pm#L68). 'croak" Định dạng tệp khóa không hợp lệ "trừ khi $ id eq PRIVKEY_ID;' ** $ id ** là '----- BEGIN RSA PRIVATE KEY -----', ** PRIVKEY_ID ** là 'TÊN CHÍNH RIÊNG CỦA SSH FORMAT 1.1'. – daxim