SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AES_ENCRYPT in MySQL and PHP encryption are not giving the same output.

    hi all,

    Here i am getting problem with encryption of data using PHP and MySQL. Actually we need to encrypt the data with 256 bit. So we have downloaded and compiled the source of MySQL and installed in CentOS. The problem is, suppose we encrypt the data using the PHP function(mcrypt_encrypt() with 256 bit) and if we decrypt the same data using MySQL decrypt function(AES_DECRYPT()) it is not giving the same output. Can you please suggest how to proceed?

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,400
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Choose to do both the encrypting and decrypting with PHP or MySQL.

  3. #3
    secure webapps for all Aleksejs's Avatar
    Join Date
    Apr 2008
    Location
    Riga, Latvia
    Posts
    755
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suspect that MYSQL uses ECB cipher mode, while mcrypt_ecrypt() uses other block cipher mode by default.

  4. #4
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow

    Even I too used the ECB in mycrypt_encrypt() function but the output is deferring.

    Code snippet:

    For Decryption:

    PHP Code:
    $iIVSize mcrypt_get_iv_sizeMCRYPT_RIJNDAEL_256MCRYPT_MODE_ECB );
    $strIV mcrypt_create_iv$iIVSizeMCRYPT_RAND );
    $strPlaneText mcrypt_decryptMCRYPT_RIJNDAEL_256$pSecKey$pContentMCRYPT_MODE_ECB$strIV );
    return 
    rtrim$strPlaneText, ( ( ordsubstr$strPlaneTextstrlen$strPlaneText ) - 1)) >= and ordsubstr$strPlaneTextstrlen$strPlaneText ) - 1) ) <= 32 ) ? chrordsubstr$strPlaneTextstrlen$strPlaneText ) - 1) ) ): null ) ); 

    For Encryption:

    PHP Code:
    $iIVSize mcrypt_get_iv_sizeMCRYPT_RIJNDAEL_256MCRYPT_MODE_ECB );
    $strIV mcrypt_create_iv$iIVSizeMCRYPT_RAND );
    $pContent str_pad$pContent, ( 32*( floorstrlen$pContent ) / 32 ) + ( strlen$pContent ) % 32 == ) ) ), chr32 - ( strlen$pContent ) % 32 ) ) );
    $strCypherText mcrypt_encryptMCRYPT_RIJNDAEL_256$pSecKey$pContentMCRYPT_MODE_ECB$strIV );
    return 
    $strCypherText
    But when I encrypt data with MySQL AES_ENCRYPT() function and decrypt the data with the function written in PHP with the same key it is not giving the correct output.

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,400
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Let me repeat myself: choose to do both the encrypting and decrypting with PHP or MySQL. Not one with PHP and the other with MySQL.

  6. #6
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Let me repeat myself: choose to do both the encrypting and decrypting with PHP or MySQL. Not one with PHP and the other with MySQL.
    But i have some requirement in which we need to encrypt the data using PHP function and decrypt the data using MySQL function. SO we have chosen this way. Please suggest which mode SQL uses and My PHP function is using the correct mode or not?

  7. #7
    SitePoint Member
    Join Date
    Dec 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    You should look into TDE for MySQL by CritoTECH. It's called ezNcrypt and it allows you to do encryption without making any changes to your application.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •