Unable to decrypt data with mcrypt ..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akaash19
    New Member
    • Mar 2010
    • 13

    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.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    Not much we can do for you without seeing your code, I'm afraid.

    Comment

    • akaash19
      New Member
      • Mar 2010
      • 13

      #3
      Oh certainly..
      I am using command line ssl to encrypt file using blowfish CBC mode.

      The decryption code is using mcrypt.
      Code:
      <?php
      //base 64 decoding the encrypted data.
      $data = base64_decode('BdRY7DN8iPDdjdsevowiZQ==');
      $key ='12345678';
      $iv ='1234';
      
      $res2 = mcrypt_decrypt(MCRYPT_BLOWFISH,$key,$data,MCRYPT_MODE_CBC,$iv);
      echo $res2;
      ?>
      Regards
      akaash
      Last edited by numberwhun; Mar 2 '10, 12:59 PM. Reason: Please use code tags!

      Comment

      • akaash19
        New Member
        • Mar 2010
        • 13

        #4
        Any ideas?

        Any ideas?
        regards
        akaash

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          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?

          Comment

          • akaash19
            New Member
            • Mar 2010
            • 13

            #6
            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

            Comment

            • akaash19
              New Member
              • Mar 2010
              • 13

              #7
              Cross Platform encryption in C and php

              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

              Comment

              • RedSon
                Recognized Expert Expert
                • Jan 2007
                • 4980

                #8
                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.

                Comment

                • akaash19
                  New Member
                  • Mar 2010
                  • 13

                  #9
                  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

                  Comment

                  • RedSon
                    Recognized Expert Expert
                    • Jan 2007
                    • 4980

                    #10
                    does it say that mcrypt_decrypt needs a $data value that has been base64_decoded?

                    That would be my first suspicion.

                    Comment

                    • akaash19
                      New Member
                      • Mar 2010
                      • 13

                      #11
                      NO.no such error..in fact i the first letter of the block is visible.
                      say i encrypt text HeloheloHelohel o, 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

                      Comment

                      • RedSon
                        Recognized Expert Expert
                        • Jan 2007
                        • 4980

                        #12
                        I would think that the first and 9th letter appearing as if they are decrypted is just a coincidence.

                        Comment

                        • RedSon
                          Recognized Expert Expert
                          • Jan 2007
                          • 4980

                          #13
                          BF encoded string = 'oYmlOpvn9335Kh BzpB2ISh/CefMkAglV6YCPQz YhlXk='
                          secret key = "\xe2\xa4\xa5\x 99(&\xa6\x105\x 11\x89\xa7\xb7> &\x9a\xc4\xea\x d2\xbcLM\xb0\x9 1'Z\x84\xe4@\x0 5\xf3\xb5"

                          Code used to encrypt the string:

                          Code:
                          define Encode(c,s):
                            base64.b64encode(c.encrypt(pad(s)))
                          
                          define pad(s):
                            s+(BLOCK_SIZE - length(s) % BLOCK_SIZE)*PADDING
                          
                          define BLOCK_SIZE as 32 and PADDING as character '}'
                          Method call examples:

                          Code:
                          pad("test")
                          >>> 'test}}}}}}}}}}}}}}}}}}}}}}}}}}}}'
                          
                          CipherObj = create new Blowfish object with argument (secret)
                          >>> CipherObj = Blowfish object at 0x92f0430
                          
                          encodedString = Encode(CipherObj, "some BF encrypted text")
                          >>> 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

                          Comment

                          • jkmyoung
                            Recognized Expert Top Contributor
                            • Mar 2006
                            • 2057

                            #14
                            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.Zer os 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.

                            Comment

                            • RedSon
                              Recognized Expert Expert
                              • Jan 2007
                              • 4980

                              #15
                              jkmyong, how is this relevant?

                              TripleDES =/= Blowfish

                              Comment

                              Working...