Skip to content
Advertisement

Encrypting / Decrypting file with Mcrypt

Trying to write a couple of functions that will encrypt or decrypt a file and am using the class found here to try and accomplish this:

http://www.itnewb.com/v/PHP-Encryption-Decryption-Using-the-MCrypt-Library-libmcrypt

The encryption function below seems to work, in that it appears to encrypt the file and place it in the intended directory. I’m trying to decrypt the file now, and it just dies with the message “Failed to complete decryption” (which is coded in there…) There’s nothing in the php error logs, so I’m not sure why it’s failing, but as mcrypt is entirely new to me, I’m more than inclined to believe I’m doing something wrong here…

Here are the functions:

JavaScript

Advertisement

Answer

Since mcrypt is abandonware and no longer recommended to be used, here’s an example using openssl.

JavaScript

Output:

JavaScript

Old Answer

Try this PHP5 class for encryption using mcrypt. In this case it’s using AES encryption. You’ll want to change the key for each site you use it on. If you don’t use it at least it may guide you on writing your own version of it.

JavaScript

Usage:

JavaScript

Notes:

  • This class is not safe for use with binary data (which may end in NUL bytes)
  • This class does not provide authenticated encryption.
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement