views:

425

answers:

3

How to encrypt/decrypt text from file with php?

A: 

You are going to want to use the mcrypt php library. It supports a wide variety of encryption schemes. You will probaly need to do a recompile. If you don't already have this exstension, you can install pgp, and shell out to the executable to run.

Byron Whitlock
+2  A: 
Mohammad
A: 

Here is a basic DES encryption

<?php

$key = 'yourSecretKey';
$plain_text = pkcs5_pad(file_get_contents('yourFile.txt'));

/* Open module, and create IV */
$td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC, '');
$key = substr($key, 0, mcrypt_enc_get_key_size($td));
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = mcrypt_create_iv($iv_size, MCRYPT_DEV_URANDOM);

/* Initialize encryption handle */
mcrypt_generic_init($td, $key, $iv);

/* Encrypt data */
$encrypted = mcrypt_generic($td, $plain_text);
mcrypt_generic_deinit($td);
file_put_contents('yourFile.txt.enc', $encrypted);
Lance Rushing