Click to See Complete Forum and Search --> : file encryption+3des


nana_konyot
February 15th, 2005, 11:53 PM
i want to encrypt a file(any type of file) from my pc
-> save it with another name n
-> add my own ext to the file(eg *.eft)..

does anybody know how to do that using php pgrmmg???

act this is my class project..
i have to choose a filename (using browse button) from a webpage that i created..
enter the symm key for 3des algo...
and choose a directory where the encrypted file will be save..
but where to start??? pleassse help me....

bigBA
February 16th, 2005, 12:27 AM
well.. design a little html page with a form and a inputfield. you have to distinguish who you pass the file to the script: just the file name/path (then you server has to run on your local machine) or per file upload.

2. create a script which stands behind this page and encrypts the file. if you don't want to implement the encryption algorithm yourself have a look at this: http://www.php.net/manual/en/ref.mcrypt.php

what exactly is your problem? had your ever to deal with c? than file handling isn't much different... if you want, it is exactly the same :) (correct me if i'm wrong on this)

nana_konyot
February 16th, 2005, 01:53 AM
thanks for rplying..

rite now i try to dvlp a web based application..using php(just like a normal webpage) this appl will prvide a fnction for user to encrypt their file(.doc, .jpg etc)...and save the encrypted file on their local drctory...i just implmnt d exstng script..no need to build it myslf :-) is there any easiest wy i can do??(using web based app is a must!)

i'm new to php..how to read the user file content?should i read it in binary or what....hu3 :(

bigBA
February 16th, 2005, 02:32 AM
well.. so the web-server isn't running on the users machine....
so you have to upload the file. for this, have a look here: http://de.php.net/manual/en/features.file-upload.php

after this you can open the file with fopen and then read all bytes (and yes, i would specify the b(inary) flag).
then use fread (http://de.php.net/manual/en/function.fread.php) to read the contents of your file into a string (for example).
this string do you pass to your encryption function (maybe the mcrypt thing mentioned above) and after this you could write the encrypted data to a temporary file and provide a user a link to download the encrypted file. or you send the appropiate html headers and the content right away to the user.

maybe i don't get you problem... ask more specific if anything isn't clear.

you know how to write a basic php script and how to get the user's input from a html form?

bigBA
February 16th, 2005, 02:34 AM
i just implmnt d exstng script..no need to build it myslf :-)
what do you mean by that?
implementing an existing script? so it is implemented... are you to redesign it? or do you have to port something from (lets say) c++?

nana_konyot
February 16th, 2005, 06:27 AM
i have try ur instructions..
i used 2 textfields..
1. to enter the filename ($filename)
2. to enter the key($key)..
below is the code..

$filename=$HTTP_POST_VARS['filename'];
$key = $HTTP_POST_VARS['key'];
$handle = fopen($filename,"rb");
$contents = fread($handle, filesize($filename));
$ciphertext = des ($key, $contents, 1, 0, null); --> c attachment
fclose($handle);


it works with .txt file.thanks!! :-)
how about encrypting other files? eg gif,jpg,doc??
and how to read a filename entered if i use 'file' input instead of 'textfield' input..??
browse button in 'file' allows us to browse folder rite..
but when using txtfield..we must type the name correctly..

i found 3des scrpt wrtten in php...so what do u think about it??can use or not..


thanks a lot..

bigBA
February 16th, 2005, 07:57 AM
if you use the file type input, the file gets uploaded to your script. look at the link i posted above.

the method you used, only works, when the web-server/ the script gets executed on the machine the file lies....

bigBA
February 16th, 2005, 10:10 AM
regarding other file types:
no problem. basically text files are no different from any other 'binary' file. they all contain bits and bytes :)

nana_konyot
February 16th, 2005, 09:46 PM
hi again..
when i mntioned abt 'binary' file..i mean the binary value of the file when we read it..eg when A is read..mybe its bnry vlue is 0001 (just an eg..)

and..is there any class available for file encryption?? not text encryption....
or any class in php that can read msg in byte??

thanks in advance
:)

bigBA
February 17th, 2005, 03:19 AM
well.. as i said, if you specified the "b" flag with fopen, it should be regardless wether you read a "text" or binary file.
just read it.

for ready-to-use classes you may look at this post (http://www.codeguru.com/forum/showthread.php?t=313987).
cilu posted some usefull links for php-resources. maybe on one of these pages you'll find an encryption class.

but why don't you write one on your own, especially when you already have an encryption function?

and besides: binary value for A (ASCII or hex) isn't 0001 :p

bigBA
February 17th, 2005, 03:28 AM
i just encountered two functions which are (IMHO) very suitable for you:

file_get_contents (since PHP4)
file_put_contents (since PHP5)

the first one (can) gets you the while file into a string, the second one writes this string to a file.
and: they just want the filename.

but it is (nearly) the same if you used fopen (with "b" flag) - fread - fwrite - fclose.

bigBA
February 17th, 2005, 03:32 AM
i found 3des scrpt wrtten in php...so what do u think about it??can use or not..
well, i'm not familiar with des encryption... so i don't know if the algorithm is right.
and for my understanding, its hust des not 3des. but afaik, you just have to do des three times to get 3des (like its name implies :D ).

so maybe you try it out.
encrypt something with this code.
decrypt it with another program capable of decrypting des encrypted data.
see if the encrypted data do match.