坚持将JS函数转换为Perl

时间:2014-12-01 22:29:32

标签: javascript perl

我试图将此:(JS函数)转换为Perl。

Decrypt3 = function (strIn, strKey) {
  var strOut = new String();
  var lenIn = strIn.length;
  var lenKey = strKey.length;
  var i = 0;
  var numIn;
  var numKey;
  while (i < lenIn) {
    numIn = parseInt(strIn.substr(i, 2), 32);
    numKey = strKey.charCodeAt(i / 2 % lenKey);
    strOut += String.fromCharCode(numIn - numKey);
    i += 2;
  }
  return strOut;
};

据我所知: 我不知道如何使用strOut并纠正NumKey。

    while (<>) {
  chomp;
  print Decrypt3($_, $key),"\n";
}


sub Decrypt3 {
    my @str_in  = unpack 'C*', shift;
    my @str_key = unpack 'C*', shift;
    my @str_out;

    for my $i (0 .. $str_in) {
        my $numin = int[$str_in[ord[$i 2], 32]
        my $sum = $str_in[$i] + $str_key[$i / 2% @str_key];

1 个答案:

答案 0 :(得分:2)

use List::Util 1.29 qw( pairmap );

my @base32_syms = (0..9, 'a'..'v');
my %base32_sym_vals =
   map {
      lc($base32_syms[$_]) => $_,
      uc($base32_syms[$_]) => $_,
   }
      0..$#base32_syms;

sub decrypt3 {
   my @cypher =
      pairmap { $base32_sym_vals{$a} * 32 + $base32_sym_vals{$b} }
         split(//, shift);

   my @key = unpack('C*', shift);

   return
      join ' ',
         map { chr($cypher[$_] - $key[ $_ % @key ]) }
            0..$#cypher;
}