Jump to content

AES256


hackalive

Recommended Posts

Hi guys

 

Have thhis code to encrypt files

	$passphrase = 'My secret';

/* Turn a human readable passphrase
 * into a reproducable iv/key pair
 */
$iv = substr(md5('iv'.$passphrase, true), 0, ;
$key = substr(md5('pass1'.$passphrase, true) . 
			   md5('pass2'.$passphrase, true), 0, 24);
$opts = array('iv'=>$iv, 'key'=>$key);

$fp = fopen('secret-file.enc', 'wb');
stream_filter_append($fp, 'mcrypt.tripledes', STREAM_FILTER_WRITE, $opts);
fwrite($fp, 'Secret secret secret data');
fclose($fp);

 

However I want to be able to do it as guaranteed AES256, so I need the ley length etc to match the AES256 standard.

 

I tried this

<?php

$passphrase = 'My secret';

/* Turn a human readable passphrase
 * into a reproducable iv/key pair
 */
$iv = substr(md5('iv'.$passphrase, true), 0, ;
$key = substr(md5('pass1'.$passphrase, true) . 
			   md5('pass2'.$passphrase, true), 0, 24);
$opts = array('iv'=>$iv, 'key'=>$key);

$fp = fopen('secret-file.enc', 'wb');
stream_filter_append($fp, 'mcrypt.RIJNDAEL_256', STREAM_FILTER_WRITE, $opts);
fwrite($fp, 'Secret secret secret data');
fclose($fp);

 

But it stays could not locate or open encryption filter.

Link to comment
Share on other sites

Okay I now have this working - however I assume I will need todo file_get_conetents to encypt the actual file contents rather than create new content that is encrypted?

 

<?php

$passphrase = 'My secret';

/* Turn a human readable passphrase
 * into a reproducable iv/key pair
 */
$iv = substr(md5('iv'.$passphrase, true), 0, ;
$key = substr(md5('pass1'.$passphrase, true) . 
			   md5('pass2'.$passphrase, true), 0, 24);
$opts = array('iv'=>$iv, 'key'=>$key);

$fp = fopen('secret-file.enc', 'wb');
stream_filter_append($fp, 'mcrypt.rijndael-128', STREAM_FILTER_WRITE, $opts);
fwrite($fp, 'Secret secret secret data');
fclose($fp);

/* Turn a human readable passphrase
 * into a reproducable iv/key pair
 */
$iv = substr(md5('iv'.$passphrase, true), 0, ;
$key = substr(md5('pass1'.$passphrase, true) . 
			   md5('pass2'.$passphrase, true), 0, 24);
$opts = array('iv'=>$iv, 'key'=>$key);

$fp = fopen('secret-file.enc', 'rb');
stream_filter_append($fp, 'mdecrypt.rijndael-128', STREAM_FILTER_READ, $opts);
$data = rtrim(stream_get_contents($fp));
fclose($fp);

echo $data;
?>

 

So if people could just help on how to do the keys properly - i will need to store them somehow - probably MySQL so they can be used to decrypt - is is best to do a phrase like the PHP.net demos or specific exact keys?

Link to comment
Share on other sites

If someone wouldn't mind checking this and how do I make it for all files not just plain text?

 

<?php

$passphrase = 'My secret';

/* Turn a human readable passphrase
 * into a reproducable iv/key pair
 */
$iv = substr(md5('iv'.$passphrase, true), 0, ;
$key = substr(md5('pass1'.$passphrase, true) . 
			   md5('pass2'.$passphrase, true), 0, 24);

$key256 = '12345678901234561234567890123456';
$iv =  '1234567890123456';
$opts = array('iv'=>$iv, 'key'=>$key);

// $fp = fopen('secret-file2.enc', 'wb');

$myFile = 'secret-file.enc';

$fi = file_get_contents('secret-file.enc');

$fp = fopen($myFile, 'w+');

stream_filter_append($fp, 'mcrypt.rijndael-128', STREAM_FILTER_WRITE, $opts);
fwrite($fp, $fi);
fclose($fp);

 

 

Tell me if it is doing it correctly - it appears to be but appearances can be deceiving :)

 

Cheers

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.