enc.pl 2.95 KB
Newer Older
Guba Sandor committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
#http://www.nomachine.com/ar/view.php?ar_id=AR01C00125
use strict;


use Time::localtime;

$::numValidCharList = 85;
$::dummyString = "{{{{";

#
#FOR TEST
#
my $password = @ARGV[0];
print $password,"\n";
my $scrambled_string = scrambleString($password);
print $scrambled_string,"\n";

sub getvalidCharList
{
  my $pos = shift;
  my @validCharList =
  (
    "!",  "#", "\$",  "%",  "&",  "(", ")",  "*",  "+",  "-",
    ".",  "0",   "1",  "2",   "3",  "4",  "5",  "6", "7", "8",
    "9", ":",  ";",  "<",  ">",  "?",  "@",  "A",  "B", "C",
    "D",  "E",  "F",  "G",  "H",  "I",  "J",  "K",  "L", "M",
    "N", "O",  "P",  "Q",  "R",  "S",  "T", "U", "V", "W",
    "X",  "Y",  "Z",  "[", "]",  "_",  "a",  "b",  "c",  "d",
    "e",  "f",  "g",  "h",  "i",  "j",  "k",  "l",  "m",  "n",
    "o",  "p",  "q",  "r",  "s",  "t",  "u",  "v",  "w",  "x",
    "y",  "z",  "{",  "|",  "}"
  );
  return $validCharList[$pos];
}

sub encodePassword
{
  my $p = shift;
  my $sPass = ":";
  my $sTmp = "";


  if (!$p)
  {
    return "";
  }
  for (my $i = 0; $i < length($p); $i++)
  {
    my $c = substr($p,$i,1);
    my $a=ord($c);

    $sTmp=($a+$i+1).":";
    $sPass .=$sTmp;
    $sTmp = "";
  }

  return $sPass;
}

sub findCharInList
{
  my $c = shift;
  my $i = -1;

  for (my $j = 0; $j < $::numValidCharList; $j++)
  {
    my $randchar = getvalidCharList($j);
    if ($randchar eq $c)
    {
      $i = $j;
      return $i;
    }
  }


  return $i;
}


sub getRandomValidCharFromList
{
  my $tm = localtime;
  my $k = ($tm->sec);

  return getvalidCharList($k);
}


sub scrambleString
{
  my $s = shift;
  my $sRet = "";
  
  if (!$s)
  {
    return $s;
  }
  my $str = encodePassword($s);
  if (length($str) < 32)
  {
    $sRet .= $::dummyString;
  }

  for ( my $iR = (length($str) - 1); $iR >= 0; $iR--)
  {
    #
    #Reverse string.
    #
    $sRet .= substr($str,$iR,1);
  }

  if (length($sRet) < 32)
  {
    $sRet .= $::dummyString;
  }

  my $app=getRandomValidCharFromList();
  my $k=ord($app);
  my $l=$k + length($sRet) -2;
  $sRet= $app.$sRet;

  for (my $i1 = 1; $i1 < length($sRet); $i1++)
  {

    my $app2=substr($sRet,$i1,1);
    my $j = findCharInList($app2);
    if ($j == -1)
    {
      return $sRet;
    }
    my $i = ($j + $l * ($i1 + 1)) % $::numValidCharList;
    my $car=getvalidCharList($i);

    $sRet=substr_replace($sRet,$car,$i1,1);

  }

  my $c = (ord(getRandomValidCharFromList())) + 2;
  my $c2=chr($c);

  $sRet=$sRet.$c2;

  return URLEncode($sRet);
}

sub URLEncode 
{
  my $theURL = $_[0];
  $theURL =~ s/&/&amp;/g;
  $theURL =~ s/\"\"/&quot;/g;
  $theURL =~ s/\'/&#039;/g;
  $theURL =~ s/</&lt;/g;
  $theURL =~ s/>/&gt;/g;
  return $theURL;
}

sub substr_replace
{
  my $str = shift;
  my $ch = shift;
  my $pos = shift;
  my $qt = shift;
  
  my @list = split (//,$str);
  my $count = 0;
  my $tmp_str = '';
  foreach my $key(@list)
  {
    if ($count != $pos)
    {
      $tmp_str .= $key;
    }
    else
    {
      $tmp_str .= $ch;
    }
    $count++;
  }
  return $tmp_str;
}