Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encrypt file in PHP, Decrypt in ASP. Any ideas?

Status
Not open for further replies.

monkey64

Technical User
Apr 27, 2008
69
GB
I need to Encrypt a file in PHP and Decrypt in ASP.
This is quite a loaded question, so I just wondered if anyone has any experience doing this or could point me in the right direction.

Using PGP would be a solution, but my PHP host does not allow access to the command line.

I have found an excellent page on PHP encryption here:


Problem is, I am not an ASP developer and need to impliment a cross language solution.
 
i doubt whether the host prevents you from accessing the command line via sys/exec etc.

the easiest solution is perhaps to use the zip function to compress and encrypt the archive. i have not tested this, but perhaps this code

Code:
<?php
define('MYZIPPASSWORD' , 'password');

function encryptFile($file){
	if (!is_file($file)) return false;
	$tempFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'zipArchive_' . uniqid('zipper', true) . '.zip';
	unlink($tempFile);
	exec("zip -P " . MYZIPPASSWORD . " $tempFile $file", $return, $output);
	if (is_file($tempFile)) return $tempFile;
	return false;
}
 
Thanks for that.

To use a zip file would make a lot of sense as it's not language dependant. I'm experimenting at the moment with some classes.
 
Aes is also not language dependent.

When you say classes I assume that my code above did not work for you. Can you let us know why or what errors you get so that other readers will know how to fix or to avoid?
 
Code:
<?

define('MYZIPPASSWORD' , 'password');

function encryptFile($file){
    if (!is_file($file)) return false;
    $tempFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'zipArchive_' . uniqid('zipper', true) . '.zip';
    unlink($tempFile);
    exec("zip -P " . MYZIPPASSWORD . " $tempFile $file", $return, $output);
    if (is_file($tempFile)) return $tempFile;
    return false;
}

encryptFile("test.txt");

?>
[/CODE>

I wasn't able to get the above code to work.
I didn't get any errors, but the .zip file was not created.
 
Have a variable capture the return code of the exec command. Chances are that it is non zero, indicating an error. I ran into problems with this trying to create a temporary file for use with PGP and it was a write permissions problem that gave me a return code 2. The answer was to use the /tmp directory which has generalized rw access.
 
Noway2 - that is correct. which is why I use the sys_get_temp_dir() call.

@monkey64, unless you capture the return value of the function you will not know where the file is located

Code:
$myNewZipFile = encryptFile($file); //must be absolute url or correct relative url to this script
if ($myNewZipFile == false){die('error creating zip');}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top