Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6604893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:13:04+00:00 2026-05-25T19:13:04+00:00

I am trying to come up with a way to have PHP encrypt a

  • 0

I am trying to come up with a way to have PHP encrypt a file. I used to just use a PHP system call to run a script that encoded the file:

#!/bin/sh
/usr/bin/openssl aes-256-cbc -a -salt -k $1 -in $2

Argument 1 was the password to use and argument 2 is the data. I then use a second script on a computer to de-crypt the file.

#!/bin/sh
/usr/bin/openssl aes-256-cbc -a -d -salt -k $1 -in $2

This method of encrypting will not work on a production host as the PHP system call is disabled. I also would prefer not the change the decode function if at all possible.

Is there a way to replicate the above encrypt function using only PHP?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:13:05+00:00Added an answer on May 25, 2026 at 7:13 pm

    As stated above in the comments padding is necessary to make this work. The function below will make a file that can be decrypted on the command line like this:

    openssl enc -d -aes-256-cbc -a -salt -in test.txt
    

    The test.txt file is created from the output of the ssl_encrypt function below.

    function ssl_encrypt($pass, $data)
    {
        // Set a random salt
        $salt = substr(md5(mt_rand(), true), 8);
    
        $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        $pad = $block - (strlen($data) % $block);
    
        $data = $data . str_repeat(chr($pad), $pad);
    
        // Setup encryption parameters
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, "", MCRYPT_MODE_CBC, "");
    
    
        $key_len =  mcrypt_enc_get_key_size($td);
        $iv_len =  mcrypt_enc_get_iv_size($td);
    
        $total_len = $key_len + $iv_len;
        $salted = '';
        $dx = '';
        // Salt the key and iv
        while (strlen($salted) < $total_len)
        {
            $dx = md5($dx.$pass.$salt, true);
            $salted .= $dx;
        }
        $key = substr($salted,0,$key_len);
        $iv = substr($salted,$key_len,$iv_len);
    
    
        mcrypt_generic_init($td, $key, $iv);
        $encrypted_data = mcrypt_generic($td, $data);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
    
    
        return chunk_split(base64_encode('Salted__' . $salt . $encrypted_data),32,"\r\n");
    }
    

    Example Usage:

    $plainText = "Secret Message";
    $password = "SecretPassword";
    $test = ssl_encrypt($password, $plainText);
    $file = fopen('test.txt', 'wb');
    // Write the Base64 encrypted output to a file.
    fwrite($file, $test);
    fclose($file);
    // Show the output on the screen
    echo $test;
    

    References: http://juan.boxfi.com/2010/03/16/write-openssl-files-in-php/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this ajax_update script that updates file.php every 60 seconds.. Now file.php outputs
I'm trying to come up with a way to make the computer do some
I'm trying to come up with a way to query dates in Lucene. Basically
I'm trying to come up with a way to query the values in two
I've been trying to come up with a way to solve my problem, but
I'm trying to come up with a way to query my blog database to
I'm trying to come up with a way of checking in the derived class
I'm trying to come up with a way to mimik something we were previously
I am trying to come up with a better way of storing/parsing CSS/JS content
I am trying to come up with a manageable way to handle exceptions in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.