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 8037655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:59:22+00:00 2026-06-05T02:59:22+00:00

I’m using PHP mcrypt to encrypt and decrypt values to a flat file in

  • 0

I’m using PHP mcrypt to encrypt and decrypt values to a flat file in Windows 7.

If I write the value directly with no other information and decrypt it out of the file the algorithm works fine. If I try to encode it as a key/value pair (password=xxxxxx) and use explode() to parse out the pair at the “=” sign, the decrypt function hands back something other than what was put in.

I’m using trim() to cut off any whitespace (it looks like it might be carrying an EOL character), and have tried parsing out the key and value manually as well.

Nothing seems to be working. Has anyone else seen this issue and if so, how did you get past it?

Encrypt/decrypt code:

function encrypt ($strToEncrypt) {
    global $td, $iv, $key;

    /* Intialize encryption */
    mcrypt_generic_init($td, $key, $iv);

    /* Encrypt data */
    $encrypted = mcrypt_generic($td,$strToEncrypt);
    echo "encrypted = " . $encrypted . "<BR>";
    return $encrypted;
}

function decrypt($strToDecrypt) {
    global $td, $iv, $key;

    /* Initialize encryption */
    mcrypt_generic_init($td, $key, $iv);

    /* Decrypt data */
    $decrypted = mdecrypt_generic($td, $strToDecrypt);
    return $decrypted; 
}
?>

File manipulation code:

include 'libEncryption.php';
$tempStr ="";
$tempStr2="";
try {
    $nextPage = $_REQUEST["NEXTPAGE"];
    switch ($nextPage) 
    {
        case "ENCRYPT":
            echo 'String to encrypt = ' . $_POST["txtSeedStr"] ."<BR>";
            if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
                $file = fopen("C:\\temp\\EncryptTest.txt", "wb") or exit("Unable to open file!");
            } else {
                $file = fopen("//home/prem03/EncryptTest.txt", "w") or exit("Unable to open file!");
            }
            fwrite($file, "username=web_app\npassword=");
            $tempStr = encrypt($_POST["txtSeedStr"]);
            fwrite($file, $tempStr, strlen($tempStr));
            fwrite($file, "\n");
            fwrite($file, "DBNAME=DEV11");
            fclose($file);
            break;
        case "DECRYPT":
            if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
                $file = fopen("C:\\temp\\EncryptTest.txt", "rb") or exit("Unable to open file!");
            } else {
                $file = fopen("//home/prem03/EncryptTest.txt", "r") or exit("Unable to open file!");
            }
            if ($file) {
                while (!feof($file)) {
                    $str1=fgets($file);
                    echo "str1 = " . $str1 . "<br>";
                    list($key,$value) = explode("=",$str1);
                    $var1=strlen(trim($value));
                    echo "key = ".$key.' value = '.trim($value) . ' ' . $var1 . "<br />";
                    if ($key == "password") {
                        $var2 = trim($value);
                        $tempStr2 = decrypt($var2);
                        echo 'tempStr2 = ' . $tempStr2 . "<BR>";
                    }
                }

            }
            fclose($file);
            break;
        default:
            break;
    } 
} catch (Exception $ex) {
    echo $ex->getMessage();
}  
?>
  • 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-06-05T02:59:23+00:00Added an answer on June 5, 2026 at 2:59 am

    I found the error – it was a typical greenhorn mistake. I used the variable $key in two different contexts – one in the encryption library file, which is where I wanted to use the variable, and again in the file manipulation code, where I changed the value of $key based on the string I was exploding. My thanks to the NetBeans IDE and xdebug for illuminating my logic flaw.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a reasonable size flat file database of text documents mostly saved in
I am using parse_ini_file to read the contents of a file however it is
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.