Solved

How to encrypt and decrypt text (DES) with PHP?

Posted on 2004-03-23
5
559 Views
Last Modified: 2008-02-07
Hello everyone.
 
    I have this question: how to encrypt and decrypt text with PHP using DES, when text and the key are provided. Encrypting should be easy, that's what encrypt() function is for. Sadly, it's one way. How do I decrypt when I have a key?  Anyw help appreciated.
0
Comment
Question by:intoxicated
  • 3
  • 2
5 Comments
 
LVL 19

Accepted Solution

by:
Michael701 earned 500 total points
Comment Utility
here's a cut and paste i saved from a previous posting. I haven't used the code because I haven't gotten to that section of my project.

<?PHP
function encrypt($key,$data,$iv) {
// This function is for encrypting data. You must
// pass the key, data and iv in order to encrypt
    // Open the cipher
    $td = mcrypt_module_open ('rijndael-256', '', 'ofb', '');
     
    // Intialize encryption
    mcrypt_generic_init ($td, $key, $iv);

    // Encrypt data and return the results
    return mcrypt_generic ($td, $data);

    // Terminate encryption handler and close module
    mcrypt_generic_deinit ($td);
    mcrypt_module_close ($td);
}

function decrypt($key,$data,$iv) {
// This function will decrypt the data. You must
// pass the key, data and iv
    //Open the cipher
    $td = mcrypt_module_open ('rijndael-256', '', 'ofb', '');
     
    // Initialize encryption module for decryption
    mcrypt_generic_init ($td, $key, $iv);

    // Decrypt encrypted string and return results
    return mdecrypt_generic ($td, $data);

    // Terminate decryption handle and close module
    mcrypt_generic_deinit ($td);
    mcrypt_module_close ($td);
}

function encodekey($newkey) {
    // Open the cipher
    $td = mcrypt_module_open ('rijndael-256', '', 'ofb', '');
     
    // Detrmine key size
    $ks = mcrypt_enc_get_key_size ($td);

    // Create key in hash and return
    return $encodekey = substr (md5 ($newkey), 0, $ks);
     
    // Close module
    mcrypt_module_close ($td);
}

function getiv() {
    $td = mcrypt_module_open ('rijndael-256', '', 'ofb', '');
     
    // Create the IV and return results
    return $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
     
    // Close module
    mcrypt_module_close ($td);
}

--------------------------------------------------------------------------------



To use the above to encrypt this would work:


PHP:
--------------------------------------------------------------------------------
 
// We do need to make an iv and make an encrypted
    // Key here first
$mykey = "this is the long key";
    $iv = getiv();
    $myenkey = encodekey($mykey);
    // Lets do some encrypting
    $encrypted = encrypt($myenkey,$mydata,$iv);
     
    // Lets connect to the database
    $connect = mysql_connect($host,$username,$password) or die("Unable to connect");
    mysql_select_db($database);
    $query = "INSERT INTO $tbl(pass, data, iv) VALUES('$myenkey', '$encrypted', '$iv')";
     
    // Lets add the stuff to the database
    mysql_query($query) or die("Could not insert");

--------------------------------------------------------------------------------



Here is how you can decrypt the data:

PHP:
--------------------------------------------------------------------------------
 
// Lets encode the password to test it
        $passchk = encodekey($enkey);
     
        // Lets connect to the database
        $connect = mysql_connect($host,$username,$password) or die("Unable to connect");
        mysql_select_db($database);
        $query = "SELECT * FROM $tbl WHERE id = '$endata' and pass = '$passchk'";
     
        // Lets add the stuff to the database
        $result = mysql_query($query) or die("Could not insert");
        $getit = mysql_fetch_array($result);
        if(!$getit) {
            echo "You entered the wrong information please try again";
        }else{
            // Now lets decode this information
            $decoded =  decrypt($getit['pass'],$getit['data'],$getit['iv']
);
            echo $decoded;
        }

?>
0
 
LVL 12

Expert Comment

by:venkateshwarr
Comment Utility

There is no need to decrypt. That is why there are called one way functions.
if you have the key, then encrypt the given text again with the key and compare both the encryptions.
If they match then the key and text are correct or else, one of them is incorrect.

for encryption the function is crypt()

http://www.php.net/manual/en/function.crypt.php

venkat.
0
 
LVL 12

Expert Comment

by:venkateshwarr
Comment Utility

I did not see your post  Michael
Anyway, thanks for the code

venkat.
0
 
LVL 19

Expert Comment

by:Michael701
Comment Utility
venkateshwarr the need to encrypt credit card data and then be able to decrypt it to process the transactions.

I agree that passwords should be a one way encryption.
0
 
LVL 12

Expert Comment

by:venkateshwarr
Comment Utility
Michael, you are right.., I did not think in that direction.

venkat.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

728 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

14 Experts available now in Live!

Get 1:1 Help Now