Xigenere cypher
Posted on July 18, 2008
Filed Under Cryptography, Source code |
another old implementation of vigenere encryption combained with logical xor operator.
# Xignere:
-
## this script is an implementation of vignere encryption
-
### underlying xor logical operator mutated into Xignere
(was bored) -
#### useage: Xigenere-v1.0
-
<-e|-d>
-
-
if(@ARGV < 3)
-
{ &usage; }
-
-
if($ARGV[2] eq "-e")
-
{
-
$fileCont = loadFile($ARGV[0]);
-
$crypt = xigenereCrypt($fileCont,$ARGV[1]);
-
open(FILE,">$ARGV[0]");
-
print FILE $crypt;
-
print "Encryption completed, results saved to $ARGV[0]\n";
-
-
}
-
elsif($ARGV[2] eq "-d")
-
{
-
$fileCont = loadFile($ARGV[0]);
-
$decrypt = xigenereDecrypt($fileCont,$ARGV[1]);
-
open(FILE,">$ARGV[0]");
-
print FILE $decrypt;
-
print "Decryption completed, results saved to $ARGV[0]\n";
-
}
-
else
-
{ &usage; }
-
-
sub xigenereCrypt
-
{
-
($text,$key) = @_;
-
$textLen = length($text);
-
for($i = 0; $i < $textLen; $i++)
-
{
-
$k=0 if($k == length($key));
-
$ord = ord(substr($text,$i,1)) + ord(substr($key,$k,1));
-
$ord -= 255 if ($ord > 255);
-
$crypted .= chr($ord) xor substr($key,$k,1);
-
$k++;
-
}
-
return $crypted;
-
-
}
-
-
sub xigenereDecrypt
-
{
-
($text,$key) = @_;
-
$textLen = length($text);
-
for($i = 0; $i < $textLen; $i++){
-
$k=0 if($k == length($key));
-
$ord = ord(substr($text,$i,1)) - ord(substr($key,$k,1));
-
$ord += 255 if ($ord < 0);
-
$decrypted .= chr($ord) xor substr($key,$k,1);;
-
$k++;
-
}
-
return $decrypted;
-
}
-
-
sub loadFile
-
{
-
open(FILE,shift);
-
while($line = ) {
-
$content .= $line;
-
}
-
-
return $content;
-
}
-
-
sub usage
-
{
-
print "Usage:
-
<-e|-d>";
-
exit;
-
}
Comments
One Response to “Xigenere cypher”
Leave a Reply
blabla