473,387 Members | 3,820 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,387 software developers and data experts.

Unable to decrypt data with mcrypt ..

I am encrypting the data with blowfish CBC mode in C using openssl.
I am trying to decrypt the data with the key using PHP mycrypt.But it does not give me the actual data but some garbage text.I am able to decrypt and encrypt with mcrypt but if i encrypt with c i am unable to decrypt with PHP.
Mar 2 '10 #1
16 7099
Atli
5,058 Expert 4TB
Hey.

Not much we can do for you without seeing your code, I'm afraid.
Mar 2 '10 #2
Oh certainly..
I am using command line ssl to encrypt file using blowfish CBC mode.

The decryption code is using mcrypt.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //base 64 decoding the encrypted data.
  3. $data = base64_decode('BdRY7DN8iPDdjdsevowiZQ==');
  4. $key ='12345678';
  5. $iv ='1234';
  6.  
  7. $res2 = mcrypt_decrypt(MCRYPT_BLOWFISH,$key,$data,MCRYPT_MODE_CBC,$iv);
  8. echo $res2;
  9. ?>
  10.  
Regards
akaash
Mar 2 '10 #3
Any ideas?
regards
akaash
Mar 3 '10 #4
Atli
5,058 Expert 4TB
I can't see anything wrong with that, unless:
  1. Your $key or $iv are incorrect.
  2. You input data is invalid
  3. Your C code is doing something wrong. - Which, honestly, is a lot more likely than your PHP code being wrong.
  4. ... Nope, that's all I've got.
One thing, though. Why do you decrypt the data as Base64 before sending it to Mcrypt?
Mar 3 '10 #5
hi,,thanks for the input.
I am doing base64 decode because data that i get as encrypted is garbage type.doing base64 helps to make it easy to send.

Regards
sumit
Mar 4 '10 #6
Hello friends.I have been trying to encrypt data in C and decrypt in PHP.SO far no success.:(
I want to know if any of you have done encryption in C and PHP.
If yes,please let me know and how.
I encrypt data in c and when in decrypt in php,i get vague characters.
Regards
akaash
Mar 9 '10 #7
RedSon
5,000 Expert 4TB
Are you sure you are using the same encryption algorithim. If you encrypt something in C and write the encrypted text to a plain old text file, then you should be able to take that to any platform and decrypt it.

If you encrypted file that you encrypted in C will not decrypt using PHP then I would blame the user not the technology.
Mar 9 '10 #8
I do agree that but cross platform encryption has much more that just key and iv like padding,file format etc .
I want to know where i am missing those details and are there more to be counted.I guess person who has done this in php and C is the best judge.

regards all
akaash
Mar 9 '10 #9
RedSon
5,000 Expert 4TB
does it say that mcrypt_decrypt needs a $data value that has been base64_decoded?

That would be my first suspicion.
Mar 9 '10 #10
NO.no such error..in fact i the first letter of the block is visible.
say i encrypt text HeloheloHelohelo, I am able to get the first and 9th letter and rest is garbage text. It is able to decrypt the first byte of the block.
regards
Mar 10 '10 #11
RedSon
5,000 Expert 4TB
I would think that the first and 9th letter appearing as if they are decrypted is just a coincidence.
Mar 10 '10 #12
RedSon
5,000 Expert 4TB
BF encoded string = 'oYmlOpvn9335KhBzpB2ISh/CefMkAglV6YCPQzYhlXk='
secret key = "\xe2\xa4\xa5\x99(&\xa6\x105\x11\x89\xa7\xb7>&\x9a \xc4\xea\xd2\xbcLM\xb0\x91'Z\x84\xe4@\x05\xf3\xb5"

Code used to encrypt the string:

Expand|Select|Wrap|Line Numbers
  1. define Encode(c,s):
  2.   base64.b64encode(c.encrypt(pad(s)))
  3.  
  4. define pad(s):
  5.   s+(BLOCK_SIZE - length(s) % BLOCK_SIZE)*PADDING
  6.  
  7. define BLOCK_SIZE as 32 and PADDING as character '}'
  8.  
Method call examples:

Expand|Select|Wrap|Line Numbers
  1. pad("test")
  2. >>> 'test}}}}}}}}}}}}}}}}}}}}}}}}}}}}'
  3.  
  4. CipherObj = create new Blowfish object with argument (secret)
  5. >>> CipherObj = Blowfish object at 0x92f0430
  6.  
  7. encodedString = Encode(CipherObj, "some BF encrypted text")
  8. >>> encodedString = 'oYmlOpvn9335KhBzpB2ISh/CefMkAglV6YCPQzYhlXk='
  9.  
Now take my encodedString and secretkey and decrypt it using blowfish. See if you get "some BF encrypted text".

This was encrypted using python, but if anyone is interested in doing it in any other language post your code here.
Attached Files
File Type: txt secretkey.txt (32 Bytes, 438 views)
Mar 10 '10 #13
jkmyoung
2,057 Expert 2GB
Are you referring to the problem indicated in this message at http://php.net/manual/en/book.mcrypt.php?

When using 3DES between PHP and C#, it is to be noted that there are subtle differences that if not strictly observed, will result in annoying problem encrypt/decrypt data.

1), When using a 16 bytes key, php and c# generates total different outcome string. it seems that a 24 bytes key is required for php and c# to work alike.
2), php doesnt have a "padding" option, while c# has 3 (?). My work around is to add nulls i.e. chr(0) to the end of the source string to make its size times of 8, while in c#, PaddingMode.Zeros is required.
3) the key size has to be times of 8, in php, to make it work for c#.
Try using a 24 byte encryption key.
Mar 11 '10 #14
RedSon
5,000 Expert 4TB
jkmyong, how is this relevant?

