View Single Post
cerbere
x10Hosting Member

Join Date: Nov 2007
Posts: 28
Credits: 721
cerbere is on a distinguished road
Quote  
09-24-2008, 04:13 AM
Re: I need a simple encryption algorithm

Here is a method I devised that fits my needs (it must have been
around since ENIAC). I XOR the key with packets of hex digits
from the the input string, using the binary value of the digits
(not the ASCII code). The hexcrypt function does both encryp-
tion and decryption.

Perl code :

Code:
$string = "0123456789ABCDEFBEBEBA0BAB0123CACA"; # any length
$keystr = "948D5E5"; # length <= 8

print "original  : \"$string\"   key = \"$keystr\"\n";
$enc = hexcrypt($string, $keystr);
print "encrypted : \"$enc\"\n";
$dec = hexcrypt($enc, $keystr);
print "decrypted : \"$dec\"\n";

sub hexcrypt
{
  my (@pac, $pacstr, $l, $pac, $pacenc, $format, $pacencstr);
  my @string = split(//, @_[0]); # the string to encrypt
  my $keystr = @_[1]; # the key
  my $key = hex($keystr); # the key in binary
  my $keylength = length($keystr);
  my $encstr = ""; # accumulator for returned string
  do
  {   
    @pac = splice(@string, 0, $keylength); # take out a packet of digits (or less at end)
    $pacstr = join("", @pac); # into a string
    $l = length($pacstr); 
    $pac = hex($pacstr); # convert to a 32-bit integer
    $pacenc = $pac ^ ($key >> ($keylength * 4 - 4 * $l)); # shift key right if $l < $keylength, and XOR
    $format = "%0" . sprintf("%.d", $l) . "X"; # we want the leading zeros
    $pacencstr = sprintf($format, $pacenc); # back into a string
    $encstr .= $pacencstr; # add to encrypted string
  }
  while scalar @string > 0;
return $encstr;  
}
Program output :

Code:
original  : "0123456789ABCDEFBEBEBA0BAB0123CACA"   key = "948D5E5"
encrypted : "95AE1B3EC17E287B33E0E3437EE4B74794"
decrypted : "0123456789ABCDEFBEBEBA0BAB0123CACA"

Last edited by cerbere; 09-24-2008 at 04:32 AM.
Reply With Quote
cerbere is offlineReport Post