sQuo Posted February 19, 2014 Share Posted February 19, 2014 [LENGUAJE=perl]#!/usr/bin/perl $ENV{'PATH'} = 'MY WEB SERVER PATH -_^ '; use strict; use warnings; use Crypt::CBC; use Crypt::Rijndael; use Data::Entropy::Algorithms qw(rand_bits rand_int rand_prob); use Digest::SHA; use MIME::Base64; use MIME::Base64::URLSafe; use Encode qw(encode); my $iv = rand_bits(128); my $data = "plain text stuff over there and here because.. HWY NAWT?!"; print "Original Text: $data\n"; my $key = "encryption key"; print "Original Key: $key\n"; encode("UTF-8",$key); my @ascii = unpack("C*", $key); print "UTF-8 : "; foreach my $i (@ascii) { print "$i "; } print "\n"; my $sha = Digest::SHA->new; $sha->add(@ascii); my $shaedString; my @shaed = unpack("c*",$sha->digest); print "SHA-1 Key Bytes : "; foreach my $i (@shaed) { print "$i "; $shaedString .= $i; } print "\n"; my $newKey = pack("b128", join("", $sha->digest())); my $cipher = Crypt::Rijndael->new($newKey, Crypt::Rijndael::MODE_CBC()); $cipher->set_iv($iv); encode("UTF-8",$data); my $data_16 = get16($data); my $ciphertext = $cipher->encrypt($data_16); my $fullData = $iv.$ciphertext; my $ciphertextB64 = urlsafe_b64encode($fullData); chomp($ciphertextB64); print "Encoded Encrypted Text: $ciphertextB64\n"; ## DECODING my $cipher2 = Crypt::Rijndael->new($newKey, Crypt::Rijndael::MODE_CBC()); my $decodedtextB64 = urlsafe_b64decode($ciphertextB64); chomp($decodedtextB64); my @ivarray = unpack("c*", $iv); print "iv : "; foreach my $i (@ivarray) { print "$i "; } print "\n"; my @decodeData = unpack("c*",$decodedtextB64); my $datar; print "iv2: "; for (my $i=0; $i $datar .= $decodeData[$i]; print "$decodeData[$i] "; } print "\n"; my $decodeData2 = unpack("a*",$decodedtextB64); my $iv2 = substr($decodeData2, 0, 16); my $datarSize = length($decodeData2); my $outPut = substr($decodeData2, 16, $datarSize); my $size = length($decodeData2); my $ciphertext2 = substr($decodeData2, 16, $size); $cipher2->set_iv($iv2); my @iv21array = unpack("c*", $iv2); print "ivM: "; foreach my $i (@iv21array) { print "$i "; } print "\n"; my $plaintext = $cipher2->decrypt($ciphertext2); print "Decrypted Text: $plaintext\n"; sub get16 { my $data = shift; return $data . "\0" x ( 16 - length($data)%16 ); }[/LENGUAJE] Link to comment Share on other sites More sharing options...
Recommended Posts