Question

I couldn't find enough resource about this subject and i need to learn how can i encrypt and decrypt file using TripleDes in PHP (while uploading it should encrypt the file and while downloading the file it should decrypt).

Also i found some examples but i couldn't implement it http://php.net/manual/en/mcrypt.examples.php http://stackoverflow.com/questions/10548386/issue-with-encrypt-and-decrypt-a-word-docx-file-in-php

Thanks for your interests.

Was it helpful?

Solution

You can use this code to encrypt a string:

$buffer = $file; 
// get the amount of bytes to pad
$extra = 8 - (strlen($buffer) % 8);
// add the zero padding
if($extra > 0) {
    for($i = 0; $i < $extra; $i++) {
        $buffer .= "\0";
    }
}
// very simple ASCII key and IV
$key = "passwordDR0wSS@P6660juht";
$iv = "password";
// hex encode the return value
$encrypted_file = mcrypt_cbc(MCRYPT_3DES, $key, $buffer, MCRYPT_ENCRYPT, $iv);

And this to decrypt it:

$decrypted_file = mcrypt_cbc(MCRYPT_3DES, $key, $encrypted_file, MCRYPT_DECRYPT, $iv);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top