Thursday, September 17, 2009

OpenSocialX::Shindig::Crypter long story

The long story is

we (Zorpia) were developing OpenSocial in last days. The first choice to build a opensocial container is Shindig.

unlucky, it ONLY has PHP and Java version. yeah, I do all my code in Perl, I know nothing about PHP or Java. (well, I know a little PHP now because I was writing Service package with PHP in last days, and I must say, PHP sucks.)

lucky, the Shindig author points me to an URL: Using Shindig in a non PHP or Java environment
that I can write the 'Application' section in Perl. It IS great, really very great.

so at last, I google around and wrote my last solution: OpenSocialX::Shindig::Crypter and the php lib BasicBlobCrypter.php

and now I'm happy to share it with your guys. if you have the same situation with me, you can try that module and tell me your feeling. :)

Thanks. (OK, it's not so long.)

Labels: , ,

OpenSocialX::Shindig::Crypter short story

the short story is that I released the OpenSocialX::Shindig::Crypter yesterday.

the short story is if you want to find a way that encrypt data by Perl and decrypt the encrypted data by PHP, you come to the right place.

Perl: crypt.pl
my $str = 'o=1&v=3&p=5';
my $cipher = Crypt::CBC->new( {
    'key' => 'length16length16',
    'cipher'=> 'Rijndael',
    'iv' => '1234567890abcdef',
    'literal_key' => 1,
    'padding' => 'null',
    'header' => 'none',
    keysize => 128/8
} );
my $encrypted = $cipher->encrypt($str);
print "encrypted: ".encode_base64($encrypted)."\n";
print "decrypted: ".$cipher->decrypt($encrypted)."\n";

my $hmac = Digest::SHA::hmac_sha1($encrypted, 'hmackey');
print "hmac: " . encode_base64($hmac) . "\n";
print "total: " . encode_base64($encrypted . $hmac) . "\n";
PHP: crypt.php
$str = 'o=1&v=3&p=5';
$encrypted = mcrypt_cbc(MCRYPT_RIJNDAEL_128, 'length16length16', $str, MCRYPT_ENCRYPT, '1234567890abcdef');
$decrypted = mcrypt_cbc(MCRYPT_RIJNDAEL_128, 'length16length16', $encrypted, MCRYPT_DECRYPT, '1234567890abcdef');
echo "encrypted: ".base64_encode($encrypted)."\n"; 
echo "decrypted: ".$decrypted."\n"; 

$blabla = hash_hmac('sha1', $encrypted, 'hmackey', 1);
echo "hmac: " . base64_encode($blabla) . "\n";
echo "total: " . base64_encode($encrypted . $blabla) . "\n";

both of them show the same output:
# perl crypt.pl
encrypted: rgPZe6swWb3t3yIoIZFQaQ==
decrypted: o=1&v=3&p=5
hmac: WLTGeyesYTUhwg6fjVqDudrKRz0=
total: rgPZe6swWb3t3yIoIZFQaVi0xnsnrGE1IcIOn41ag7naykc9
# php crypt.php
encrypted: rgPZe6swWb3t3yIoIZFQaQ==
decrypted: o=1&v=3&p=5
hmac: WLTGeyesYTUhwg6fjVqDudrKRz0=
total: rgPZe6swWb3t3yIoIZFQaVi0xnsnrGE1IcIOn41ag7naykc9

I'll post a long story later today.

Thanks.

Labels: , ,