2

I am buidling a private site where each user has private data. I would like to provide a good level of security for their content. Towards this i am planning to:

-Use SSL sitewide (the site is not big so i think i will go sitewide ssl) -I am hashing the user password with crypt() using my codeigniter configuration encryption key.

I want to give the users the ability to chose some or all of their private posts to be encrypted in the database.

I think of letting them specify a private key which i will use to encrypt the post body text and then insert it into the database field. I want them to have the responsibility of which key they use where. They could use one key or they can choose to use different.

  • Is there any special consideration regarding the posts table column? Now the field is created as mediumtext utf8_unicode_ci .

  • Assuming that a strong provacy agreement will compliement the use of SSL, what else i could do to provide my users with the sense that their data are as secure as they can be?

1 Answer 1

2

The db field is ok.

you can use unique encryption key for each user, so data in db will be encrypted by that key which is a long unique hash

then you can use 2 separated databases (not tables)

1 db for users encryption keys

1 db for users data encrypted by keys

then i share you my encrypt/decrypt library which uses AES and it's all the best i've found:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Crypto encrypt/decrypt Class it uses AES by $secret_key
 *
 * @access    public
 * @param     array/value
 * @return    array/value
 */
class Crypto(){

    function encrypt($data,$secret_key){
        $array = array();

        if(is_array($data)){
            foreach($data as $key=>$value){
                 $array[$key] = trim(
            base64_encode(
                mcrypt_encrypt(
                    MCRYPT_RIJNDAEL_256,
                    $secret_key, $value, 
                    MCRYPT_MODE_ECB, 
                    mcrypt_create_iv(
                        mcrypt_get_iv_size(
                            MCRYPT_RIJNDAEL_256, 
                            MCRYPT_MODE_ECB
                            ), 
                        MCRYPT_RAND)
                    )
                )
            );
            }
            return $array;

        }else{

           return trim(
            base64_encode(
                mcrypt_encrypt(
                    MCRYPT_RIJNDAEL_256,
                    $secret_key, $data, 
                    MCRYPT_MODE_ECB, 
                    mcrypt_create_iv(
                        mcrypt_get_iv_size(
                            MCRYPT_RIJNDAEL_256, 
                            MCRYPT_MODE_ECB
                            ), 
                        MCRYPT_RAND)
                    )
                )
            );
       }
   }



   function decrypt($data,$secret_key)
   {
    $array = array();

        if(is_array($data)){
            foreach($data as $key=>$value){
                 $array[$key] = trim(
            mcrypt_decrypt(
                MCRYPT_RIJNDAEL_256, 
                $secret_key, 
                base64_decode($value), 
                MCRYPT_MODE_ECB,
                mcrypt_create_iv(
                    mcrypt_get_iv_size(
                        MCRYPT_RIJNDAEL_256,
                        MCRYPT_MODE_ECB
                        ), 
                    MCRYPT_RAND
                    )
                )
            );
            }
            return $array;
        }else{
        return trim(
            mcrypt_decrypt(
                MCRYPT_RIJNDAEL_256, 
                $sSecretKey, 
                base64_decode($sValue), 
                MCRYPT_MODE_ECB,
                mcrypt_create_iv(
                    mcrypt_get_iv_size(
                        MCRYPT_RIJNDAEL_256,
                        MCRYPT_MODE_ECB
                        ), 
                    MCRYPT_RAND
                    )
                )
            );
    }
}


}
//end class Crypto

NOTE: $secret_key is the user key and $data is the data you want to encrypt or decrypt

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! regarding the field: should i change the data field in varbinary or not? the content i will encrypt will have unicode characters
@e4rthdog no leave default it's ok ;), just take care on fields length cause encrypted data is much longer then decrypted one ;), so maybe better TEXT then varchar 255 for example. You're welcome, don't forget to check answer if it's all ;)
and another: your class uses core php funcitons? I mean , i imagine my users to have many posts encrypted and after5-10 years something changes. I should always watch out for these changes and re-encrypt the data, yes?
@e4rthdog well encryption is always updated cause it's always trying to be more safe, actually AES is the maybe only one little bit more safe method, then if you'll need to upgrade the code is not a problem you'll find all the instructions on PHP official documentation for sure
@e4rthdog right, usually if you stole 1 db you stoled all, in this case you have 2 db 1 depends on other, if you stole 1 db you have to stole also the other or you'll get nothing in your hands :D
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.