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
https://forums.phpfreaks.com/topic/267907-aes256/
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
https://forums.phpfreaks.com/topic/267907-aes256/#findComment-1374588
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
https://forums.phpfreaks.com/topic/267907-aes256/#findComment-1374592
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.