juanli
juanli

Reputation: 613

$this->encryption->decrypt($text) return boolean

Library 'encryption' is used.

$this->encryption->encrypt($plain_text)

Can return the encrypted text, but

$this->encryption->decrypt($cipher_text)

only return boolean.

I'm wondering what's going on?

Edit reason: Add the code for defining the class:


 <?php

class Messages extends CI_Controller {
    public function index() {
        $this->load->view("messages/index.php");
    }
    function encrypt()
    {   
        $this->load->view("messages/encrypt.php");
    }
    function decrypt()
    {
        $this->load->view("messages/decrypt.php");
    }
    function process_encrypt() {
        if(!$this->session->userdata('encrypted_count')) {
            $this->session->set_userdata('encrypted_count', 0);
        }
       $encrypted_count = $this->session->userdata('encrypted_count');

        $plain_text = $this->input->post('plain_text', TRUE);
        if($plain_text) {
            $encrypted_text = $this->encryption->encrypt($plain_text['plain_text']);
            $this->session->set_flashdata('encrypted_text', $encrypted_text);
            $encrypted_count++;
        } else {
            $this->session->set_flashdata('encrypted_text', "Error: please enter your text to encrypt."); 
        }
        $this->session->set_userdata('encrypted_count', $encrypted_count); 
        redirect("encrypt");

    }
    function process_decrypt() {
        if(!$this->session->userdata('decrypted_count')) {
            $this->session->set_userdata('decrypted_count', 0);
        }
        $decrypted_count = $this->session->userdata('decrypted_count');

         // decrypted text
        $cipher_text = $this->input->post('cipher_text', TRUE);
        if($cipher_text) {
            $decrypted_text = $this->encryption->decrypt('hello');
            $this->session->set_flashdata('decrypted_text', $decrypted_text);
            $decrypted_count++;
        } else {
            $this->session->set_flashdata('decrypted_text', "Error: please enter your text to decrypt.");  
        }
        $this->session->set_userdata('decrypted_count', $decrypted_count);
         redirect("decrypt");
    }

}

No matter what inside the $this->encryption->decrypt($sometext), it always return bool(false).But this works in my college's project.

I load the library inside the autoload.php file:

$autoload['libraries'] = array('database','session', 'encryption');

Upvotes: 0

Views: 678

Answers (1)

Alex
Alex

Reputation: 9265

This $encrypted_text = $this->encryption->encrypt($plain_text['plain_text']); should be $encrypted_text = $this->encryption->encrypt($plain_text); because of how you defined it here $plain_text = $this->input->post('plain_text', TRUE);

Generally when debugging it is good to remove redirects as they prevent you from seeing notices and such (and also make sure error reporting is on; in CI this is done by setting the environment to development). This would have uncovered your error rather quickly.

Upvotes: 1

Related Questions