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

  • Home
  • SEARCH
  • 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 8577425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:09:25+00:00 2026-06-11T20:09:25+00:00

I got a piece of code from Internet to encrypt my data using TripleDES.

  • 0

I got a piece of code from Internet to encrypt my data using TripleDES.

$key = "ThisIsTheKey"; // 24 bit Key
$iv = "fYfhHeDm"; // 8 bit IV
$bit_check = 8; // bit amount for diff algor.


//function to encrypt
function encrypt($text) {
    global $key,$iv,$bit_check;
    $text_num = str_split($text, $bit_check);
    $text_num = $bit_check - strlen($text_num[count($text_num) - 1]);
    for ($i = 0; $i < $text_num; $i++) {
        $text = $text . chr($text_num);
    }
    $cipher = mcrypt_module_open(MCRYPT_TRIPLEDES, '', 'cbc', '');
    mcrypt_generic_init($cipher, $key, $iv);
    $decrypted = mcrypt_generic($cipher, $text);
    mcrypt_generic_deinit($cipher);
    return base64_encode($decrypted);
}

The problem is even though I call the variables as global, in the top (where I declared the variables) show the variable are unused. As well as when I try to run this it gave an error. But when I declare the same variable set inside the function it works.

  • 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-11T20:09:26+00:00Added an answer on June 11, 2026 at 8:09 pm

    As a general note, The use of global variables is highly not recommended. When I look at your function, I see it only needs $text, however, it actually needs $text, $key, $iv, and $bit_check.

    Lets try not using globals:

    $key = "ThisIsTheKey"; // 24 bit Key
    $iv = "fYfhHeDm"; // 8 bit IV
    $bit_check = 8; // bit amount for diff algor.
    
    
    //function to encrypt
    function encrypt($text, $key, $iv, $bit_check) {
        $text_num = str_split($text, $bit_check);
        $text_num = $bit_check - strlen($text_num[count($text_num) - 1]);
        for ($i = 0; $i < $text_num; $i++) {
            $text = $text . chr($text_num);
        }
        $cipher = mcrypt_module_open(MCRYPT_TRIPLEDES, '', 'cbc', '');
        mcrypt_generic_init($cipher, $key, $iv);
        $decrypted = mcrypt_generic($cipher, $text);
        mcrypt_generic_deinit($cipher);
        return base64_encode($decrypted);
    }
    

    And call it using

    encrypt("Hello World!", $key, $iv, $bit_check);
    

    Another solution involves the use of CONSTANTS, assuming the key, iv and bit_check will never change throughout the entire execution time, you can define them as constants, and they would be globally available throughout the entire application, and will not be able to change.

    Like so:

    const KEY = "ThisIsTheKey"; // 24 bit Key
    const IV = "fYfhHeDm"; // 8 bit IV
    const BIT_CHECK = 8; // bit amount for diff algor.
    
    
    //function to encrypt
    function encrypt($text) {
        $text_num = str_split($text, BIT_CHECK);
        $text_num = BIT_CHECK - strlen($text_num[count($text_num) - 1]);
        for ($i = 0; $i < $text_num; $i++) {
            $text = $text . chr($text_num);
        }
        $cipher = mcrypt_module_open(MCRYPT_TRIPLEDES, '', 'cbc', '');
        mcrypt_generic_init($cipher, KEY, IV);
        $decrypted = mcrypt_generic($cipher, $text);
        mcrypt_generic_deinit($cipher);
        return base64_encode($decrypted);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a piece of code from the internet . The code connects to
I got a piece of code from my friend. But I am really confused,
I got this piece of Assembly code extracted from some piece of software, but
in the following piece of code I got from a book. The NSString *pPath
I got this piece of code from Mysql 4th edition to check table existence
I've got this piece of code: using Posix; int fuseguifs_getattr(string path, Posix.Stat *stbuf) {
I've got this piece of code from a Backbone.js tutorial from here . The
I've got this piece of code, where I gather device id from different types
I got the following piece of code from SSIS Team Blog to cast the
I got a piece of code that display a random quote from a text

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.