3

My perl script is not to decrypt a AES encrypted string in php

I think I am missing something here , Are there any sample scripts available to decrypt in perl with an IV

This is the Encrypt script in php

<?php
  $encryption_key = base64_decode('OhjsqEflVL1GNteBIgpD1ngMvS3vVicAkfTyerJjr/c=');
  $iv = "1234567812345678";
  $data = "PLAIN TEXT";
  $encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, OPENSSL_RAW_DATA, $iv);
  print "Encrypted string = <".base64_encode($encrypted).">\n";

  $decrypted = openssl_decrypt($encrypted,'aes-256-cbc', $encryption_key, OPENSSL_RAW_DATA , $iv);
  print "DECRYPTED DATA=<$decrypted>\n";
?>

This is the decrypt script in perl

use Crypt::CBC;
use Crypt::Cipher::AES;
use MIME::Base64 qw(decode_base64);
use strict;
my $key=decode_base64('OhjsqEflVL1GNteBIgpD1ngMvS3vVicAkfTyerJjr/c=')
my $iv = "1234567812345678";
$iv=undef;
my $cbc = Crypt::CBC->new( -cipher=>'Cipher::AES', -key=>$key, -iv=>$iv );
my $plaintext = $cbc->decrypt(decode_base64('sR9aVLpjHgpbM7Kw8hb7Ig=='));
print "$plaintext\n";
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Ram
  • 1,155
  • 13
  • 34
  • 3
    If I specify -literal_key => 1, -header=>'none' it seems to work But is that the right thing to do – Ram Aug 16 '17 at 11:06

1 Answers1

0

Your code comes from http://search.cpan.org/dist/CryptX/lib/Crypt/Cipher/AES.pm

But there is an error. It gave me

 cannot use salt-based IV generation if literal IV is specified

CBC module gives a code which works.

I saw you have added options taken from Crypt CBC module documentation, it works fine for me too.

Plaute
  • 4,669
  • 2
  • 17
  • 19