Created
August 2, 2011 01:20
-
-
Save cesare/1119396 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/perl -w | |
use Crypt::Rijndael; | |
use Digest::SHA; | |
my $cleartext = "Here is some data for the coding"; # 32 bytes | |
my $iv = 'a2xhcgAAAAAAAAAA'; | |
my $hash_1 = Digest::SHA->new(256); # SHA256 | |
$hash_1->add("Nixnogen"); | |
my $cryptkey = $hash_1->digest; | |
sub encode { | |
my ($cryptkey, $iv, $cleartext) = @_; | |
my $cipher = new Crypt::Rijndael $cryptkey, Crypt::Rijndael::MODE_CBC; | |
$cipher->set_iv($iv); # we assume this will change for each cookie anyway. | |
my $encrypted = $cipher->encrypt($cleartext); | |
return $encrypted; | |
} | |
sub decode { | |
my ($cryptkey, $iv, $secretdata) = @_; | |
my $cipher = new Crypt::Rijndael $cryptkey, Crypt::Rijndael::MODE_CBC; | |
$cipher->set_iv($iv); | |
my $decrypted = $cipher->decrypt($secretdata); | |
return $decrypted; | |
} | |
sub flipLastByte { | |
my $original = shift; | |
my @bytes = unpack("C*", $original); | |
$bytes[-1] ^= 0xff; | |
return pack("C*", @bytes); | |
} | |
my $enc = encode($cryptkey, $iv, $cleartext); | |
my $dec = decode($cryptkey, $iv, $enc); | |
my $encFlipped = flipLastByte($enc); | |
my $decFlipped = decode($cryptkey, $iv, $encFlipped); | |
print "Encoded length " . length($enc) . "\n"; | |
print "Encoded (original): " . unpack("H*", $enc) . "\n"; | |
print "Decode original 32 bytes: $dec\n"; | |
print "Encoded (flipped) : " . unpack("H*", $encFlipped) . "\n"; | |
print "Decode flipped 32 bytes: $decFlipped\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment