469,275 Members | 1,802 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

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 6825
5,058 Expert 4TB

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';
  7. $res2 = mcrypt_decrypt(MCRYPT_BLOWFISH,$key,$data,MCRYPT_MODE_CBC,$iv);
  8. echo $res2;
  9. ?>
Mar 2 '10 #3
Any ideas?
Mar 3 '10 #4
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.

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.
Mar 9 '10 #7
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
Mar 9 '10 #9
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.
Mar 10 '10 #11
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
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)))
  4. define pad(s):
  5.   s+(BLOCK_SIZE - length(s) % BLOCK_SIZE)*PADDING
  7. define BLOCK_SIZE as 32 and PADDING as character '}'
Method call examples:

Expand|Select|Wrap|Line Numbers
  1. pad("test")
  2. >>> 'test}}}}}}}}}}}}}}}}}}}}}}}}}}}}'
  4. CipherObj = create new Blowfish object with argument (secret)
  5. >>> CipherObj = Blowfish object at 0x92f0430
  7. encodedString = Encode(CipherObj, "some BF encrypted text")
  8. >>> encodedString = 'oYmlOpvn9335KhBzpB2ISh/CefMkAglV6YCPQzYhlXk='
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, 402 views)
Mar 10 '10 #13
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
5,000 Expert 4TB
jkmyong, how is this relevant?

TripleDES =/= Blowfish
Mar 11 '10 #15
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
Mar 22 '10 #17

Post your reply

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

Similar topics

1 post views Thread by Oliver Spiesshofer | last post: by
1 post views Thread by Y Store Tools | last post: by
reply views Thread by laredotornado | last post: by
3 posts views Thread by =?UTF-8?Q?Ahmad_=E3=8B=A1_Baitalmal?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.