duanfei7508 2012-04-04 21:22
浏览 44

使用wordpress进行PHP加密

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?

  • 写回答

1条回答 默认 最新

  • duanfan1965 2015-08-18 13:35
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。