user4313475
user4313475

Reputation:

Stuck on Converting a JS function to Perl

Im trying to convert this: (JS function) to 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;
};

This is as far as I have come: Im not sure how to the strOut and correct 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];

Upvotes: 0

Views: 85

Answers (1)

ikegami
ikegami

Reputation: 385655

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

Upvotes: 2

Related Questions