0

I'm quite new with PHP, i'm trying to make aes encryption on mp3 files, using IV from java and key from keystore, i recived key and iv in string form using BASE64 // System.out.println(DatatypeConverter.printBase64Binary(IV)); IV is here byte array. Here is part of my php code which shoudl encrypt file

$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));
fclose($handle);



$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CTR, '');

$key = 'K/JED6M3WiSwke2ZZHvDInbRkUCI5lLk292ti9Bazmw=';
$iv =  'AAAAAQQFBgcAAAAAAAAAAQ==';


// Encrypt
if (mcrypt_generic_init($cipher, $key, $iv) != -1)
{
    $encrypted = mcrypt_generic($cipher, $contents);
    mcrypt_generic_deinit($cipher);
    $info = pathinfo($file);
 $nowa = $info['dirname'] 
        . DIRECTORY_SEPARATOR 
        . $info['filename'] 
        . '.' 
        . "mp3enc";
        echo $nowa;
        file_put_contents($nowa,$encrypted);
 }

But encryption does not works, new file is not being created. anyway i dont see what im doing wrong, so if anyone has any suggestion i woudl be greatful for them

4
  • 1
    You've not actually said what the problem is. Commented May 21, 2014 at 16:30
  • I'm sorry, i have edited question, it does not create new encrypted file Commented May 21, 2014 at 16:34
  • OK. When you echo $nowa, does it look like it should? What is the return value from the file_put_contents() call? Does that whole if statement get called? Commented May 21, 2014 at 16:36
  • i recive errors that key is too long and iv is to short, but i was wondering is there any way to make it works with such input ? becouse im uusing it in my java app Commented May 21, 2014 at 17:55

1 Answer 1

1

Your key and iv seem to be base64encoded. Try decoding them before passing them to mcrypt_generic_init.

$key = base64_decode('K/JED6M3WiSwke2ZZHvDInbRkUCI5lLk292ti9Bazmw=');
$iv =  base64_decode('AAAAAQQFBgcAAAAAAAAAAQ==');

Also note, your binary .mp3 data will be padded with NULL characters so that it fits in the cipher and unless you handle this (which you are not doing) your decryption can never be binary safe. So rather than reinventing the wheel, I suggest using this PHP File encryption library.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.