Welcome Guest, Not a member yet? Register   Sign In
Encryption Observation
#1
Question 
(This post was last modified: 05-10-2018, 02:38 PM by CobolGuy.)

After 
1) Auto-loading the encryption library
2) Generating an encryption key
3) Storing the encryption key in the config file

I used the encrypt function prior to storing passwords in the user table. 

Funny thing is, during Login, I tried encrypting the posted password from $this->input->post( ), and including it in the WHERE clause together with the email address, and for some reason it never returned TRUE.

An experiment lead me to DEcrypt the table value of the password, proved that the value is indeed the one I entered. 
However, when I removed the password from the WHERE clause of the query, and retrieved the user row only with the email address; I subsequently DEcrypted the database value of the password, and compared it with the posted password as-is, it worked just fine and the user was logged in. 

So, Do I understand it correctly; that there is something instance related in the encrypted value, such that if you encrypt the same value again, it won't match. To determine equality, one has to DEcrypt the encrypted version, and compare the native values??? 

What say yee?
Reply
#2

A better practice is to use PHP's password_hash, as suggested at the top of the encryption docs (https://www.codeigniter.com/user_guide/l...ption.html). You then use a password_verify call, with the plaintext password entered by the user, to authenticate them.
Reply
#3

(This post was last modified: 05-10-2018, 04:11 PM by CobolGuy. Edit Reason: Follow up )

(05-09-2018, 06:37 PM)ciadmin Wrote: A better practice is to use PHP's password_hash, as suggested at the top of the encryption docs (https://www.codeigniter.com/user_guide/l...ption.html). You then use a password_verify call, with the plaintext password entered by the user, to authenticate them.

Important
You mean this suggestion: 
DO NOT use this or any other encryption library for user password storage! Passwords must be hashed instead, and you should do that via PHP’s own Password Hashing extension.

I tried this approach and it works great!! 

Thank You!!
Reply
#4

That's the one Smile
Reply
#5

Huh  I have more encryption questions!!

The passwords are being stored as a password_hash, and I'm happy with that.
But, I want to store some data that needs to be secure, other than passwords... it's a private app running on the web, so the data has to be there long enough for one user to enter it, and another user to use before it gets deleted. No one row on that table will survive the end of the day.

After considerable digging, it seems that if you ask a million programmers, you will get a million answers. Bah!
So here I am asking for answer 1 million and 1.
And of the articles I've read, no one can explain any of it as a "step-by-step here do this". I'm not all that interested in the minutia of cryptography.

Idea
Can I improve the security of the CI Encryption Library, by taking an encryption key value that I come up with, and not store it on my server, but rather pass my encryption key value through the password_hash, and then using the hashed encryption key as the key for the encryption of the data that I intend to secure? Would it even matter? 

Huh
Reply
#6

(06-23-2018, 10:54 AM)CobolGuy Wrote: Can I improve the security of the CI Encryption Library, by taking an encryption key value that I come up with, and not store it on my server, but rather pass my encryption key value through the password_hash, and then using the hashed encryption key as the key for the encryption of the data that I intend to secure? Would it even matter? 

Password hashing is one way encryption, which means you will never be able to decrypt passwords do their original plain text form, you can only check that user input from login form matches what you have in stored in DB.

Encrypting values yourself before storing them in DB will offer protection if someone manages to download or gain direct access to your database, or lets say you don't trust or have legal requirement for your developers to not have access to customer details. Most times it is only necessary for very big enterprise level apps, where company deliberately wants to have no access to customer data, in case they are asked by government to revel some of it.

In most cases you are better off working on securing front-end part of the app if you want to be more secure. Things like escaping DB queries so no-one can send DB server their own queries, checking that user has correct privileges to access resource (if you use IDs on URL, it's very easy for someone to just change URL in their browser and try to see someone elses data).
Reply
#7

This is what you may be looking for.

MySQL AES_ENCRYPT() function

MySQL AES_DECRYPT() function
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#8

(This post was last modified: 06-24-2018, 10:41 AM by CobolGuy.)

Thank you all; but we aren't finished yet.  

I found this snippet, it seems to work,  and I intend to stick it into a helper, like so... 
I'm not yet solid on where to put the $secret_key and the $secret_iv values, certainly not hard coded in the function.

Any suggestions on where or how to hide these values?


Code:
<?php

if ( ! function_exists('encrypt_decrypt') )
{
    /**
     * encrypt_decrypt function
     *
     * Executes it when you have a string that needs encrypting
     * or decrypting .
     *
     */
    function encrypt_decrypt($action, $string)
    {      
               $output         = FALSE;
        
        $method         = "AES-256-CBC";
        $secret_key    = 'The Secret Key here';
        $secret_iv    = 'The Initialization Vector here';

//        hash the key         
        $key    = hash('sha256', $secret_key);     

//        hash the iv - the encrypt method AES-256-CBC expects 16-bytes - else get a warning
        $iv    = substr(hash('sha256', $secret_iv), 0, 16 );
        
        if ( $action == 'encrypt')
        {
            $output = openssl_encrypt($string, $method, $key, 0, $iv);
            $output = base64_encode($output);
        }        
        else if ( $action == 'decrypt')
        {
            $output = openssl_decrypt(base64_decode($string), $method, $key, 0, $iv);
        }
        
        return $output;
    }
Reply




Theme © iAndrew 2016 - Forum software by © MyBB