473,785 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Encryption help Needed

I'm having trouble decrypting a file I encrypted and wrote to the
server - the following code displays the $decrypted_stri ng 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_i v(mcrypt_get_iv _size($cipher_a lg,
MCRYPT_MODE_ECB ), MCRYPT_RAND);

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

print "Decrypted string: $decrypted_stri ng";

Jul 16 '05 #1
3 4278
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_o pen ('rijndael-256', '', 'ofb', '');
$td = mcrypt_module_o pen ('rijndael-256', '', 'ecb', '');

/* Create the IV and determine the keysize length */
$iv = mcrypt_create_i v (mcrypt_enc_get _iv_size($td),
MCRYPT_DEV_RAND OM);
$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_generi c ($td, $encrypted);

/* Terminate decryption handle and close module */
mcrypt_generic_ deinit ($td);
mcrypt_module_c lose ($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*****@wander inghorse.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_i v(mcrypt_get_iv _size($cipher_a lg,
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","hel lo world !");

function enc($data) {
$td = mcrypt_module_o pen (MCRYPT_TripleD ES, "", MCRYPT_MODE_ECB ,
"");
$iv = mcrypt_create_i v (mcrypt_enc_get _iv_size ($td), MCRYPT_RAND);
$encdata = mcrypt_ecb (MCRYPT_TripleD ES,(Keysize), $data,
MCRYPT_ENCRYPT, $iv);
$hextext=bin2he x($encdata);
return $hextext;
}

function dec($data) {
$td = mcrypt_module_o pen (MCRYPT_TripleD ES, "", MCRYPT_MODE_ECB ,
"");
$iv = mcrypt_create_i v (mcrypt_enc_get _iv_size ($td), MCRYPT_RAND);
$dectext = trim(mcrypt_ecb (MCRYPT_TripleD ES,(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***@primemai l.com> a écrit dans le message de
news:i6******** *************** *********@4ax.c om...
I'm having trouble decrypting a file I encrypted and wrote to the
server - the following code displays the $decrypted_stri ng 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_i v(mcrypt_get_iv _size($cipher_a lg,
MCRYPT_MODE_ECB ), MCRYPT_RAND);

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

print "Decrypted string: $decrypted_stri ng";

Jul 16 '05 #3
Thanks - that worked great!!!!
On Sat, 2 Aug 2003 02:22:20 +0200, "ArSeNiK" <Ar*****@BisKot t.Com>
wrote:
hi mate,

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

DEFINE ("Keysize","hel lo world !");

function enc($data) {
$td = mcrypt_module_o pen (MCRYPT_TripleD ES, "", MCRYPT_MODE_ECB ,
"");
$iv = mcrypt_create_i v (mcrypt_enc_get _iv_size ($td), MCRYPT_RAND);
$encdata = mcrypt_ecb (MCRYPT_TripleD ES,(Keysize), $data,
MCRYPT_ENCRYPT , $iv);
$hextext=bin2he x($encdata);
return $hextext;
}

function dec($data) {
$td = mcrypt_module_o pen (MCRYPT_TripleD ES, "", MCRYPT_MODE_ECB ,
"");
$iv = mcrypt_create_i v (mcrypt_enc_get _iv_size ($td), MCRYPT_RAND);
$dectext = trim(mcrypt_ecb (MCRYPT_TripleD ES,(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***@primemai l.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_stri ng 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_i v(mcrypt_get_iv _size($cipher_a lg,
MCRYPT_MODE_ECB ), MCRYPT_RAND);

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

print "Decrypted string: $decrypted_stri ng";


Jul 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
4121
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? Any modern block cipher will do: AES, Blowfish, etc. I'm not looking for public key stuff; I just want to provide a pass-phrase. I found a few modules out there, but they seem to be all but abandoned. Most seem to have died several years ago. ...
14
1954
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 serialized using a binary formatter and then symmetrically encrypted. The app will deserialize them and use the contents to judge licensing capabilities, etc. Currently the license key and vectors are stored in the code. I don't like the idea...
2
254
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 objects in System.Security.Cryptography. And this call can be searched for. Then using a program like SoftIce or Heapwalker they can examine the object and its properties (namely the IV and Key). Having that they can break my encryption. Am...
10
7595
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 changing my application I am performing all the work on the back-end; creating views, triggers and UDFs to encrypt/decrypt the data. A problem has arisen around the LIKE parameter, though. Currently: SELECT SSN, FNAME, LNAME FROM USERS WHERE LNAME...
7
3870
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 might choose the TripleDESCryptoServiceProvider class: http://msdn2.microsoft.com/en-us/library/system.security.cryptography.tripledescryptoserviceprovider.aspx No worries - it doesn't present much of a challenge, the MSDN code samples
11
5050
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 debug it let me narrow the problem down to the Cipher function I think it faults at line 84 or 85. The program makes it's first read/cipher/write pass without issue but the second pass kills it. Using gdb to print the variables left showed me the...
8
2745
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 understand why it is doing this. Below is my code, I would be greatfull if someone can guide me through the right path or even help me solve this issue. Problem: The old tool which was written in VB6 works perfect. But I needed to convert this to C#...
3
2145
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 privacy. As soon we talk of video, anyone, from the ISP to anyone who can snoop, record and store the content for any latter mischief can work on it
9
4817
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 . I didn't test script on windows. Here is the code, please send me your views. <?php /* Mother Eye Chipper with PHP :), Licence:GPL,
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10341
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.