TripleDES =/= Blowfish
Mar 11 '10 #15
jkmyoung
2,057 Expert 2GB
Oops. Would like to delete the post.

Anyways, OP please post the c code you're using for encryption. Maybe the problem is in the encryption.
Mar 12 '10 #16
Hello friends,
Thanks a lot for your response and interest.
I tried encrypting with the command line of ssl.While decrypting with php it threw error.Then i created functions to encrypt with c using EVP functions and it worked well.Just some issue like padding came which can be managed.
if you want the C code i can post that too.
Thanks to all
Akaash
Mar 22 '10 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Oliver Spiesshofer | last post by:
Hi, I am trying to load the LDAP and the MCrypt module under WinXP and PHP Version 4.3.7. The strange thing is that I have several other modules that work just fine: extension=php_bz2.dll...
7
by: Dica | last post by:
i've used the sample code from msdn to create an encyption/decryption assembly as found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp i'm...
1
by: Y Store Tools | last post by:
I've just started playing with mcrypt for encryption but not sure if I'll need to write my own. Basically I don't need anything advanced for protection but want to mask the data so it's not easily...
0
by: Chris Fink | last post by:
I have walked through all of the WSE 3 Hands on Labs and got everything working fine. When I create my own certificate and install it in the stores, my client application that is consuming my WSE...
0
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4. Using code from this newsgroup, I have a decrypt function, trying to decrypt data (encryped using a corresponding mcrypt function). What am I doing wrong? These are...
8
by: =?Utf-8?B?RGFuTQ==?= | last post by:
Can someone help with the following problem. I am sending an encrypted SOAP message to a .NET 2.0 + WSE 3.0 web service. When .NET attempts to decrypt the message it cannot read the private key...
3
by: =?UTF-8?Q?Ahmad_=E3=8B=A1_Baitalmal?= | last post by:
Hi, I'm having a hard time getting python-mcrypt extension to build. I installed libmcrypt with --prefix=/usr and I checked that the library exists -rwxr-xr-x 1 root wheel 352K Sep 19...
1
by: Elliot | last post by:
When decrypt the xml, output "Unable to retrieve the decryption key." Can anyone help me solve the problem? I got the code from http://msdn.microsoft.com/en-us/library/ms229746.aspx using...
0
by: FarooqRafiq | last post by:
Hi, My requirement is that a string is encrypted in VB.NET and sent to PHP, PHP decrypts the string (till here the logic is working) and then the PHP should encrtyp (where i am having problems)...
5
by: simon2x1 | last post by:
how can i decrypt password with i encryted with md5 i try using mcrypt it did not work <?PHP $sql = "SELECT password FROM user WHERE email='$email_to'"; $result = mysql_query($sql); $rows =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
0
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...

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.