Xigenere cypher

Posted on July 18, 2008
Filed Under Cryptography, Source code |

another old implementation of vigenere encryption combained with logical xor operator.

# Xignere:
  1. ## this script is an implementation of vignere encryption
  2. ### underlying xor logical operator  mutated into Xignere :) (was bored)
  3. #### useage: Xigenere-v1.0
  4.   <-e|-d>
  5.  
  6. if(@ARGV < 3)
  7. { &usage;  }
  8.  
  9. if($ARGV[2] eq "-e")
  10. {
  11.  $fileCont = loadFile($ARGV[0]);
  12.  $crypt = xigenereCrypt($fileCont,$ARGV[1]);
  13.  open(FILE,">$ARGV[0]");
  14.  print FILE $crypt;
  15.  print "Encryption completed, results saved to $ARGV[0]\n";
  16.  
  17. }
  18. elsif($ARGV[2] eq "-d")
  19. {
  20.  $fileCont = loadFile($ARGV[0]);
  21.  $decrypt = xigenereDecrypt($fileCont,$ARGV[1]);
  22.  open(FILE,">$ARGV[0]");
  23.  print FILE $decrypt;
  24.  print "Decryption completed, results saved to $ARGV[0]\n";
  25. }
  26. else
  27. { &usage; }
  28.  
  29. sub xigenereCrypt
  30. {
  31.  ($text,$key) = @_;
  32.  $textLen = length($text);
  33.  for($i = 0; $i < $textLen; $i++)
  34.  {
  35.   $k=0 if($k == length($key));
  36.   $ord = ord(substr($text,$i,1)) + ord(substr($key,$k,1));
  37.   $ord -=  255 if ($ord > 255);
  38.   $crypted .= chr($ord) xor substr($key,$k,1);
  39.   $k++;
  40.  }
  41.  return $crypted;
  42.  
  43. }
  44.  
  45. sub xigenereDecrypt
  46. {
  47.  ($text,$key) = @_;
  48.  $textLen = length($text);
  49.  for($i = 0; $i < $textLen; $i++){
  50.   $k=0 if($k == length($key));
  51.   $ord = ord(substr($text,$i,1)) - ord(substr($key,$k,1));
  52.   $ord += 255 if ($ord < 0);
  53.   $decrypted .= chr($ord) xor substr($key,$k,1);;
  54.   $k++;
  55.  }
  56.  return $decrypted;
  57. }
  58.  
  59. sub loadFile
  60. {
  61.  open(FILE,shift);
  62.  while($line = ) {
  63.   $content .= $line;
  64.  }
  65.  
  66.  return $content;
  67. }
  68.  
  69. sub usage
  70. {
  71.  print "Usage:
  72.  <-e|-d>";
  73.  exit;
  74. }

Comments

One Response to “Xigenere cypher”

  1. Exodus on August 6th, 2008 12:45 pm

    blabla

Leave a Reply