Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    762

    quick question about mcrypt

    Hello,

    I recently found a nice php function that helps encrypt/decrypt text with a salt key.

    I use it like this:

    PHP Code:
    <?php

    function mcryptString($text$salt_key$mode)
    {
        
    $data="";
        
    $td mcrypt_module_open('rijndael-256''''ecb''');
        
    $iv mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        
    $salt_key substr($salt_key0mcrypt_enc_get_key_size($td));
        
    mcrypt_generic_init($td$salt_key$iv);
        if(
    $mode == "encrypt") {
            
    $data mcrypt_generic($td$text);
        } else if (
    $mode == "decrypt") {
            
    $data rtrim(mdecrypt_generic($td$text));
        }
        
    mcrypt_generic_deinit($td);
        
    mcrypt_module_close($td);
        return 
    $data;
    }

    $encrypted_data mcryptString("Hello World"md5("a1b2c3d4"), "encrypt");

    ?>
    My question: is it possible to store the contents of $encrypted_data in a mysql database? if yes, how?

    if not, are there any good alternative out there that will let me encrypt/decrypt with salt key (and possibly generate encrypted data that can be saved in db)?

  2. #2
    Join Date
    Jan 2008
    Location
    England
    Posts
    573
    Just insert it like you would any other data.

  3. #3
    Join Date
    May 2007
    Location
    Orange Country, CA
    Posts
    138
    Base64 encode it before storing, or store in blob format. I prefer base64.

  4. #4
    Join Date
    Oct 2005
    Location
    UK
    Posts
    552
    If you're planning to decrypt the data later, you may also need to store the IV with the data to ensure you can actually decrypt it.

  5. #5
    Join Date
    Jan 2005
    Location
    UK, London
    Posts
    762
    thanks Codelphious, base64 works nicely

    @Dark Light, decrypting works fine without having to store the IV at the moment, all it requires is encrypted data and the salt key used during encryption

Similar Threads

  1. Another CPanel Mcrypt question
    By BoggeRs in forum Hosting Security and Technology
    Replies: 3
    Last Post: 10-06-2005, 07:55 AM
  2. 2CO Quick Question
    By My eWriters in forum Ecommerce Hosting & Discussion
    Replies: 4
    Last Post: 01-13-2004, 11:44 PM
  3. Quick Question...
    By michaelessy in forum Web Hosting Lounge
    Replies: 1
    Last Post: 06-30-2003, 07:58 PM
  4. Quick 2CO Question
    By Red Rover in forum Running a Web Hosting Business
    Replies: 2
    Last Post: 04-22-2003, 10:38 PM
  5. Replies: 4
    Last Post: 11-25-2002, 11:27 PM

Posting Permissions

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