枚举mysql表并加密字段

时间:2017-03-24 17:25:00

标签: php mysql codeigniter encryption encode

我正在进行CI 1.2到CI 3.x的升级。我有3个表,其中11个字段在CI 1.x中使用SHA1编码。 CI 3使用AES-256。实际上,我需要执行一个UPDATE tablex集(fielda = $ this-> encrypt-> encode_from_legacy(fielda)),这是一个无效的SQL。

如何枚举目标表的每一行并使用编码函数的结果更新行?

我想不出使用不同的密码和模式在本机SQL语句中解码和编码的方法。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这是我提出的将旧的MD5散列数据重新加密为AES-256数据的解决方案。 (不是密码数据)。

function doUpdates ($query = null) {
    $this->load->library('encrypt');        //obsolete MD5 (PHP 7.x not supported)
    $this->load->library('encryption');     // CIv3 AES-256
    $dbobject=array(
    'appointments'    => array('completion_notes'),
    'health_profiles' => array('allergies', 
                               'pregnancy_history', 
                               'medications', 
                               'hospitalizations',
                               'social_history',
                               'personal_illness',
                               'family_illness',
                               'height',
                               'weight')
    );

    foreach($dbobject as $table=>$fields) {
        echo "Fix table: ".$table."<br/>";
        $query = $this->db->get($table)->result_array();
        foreach ($query as $row) {
            foreach($fields as $field){
               //decrypt from legacy MD5 to AES-256
               $after = $this->encryption->encrypt(
                        $this->encrypt->decode(
                            $this->encrypt->encode_from_legacy($row[$field])));
                $sql_set = $field."= '".$after."',";
            } //end foreach field

            //remove last comma
            $sql_set = substr_replace($sql_set, "", -1);

            switch ($table) {
                case "appointments":
                    $primarykey = "appointment_id";
                    break;
                case "health_profiles":
                    $primarykey = "health_profile_id";
                    break;
            } //end switch
            echo "Encrypting data for primary key: ".$row[$primarykey]."<br/>";
            $this->db->simple_query('update '.$table. " set ". $sql_set." where ".$primarykey." = '".$row[$primarykey]."'");
        }  // end for each row  
    } //end foreach table
} // end createUpdates

} //结束recrypt控制器

相关问题