• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 580
  • Last Modified:

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

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
intoxicated
Asked:
intoxicated
  • 3
  • 2
1 Solution
 
Michael701Commented:
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
 
venkateshwarrCommented:

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
 
venkateshwarrCommented:

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

venkat.
0
 
Michael701Commented:
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
 
venkateshwarrCommented:
Michael, you are right.., I did not think in that direction.

venkat.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now