By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,890 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,890 IT Pros & Developers. It's quick & easy.

Encryption help Needed

P: n/a
I'm having trouble decrypting a file I encrypted and wrote to the
server - the following code displays the $decrypted_string variable
but the data is still encrypted - any help would be appreciated:

$handle = fopen("/home/public_html/testfolder/test.txt","r");
$buffer = fgets($handle, 4096);
fclose($handle);

// Encryption/decryption key
$key = "twenty one years ago";

// Encryption Algorithm
$cipher_alg = MCRYPT_RIJNDAEL_128;

// Create the initialization vector for added security.
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,
MCRYPT_MODE_ECB), MCRYPT_RAND);

$decrypted_string = mcrypt_decrypt($cipher_alg, $key, $buffer,
MCRYPT_MODE_CBC, $iv);

print "Decrypted string: $decrypted_string";

Jul 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Yes they are both defined - I did the echo for both and they displayed
on the web page.

I was wondering if maybe that iv parameter is the problem? does it
generate one data when I encrypt it and then another when I decrypt
it? The reason I wonder is that I can encrypt and decrypt
successfully if I do it in the same php script but if I encrypt only,
save the encryption string to disk and then use another php script to
open the file and decrypt it, it doesn't decrypt, it stays
scrambled???

If you run this you'll see that it works to encrypt and decrypt but if
I try to use just the part to encrypt and put the parts to decrypt
only in another script it won't decrypt:

/* Open the cipher */
// $td = mcrypt_module_open ('rijndael-256', '', 'ofb', '');
$td = mcrypt_module_open ('rijndael-256', '', 'ecb', '');

/* Create the IV and determine the keysize length */
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td),
MCRYPT_DEV_RANDOM);
$ks = mcrypt_enc_get_key_size ($td);

/* Create key */
$key = "follow the yellow brick road";

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

/* Encrypt data */
$encrypted = mcrypt_generic ($td, 'secret 2 password');

/* Terminate encryption handler */
mcrypt_generic_deinit ($td);

/* Initialize encryption module for decryption */
mcrypt_generic_init ($td, $key, $iv);

/* Decrypt encrypted string */
$decrypted = mdecrypt_generic ($td, $encrypted);

/* Terminate decryption handle and close module */
mcrypt_generic_deinit ($td);
mcrypt_module_close ($td);

/* Show string */
print trim ($decrypted)."\n";
$handle = fopen("/home/rfresh/public_html/ssfiles/mysqlpw.enc","w");
fputs($handle, $encrypted. "\n");
fclose($handle);

On Fri, 01 Aug 2003 10:37:08 +0200, stephan beal
<st*****@wanderinghorse.net> wrote:
Ralph Freshour wrote:
$cipher_alg = MCRYPT_RIJNDAEL_128;


Are you 100% that MCRYPT_RIJNDAEL_128 is defined for your system? Try:

echo MCRYPT_RIJNDAEL_128;
// Create the initialization vector for added security.
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,
MCRYPT_MODE_ECB), MCRYPT_RAND);


Same for MCRYPT_MODE_ECB.

i ask because certain constants are only defined is specific libs are linked
in or if PHP was configured with them.


Jul 16 '05 #2

P: n/a
hi mate,

here is some func to encode / decode just change cypher type :

DEFINE ("Keysize","hello world !");

function enc($data) {
$td = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB,
"");
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
$encdata = mcrypt_ecb (MCRYPT_TripleDES,(Keysize), $data,
MCRYPT_ENCRYPT, $iv);
$hextext=bin2hex($encdata);
return $hextext;
}

function dec($data) {
$td = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB,
"");
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
$dectext = trim(mcrypt_ecb (MCRYPT_TripleDES,(Keysize),
hex2bin($data), MCRYPT_DECRYPT,$iv));

return $dectext;
}

function hex2bin($data) {
$len = strlen($data);
return pack("H" . $len, $data);
}

hope it help :)

++
"Ralph Freshour" <ra***@primemail.com> a écrit dans le message de
news:i6********************************@4ax.com...
I'm having trouble decrypting a file I encrypted and wrote to the
server - the following code displays the $decrypted_string variable
but the data is still encrypted - any help would be appreciated:

$handle = fopen("/home/public_html/testfolder/test.txt","r");
$buffer = fgets($handle, 4096);
fclose($handle);

// Encryption/decryption key
$key = "twenty one years ago";

// Encryption Algorithm
$cipher_alg = MCRYPT_RIJNDAEL_128;

// Create the initialization vector for added security.
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,
MCRYPT_MODE_ECB), MCRYPT_RAND);

$decrypted_string = mcrypt_decrypt($cipher_alg, $key, $buffer,
MCRYPT_MODE_CBC, $iv);

print "Decrypted string: $decrypted_string";

Jul 16 '05 #3

P: n/a
Thanks - that worked great!!!!
On Sat, 2 Aug 2003 02:22:20 +0200, "ArSeNiK" <Ar*****@BisKott.Com>
wrote:
hi mate,

here is some func to encode / decode just change cypher type :

DEFINE ("Keysize","hello world !");

function enc($data) {
$td = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB,
"");
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
$encdata = mcrypt_ecb (MCRYPT_TripleDES,(Keysize), $data,
MCRYPT_ENCRYPT, $iv);
$hextext=bin2hex($encdata);
return $hextext;
}

function dec($data) {
$td = mcrypt_module_open (MCRYPT_TripleDES, "", MCRYPT_MODE_ECB,
"");
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
$dectext = trim(mcrypt_ecb (MCRYPT_TripleDES,(Keysize),
hex2bin($data), MCRYPT_DECRYPT,$iv));

return $dectext;
}

function hex2bin($data) {
$len = strlen($data);
return pack("H" . $len, $data);
}

hope it help :)

++
"Ralph Freshour" <ra***@primemail.com> a écrit dans le message de
news:i6********************************@4ax.com.. .
I'm having trouble decrypting a file I encrypted and wrote to the
server - the following code displays the $decrypted_string variable
but the data is still encrypted - any help would be appreciated:

$handle = fopen("/home/public_html/testfolder/test.txt","r");
$buffer = fgets($handle, 4096);
fclose($handle);

// Encryption/decryption key
$key = "twenty one years ago";

// Encryption Algorithm
$cipher_alg = MCRYPT_RIJNDAEL_128;

// Create the initialization vector for added security.
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,
MCRYPT_MODE_ECB), MCRYPT_RAND);

$decrypted_string = mcrypt_decrypt($cipher_alg, $key, $buffer,
MCRYPT_MODE_CBC, $iv);

print "Decrypted string: $decrypted_string";


Jul 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.