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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:58:55+00:00 2026-05-25T15:58:55+00:00

Ok, I have tried to create my own encryption/decryption methods using PHP mcrypt ,

  • 0

Ok, I have tried to create my own encryption/decryption methods using PHP mcrypt, and when I posted them a while back some called them “trash”. They were mentioning things about “Initialization Vectors” and such. Basically, how can I make these cryptography methods better:

function encrypt($key, $data){
    $encrypted_data = mcrypt_cbc(MCRYPT_RIJNDAEL_192, $key, $data, MCRYPT_ENCRYPT);
    return base64_encode($encrypted_data);
}

function decrypt($key, $encryptedData){
    $dec = base64_decode($encryptedData);
    $decrypt = mcrypt_cbc(MCRYPT_RIJNDAEL_192, $key, $dec, MCRYPT_DECRYPT);
    return trim($decrypt);
}

I want these to work the best they can except I am a duck in a brand new world when it comes to mcrypt, any suggestions are welcome, thanks!

  • 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-25T15:58:56+00:00Added an answer on May 25, 2026 at 3:58 pm

    Here is a snippet of the mcrypt functions I use. They use mcrypt_generic and mdecrypt_generic, which should be used according to the PHP manual.

    function encrypt($key, $data){
        $b = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
        $enc = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, '');
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($enc), MCRYPT_DEV_URANDOM);
        mcrypt_generic_init($enc, md5($key), $iv);
    
        // PKCS7 Padding from: https://gist.github.com/1077723
        $dataPad = $b-(strlen($data)%$b);
        $data .= str_repeat(chr($dataPad), $dataPad);
    
        $encrypted_data = mcrypt_generic($enc, $data);
    
        mcrypt_generic_deinit($enc);
        mcrypt_module_close($enc);
    
        return array(
            'data' => base64_encode($encrypted_data),
            'iv' => base64_encode($iv)
        );
    }
    
    function decrypt($key, $iv, $encryptedData){
        $iv = base64_decode($iv);
        $enc = mcrypt_module_open(MCRYPT_RIJNDAEL_256, '', MCRYPT_MODE_CBC, '');
        mcrypt_generic_init($enc, md5($key), $iv);
    
        $encryptedData = base64_decode($encryptedData);
        $data = mdecrypt_generic($enc, $encryptedData);
        mcrypt_generic_deinit($enc);
        mcrypt_module_close($enc);
    
        // PKCS7 Padding from: https://gist.github.com/1077723
        $dataPad = ord($data[strlen($data)-1]);
    
        return substr($data, 0, -$dataPad);
    }
    

    I don’t know much about mcrypt either, so I just kinda hacked these together. I md5 the key so it’s always 32 characters (the max key length), and I randomly calculate an “Initialization Vector”.

    Using PKCS7 Padding is better because you can have strings that end in white space (as trim would remove that), also the encryption is more efficient when the string is a certain length.

    I’m using AES 256 (MCRYPT_RIJNDAEL_256) here, but AES 192 (MCRYPT_RIJNDAEL_192) would work too.

    Demo: http://ideone.com/WA5Tk

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

Sidebar

Related Questions

I have tried to find how to create DLL-s on linux using google, but
I have tried to use ASP.NET MVC for a while, then I face a
I have tried to use some of the widgets in JQuery UI on an
I'm planning to create my own metronome (woodblock instrument) using Java that could set
i am using raw sockets to create my own socket. i need to set
How can I create my own methods which take a block as an argument
Im trying to create my own database with MySQL Workbench and run some queries
I have tried <ul id=contact_list> <li id=phone>Local 604-555-5555</li> <li id=i18l_phone>Toll-Free 1-800-555-5555</li> </ul> with #contact_list
I have tried this... Dim myMatches As String() = System.Text.RegularExpressions.Regex.Split(postRow.Item(Post), \b\#\b) But it is
I have tried what seems like everything - I've done similiar things many times

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.