how to encrypt data and then decrypt to use it

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

how to encrypt data and then decrypt to use it

Post by arukomp »

Hello,

I'm working on my new project. It's a fresh new and I've made only login function.

In this project, there's going to be many data which will be stored in MySQL. What I want to do is to encrypt that data and put it in MySQL. Then, decrypt that data for use.

I'm using crypt(); for password verifying for login function, but I don't know how do decrypt data encrypted with crypt(); so this function isn't for that purpose I want to create.

So, maybe anyone know a way to keep data encrypted and later decrypt it for use?

Thanks for answers
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

crypt() does one-way hashing. You cannot decrypt that. mcrypt contains several encryptions that can be decrypted.

Keep passwords in one-way hashes.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

thanks a lot, I'll at it
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

bitwise, encryption and decryption has key_str. As i understand, it is used to encrypt and decrypt a string the same way as a key_str string. Can I set one string, like "i like pies" to encrypt and then use this to encrypt and decrypt all other strings? I hope you understood what i meant :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't understand what you meant.
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

I looked further in these functions descriptions and my question disappeared :) I have no questions now :)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

I wrote the following code a while ago for fun. Like any encyption method it is crackable but with a reasonable length key it would take a pretty experience cryptographer to crack it.

Code: Select all

<?php

$target = 'This is the string I want to encode';
$key = 'How cool is this';

$encoded = vigenere($target, $key);
$decoded = vigenere($encoded, $key, true);

echo "Encoded: $encoded<br>\nDecoded: $decoded";

/*

Prints:

	Encoded: |XascYcltRY h]\]7WwIcgQ[tihotNXW8T]
	Decoded: This is the string I want to encode

*/

function vigenere($target, $key, $decode = false)
{
    # http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher
	for($i = 32; $i <127; $i++)
    {
	    $alphabet_array[] = chr($i);
    }
    $alphabet = implode($alphabet_array);
    foreach($alphabet_array as $row)
    {
        $i = 0;
        foreach($alphabet_array as $column)
        {
            $table[$row][$column] = $alphabet[$i++];
        }
        $alphabet = substr($alphabet, 1).substr($alphabet, 0, 1);
    }
    if(!$target or !$key) return false;
    $len = strlen($target);
    while(strlen($key) < $len) $key .= $key;
    $output = '';
    for($i = 0; $i < $len; $i++)
    {
        if($decode)
        {
            $letter_array = array_keys($table[$key[$i]], $target[$i]);
            $output .= $letter_array[0];
        }
        else
        {
            $output .= $table[$key[$i]][$target[$i]];
        }
    }
    return $output;
}

?>
Post Reply