我有一个Perl脚本,它接受一个输入字符串。该字符串包含$
个符号。字符串使用openssl加密并解密。 enc
和dec
是以下代码段中的加密和解密字符串。
#! /usr/bin/perl
use strict;
use warnings;
my $input = 'build:$6$n2cR7cY/$aKv6qQRWjYMIQIyAdTsw6nPtMzEZoqHziqyrL9sn/m29k5mkQjgcwZarA.8lAte6mmDGakipjetuCiH9YF2fF0:16736:0:99999:7:::';
print "Input: $input\n";
my $enc = `openssl enc -base64 -A <<< $input`;
print "Encrypted Output: $enc\n";
my $dec = `openssl enc -base64 -A -d <<< $enc`;
print "Decrypted Output: $dec\n";
输出:
# ./temp.pl
Input: build:$6$n2cR7cY/$aKv6qQRWjYMIQIyAdTsw6nPtMzEZoqHziqyrL9sn/m29k5mkQjgcwZarA.8lAte6mmDGakipjetuCiH9YF2fF0:16736:0:99999:7:::
Encrypted Output: YnVpbGQ6Ly9tMjlrNW1rUWpnY3daYXJBLjhsQXRlNm1tREdha2lwamV0dUNpSDlZRjJmRjA6MTY3MzY6MDo5OTk5OTo3Ojo6Cg==
Decrypted Output: build://m29k5mkQjgcwZarA.8lAte6mmDGakipjetuCiH9YF2fF0:16736:0:99999:7:::
我面临的问题是解密字符串与提供给加密的输入字符串不同。我看到$
后的所有字符都被删除了。我可以知道$
在这里类似于什么以及如何保留整个字符串?
答案 0 :(得分:1)
如果要对Base 64字符串进行编码和解码,则应使用MIME::Base64
模块
代码看起来像这样
use strict;
use warnings;
use MIME::Base64;
my $input = 'build:$6$n2cR7cY/$aKv6qQRWjYMIQIyAdTsw6nPtMzEZoqHziqyrL9sn/m29k5mkQjgcwZarA.8lAte6mmDGakipjetuCiH9YF2fF0:16736:0:99999:7:::';
print "Input: $input\n\n";
my $enc = encode_base64($input);
print "Encrypted Output: $enc\n";
my $dec = decode_base64($enc);
print "Decrypted Output: $dec\n";
Input: build:$6$n2cR7cY/$aKv6qQRWjYMIQIyAdTsw6nPtMzEZoqHziqyrL9sn/m29k5mkQjgcwZarA.8lAte6mmDGakipjetuCiH9YF2fF0:16736:0:99999:7:::
Encrypted Output: YnVpbGQ6JDYkbjJjUjdjWS8kYUt2NnFRUldqWU1JUUl5QWRUc3c2blB0TXpFWm9xSHppcXlyTDlz
bi9tMjlrNW1rUWpnY3daYXJBLjhsQXRlNm1tREdha2lwamV0dUNpSDlZRjJmRjA6MTY3MzY6MDo5
OTk5OTo3Ojo6
Decrypted Output: build:$6$n2cR7cY/$aKv6qQRWjYMIQIyAdTsw6nPtMzEZoqHziqyrL9sn/m29k5mkQjgcwZarA.8lAte6mmDGakipjetuCiH9YF2fF0:16736:0:99999:7:::