PHP encryption with wordpress

Go To StackoverFlow.com

0

My website is built in Wordpress and we are collecting personal information that I will need to place in my database. Here is my php so far for the insertion:

//defined in wp-config.php
$key = KEY_ENCRYPT;

function encrypt($text) 
{   
    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)))); 
} 


if($_POST){ 
    //POST object placed in variables
    $user_domain = $_POST['domain'];
    $s_user = $_POST['s-username'];
    $s_pass = $_POST['s-password'];
    $w_user = $_POST['w-username'];
    $w_pass = $_POST['w-password'];

    //encrypting data
    $encrypted_server_username = encrypt($s_user); 
    $encrypted_server_password = encrypt($s_pass);
    $encrypted_wordpress_username = encrypt($w_user); 
    $encrypted_wordpress_password = encrypt($w_pass);

    //set up array for options table
    $user_website_data = array(
        'domain'=>$user_domain,
        'server_username'=>$encrypted_server_username,
        'server_password'=>$encrypted_server_password,
        'wordpress_username'=>$encrypted_wordpress_username,
        'wordpress_password'=>$encrypted_wordpress_password
        );  

        update_option($user_domain . '_website_data', $user_website_data);

This code successfully stores the information in an array. You can even see this code working and the process at http://thewpvalet.staging.wpengine.com/sign-up/?plan=basic. Please use 4242424242424242 as the CC number to test.

Now I'm trying to implement the decode on the backend admin area so that I can search by domain and pull up credentials. This is my code:

if(isset($_POST['domain'])){
        function decrypt($text) 
        {
            $key = KEY_ENCRYPT;

            return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($text), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
        }


        $search_domain = $_POST['domain'];
        $url_removal = array("http://","www.");
        $clean_search_domain = str_replace($url_removal, '', $search_domain);
        $user_options = get_option($search_domain.'_website_data');

        echo '<strong>Login Information:</strong></br>' .
        'Domain:' . $user_options['domain'] . '</br>' .
        'Server Username:' . decrypt($user_options['server_username']) . '</br>';

    }

This returns mcrypt_decrypt() [function.mcrypt-decrypt]: Size of key is too large for this algorithm in /nas/wp/www/staging/thewpvalet/wp-content/plugins/user-info/index.php on line 43

Any idea what I could be doing wrong here?

2012-04-04 21:22
by Enrico
how long is $key / KEY_ENCRYPT - Marc B 2012-04-04 21:24
Christ, you really shouldn't be using WordPress to accept credit card data. Have you even looked into the hugely onerous task that is PCI compliance? Use a service like Stripe for this - ceejayoz 2012-04-04 21:26
ceejayoz, please excuse me but if your going to make a comment, please stay with the question. I am using stripe and using the Stripe.js class to send tokens for the CC information. This is now regarding the Wordpress credentials and servers information that I need in order to do migrations. If you do not have something nice or helpful to say without making assumption, please don't comment - Enrico 2012-04-04 21:33
Marc B, the key is 34 characters lon - Enrico 2012-04-04 21:34
Any update on this - atwellpub 2014-12-11 02:29


2

Any idea what I could be doing wrong here?

Yes, the thing you're doing wrong is rolling your own cryptography. Let's add some whitespace and look at your function in detail:

/**
 * THIS CODE IS INSECURE. DO NOT USE IT. PURGE IT FROM YOUR CODEBASE!
 */
function encrypt($text) 
{
    return trim(
        base64_encode(
            mcrypt_encrypt(
                MCRYPT_RIJNDAEL_256, // Non-standard block cipher; not AES
                $key,
                $text,
                MCRYPT_MODE_ECB, // ECB mode is insecure
                mcrypt_create_iv( // ECB mode doesn't use an IV anyway
                    mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB),
                    MCRYPT_RAND // you want MCRYPT_DEV_URANDOM
                )
            )
        )
    ); 
}

You're generating an IV (insecurely) with ECB mode (which discards the IV anyway), for a non-standard Rijndael variant, and you're not employing message authentication. Encryption without message authentication is a fatal mistake.

Marc B, the key is 34 characters long

If you're using MCRYPT_RIJNDAEL_256 or AES (which is exclusively MCRYPT_RIJNDAEL_128 by the way; mcrypt is considered harmful), these are the only acceptable key sizes:

  • 16 bytes
  • 24 bytes
  • 32 bytes

The reason you are getting the error is that 34 is an invalid input. This likely means that you are using a human-readable password instead of encryption key.

TL;DR: Don't roll your own crypto, use a well-studied implementation instead. defuse/php-encryption and Zend\Crypt are your best bet.

2015-08-18 13:35
by Scott Arciszewski
Ads