How to do encryption in PHP equal to Perl's bz_crypt() function?

Posted on 2009-04-27
Last Modified: 2012-05-06
What is the equivalent PHP code for the following Perl code?
(Note there is no salt arg, there is just one argument for a plaintext $password.)
I need to be able to create the identical encrypted password as in Perl.
my $cryptpassword = bz_crypt($password);

Open in new window

Question by:DigitalDave1
    LVL 2

    Expert Comment

    PHP now contains its own implementation for the MD5 crypt, Standard DES, Extended DES and the Blowfish algorithms and will use that if the system lacks of support for one or more of the algorithms.

    You can get more info at:
    $cryptpassword = crypt($password);

    Open in new window


    Accepted Solution

    Thanks for the tip. The PHP function crypt() has some similarities to crypt() in Perl.

    I was able to find some Perl documentation about bz_crypt() and with your tip, I put together something that works...
    private function bz_crypt( $in_plainText ) {
       // verify we can do the two-character salt with DES encryption:
       if (1 != CRYPT_STD_DES) {
          return false;
       static $strCharset = 
       $len  = strlen ($strCharset);
       $idx1 = rand (0, $len - 1);
       $idx2 = rand (0, $len - 1);
       $salt = substr ($strCharset, $idx1, 1) . 
               substr ($strCharset, $idx2, 1);
       return crypt ($in_plainText, $salt);    
    } /* end bz_crypt() private function definition */

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    Title # Comments Views Activity
    php query string addition 4 23
    sql statement - php 5 40
    Reverse asset logic not working 11 26
    Is COM supported from Apache 1 18
    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now