note 109490 modified in function.mcrypt-cbc by danbrown

From: Date: Mon, 30 Jul 2012 15:58:39 +0000
Subject: note 109490 modified in function.mcrypt-cbc by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-189511@lists.php.net to get a copy of this message
The PERL libraries have changed a little bit and getting PHP and PERL to mcrypt together can be confusing, so here is a current example;

PHP
===
<?php
$string = 'Some Secret thing I want to encrypt';
$iv = '12345678';
$passphrase = '8chrsLng';

$encryptedString = encryptString($string, $passphrase, $iv);
// Expect: 7DjnpOXG+FrUaOuc8x6vyrkk3atSiAf425ly5KpG7lOYgwouw2UATw== 

function encryptString($unencryptedText, $passphrase, $iv) {
 $enc = mcrypt_encrypt(MCRYPT_BLOWFISH, $passphrase, $unencryptedText, MCRYPT_MODE_CBC, $iv);
 return base64_encode($enc);
}
?>

PERL
====
$encryptedString = '7DjnpOXG+FrUaOuc8x6vyrkk3atSiAf425ly5KpG7lOYgwouw2UATw==';
$iv = '12345678';
$passphrase = '8chrsLng';

$string = &decryptPhpEncrypted $encryptedString, $passphrase, $iv;
# Expect: Some Secret thing I want to encrypt

sub decryptPhpEncrypted() {
 my ($encryptedString, $passphrase, $iv) = @_;
 my $keysize = length($passphrase);
 use Crypt::CBC;
 $cipher = Crypt::CBC->new( {'key' => $encryptedString,
 'cipher'=> 'Blowfish',
 'iv' => $iv,
 'keysize' => $keysize,
 'regenerate_key' => 0,
 'padding' => 'null',
 'prepend_iv' => 0});

 return $cipher->decrypt($encryptedString);
} --was-- The PERL libraries have changed a little bit and getting PHP and PERL to mcrypt together can be confusing, so here is a current example; PHP === $string = 'Some Secret thing I want to encrypt'; $iv = '12345678'; $passphrase = '8chrsLng'; $encryptedString = encryptString($string, $passphrase, $iv); // Expect: 7DjnpOXG+FrUaOuc8x6vyrkk3atSiAf425ly5KpG7lOYgwouw2UATw== function encryptString($unencryptedText, $passphrase, $iv) { $enc = mcrypt_encrypt(MCRYPT_BLOWFISH, $passphrase, $unencryptedText, MCRYPT_MODE_CBC, $iv); return base64_encode($enc); } PERL ==== $encryptedString = '7DjnpOXG+FrUaOuc8x6vyrkk3atSiAf425ly5KpG7lOYgwouw2UATw=='; $iv = '12345678'; $passphrase = '8chrsLng'; $string = &decryptPhpEncrypted $encryptedString, $passphrase, $iv; # Expect: Some Secret thing I want to encrypt sub decryptPhpEncrypted() { my ($encryptedString, $passphrase, $iv) = @_; my $keysize = length($passphrase); use Crypt::CBC; $cipher = Crypt::CBC->new( {'key' => $encryptedString, 'cipher'=> 'Blowfish', 'iv' => $iv, 'keysize' => $keysize, 'regenerate_key' => 0, 'padding' => 'null', 'prepend_iv' => 0}); return $cipher->decrypt($encryptedString); } http://php.net/manual/en/function.mcrypt-cbc.php

« previous php.notes (#189511) next »