473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Making Perl Crypt::CBC work with PHP mcrypt_cbc()

First of all, I know this is a PHP news group, so my question may not
be completely appropriate here since it involves some perl.

I have read this post that dealt with a similar problem that I am
having now:
http://groups.google.com/group/comp....se_thread/thre...

Summary of my problem:
I need to have a perl script that encrypts a string, write the output
to a file,
and then have a PHP script read the file, and decrypts it. I took most
of the code from this URL:

http://us2.php.net/manual/en/function.mcrypt-cbc.php

Below is my perl code used to encrypt

<perl>
#!/usr/bin/perl
use MIME::Base64;
use Crypt::CBC;
my $key = '012345678901234567890123456789';
my $iv = '12345678';
my $text = 'This is my plain text';
$cipher = Crypt::CBC->new({'literal_key' => 0,
'key' => $key,
'cipher' => 'Blowfish',
'iv' => $iv,
'padding' => 'null',
'prepend_iv' => 0});
$encrypted = $cipher->encrypt($text);
$encoded = encode_base64($encrypted);
open(FILE, ">encrypt.txt");
print FILE $encoded;
close(FILE);
</perl>

And here's my PHP script used to decrypt:

<?php
$key = '012345678901234567890123456789';
$iv = '12345678';
$lines = file('encrypt.txt');
$encoded = '';
foreach ($lines as $line) {
$encoded .= $line;
}

$encrypted = base64_decode($encoded);
$decrypted = mcrypt_cbc(MCRYPT_BLOWFISH, $key, $bin_encrypted,
MCRYPT_DECRYPT, $iv);
echo "decrypted : [$decrypted]";
exit();
?>

But the decryption doesn't seem to work... If I printed out the base64
encoded strings, they match. So I suspect it's something that I did
not do correctly either in the encryption or decryption of the message.

Any help is appreciated.

Jun 3 '06 #1
1 3713
Answer my own question...

The perl Crypt::CBC documentation states that the '-regenerate_key' and
'-prepent_iv' are deprecated, that's why I used '-literal_key' instead
of '-regenerate_key'.

However, when I switch back to use the old '-regenerate_key', I got a
different error:

If specified by -literal_key, then the key length must be equal to the
chosen cipher's key length of 56 bytes at ./blow.pl line 9

So I changed my key to something like this in both perl and PHP:

my $long_key = "0123456789012345678901234567890123456789012345678 9"
my $key = substr($long_key, 0, 56);

And while decrypting on the PHP end, I needed to use rtrim() on the
decrypted string.

After these changes everything is working.

Jun 3 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: | last post by:
I'm curious how these two functions work together (when crypt() uses a md5 algo). Currently I know they will produce different output; is this due to the salt being used behind-the-scenes in md5()?...
3
by: Marco Herrn | last post by:
I want to use a crypt function to store crypted passwords. These will be used to verify mail-user access. Now the crypt() function from the module crypt is only significant for the first 8...
0
by: Jonas | last post by:
I have the following perl program witch i use to encrypt a password file with. In perl 5.6 this program works like a charm but when trying it on the RED HAT EL 3 platform (taroon) is doesnt...
4
by: Piotr Turkowski | last post by:
Hi! I've got some code in Perl and I have to have it in C, but my knowlege of Perl is < 0 :-(, so I need your help. here's the code. Thanks in advance. decrypt.pl #!/usr/local/bin/perl...
0
by: aars | last post by:
Hello all, I am creating a user administration system where system administrator can activate services for a user, like webspace, a mail account or a subdomain. I now want to create a...
3
by: Shiraz | last post by:
Updated to the latest version of DBD-mysql using perl -MCPAN -e "install DBD-mysql" and now the calling mysql function r2() within perl work > $SQL_Text = "select r2() from dual " ; >...
2
by: vermarajeev | last post by:
Hi guys, I have written code to encrypt and decrypt files using perl script. Please help me to port below code to crypto++ library. //ENCRYPTION my $cipher = Crypt::CBC->new( -cipher =>...
3
by: vermarajeev | last post by:
Hello friends, I am new to Perl scripts. Please help me out.... I am using crypt::CBC to encrypt my files... during decryption process it goes through the code as follows if...
5
by: Peter Pei | last post by:
I am using things like "%-20s%-60s%-10s" in tkinter listbox to make it look like a table, with mono sized font like lucie system. But this does not work with data contains "Les misérables", because...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.