473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Encryption help Needed

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
3 4257
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Blake T. Garretson | last post by:
I want to save some sensitive data (passwords, PIN numbers, etc.) to disk in a secure manner in one of my programs. What is the easiest/best way to accomplish strong file encryption in Python? ...
14
by: Ray Cassick \(Home\) | last post by:
Ok, time to ask the question here.. I have been battling over this one for sometime now and just have to ask it. I have created a few classes that I use to act a security keys. These classes get...
2
by: B Maxey | last post by:
I have been working with encryption. And it seems to me that the IV and Key are the only things you need to decrypt my data. I can obfuscate, but my program still needs to 'call' the framework...
10
by: joshsackett | last post by:
I am starting an encryption project for my database and I'm performing some tests on decryption speed. A lot of my application queries use a LIKE parameter in the WHERE clause. To keep from...
7
by: Mark Rae | last post by:
Hi, Picking your collective brains again, this time regarding the storage of the key used in symmetric encryption. Let's say you have a requirement to add encryption to a C# project, so you...
11
by: John Williams | last post by:
I've written a simple program to do XOR encryption as my first foray into understanding how encryption works. The code compiles fine, however it segmentation faults on every run. using gdb to...
8
by: manmit.walia | last post by:
Hello Everyone, Long time ago, I posted a small problem I had about converting a VB6 program to C#. Well with the help with everyone I got it converted. But I overlooked something and don't...
3
by: john.f.klein | last post by:
I want to be able to contact my wife via video teleconferencing and see and talk to her and our new baby. For this purpose, I need software and hardware that can allow me to do with secure and...
9
by: Betikci Boris | last post by:
I get bored last night and wrote a script that uses xor for encrypt- decrypt, however it woks fine under linux 2.6.25, text and documents are ok, but fails on compressed files *.jpg, *.pdf , etc ....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.