Page 1 of 1

how to encrypt data and then decrypt to use it

Posted: Tue Dec 19, 2006 8:36 am
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

Posted: Tue Dec 19, 2006 9:10 am
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.

Posted: Tue Dec 19, 2006 9:11 am
by John Cartwright

Posted: Tue Dec 19, 2006 9:43 am
by arukomp
thanks a lot, I'll at it

Posted: Tue Dec 19, 2006 9:48 am
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 :)

Posted: Tue Dec 19, 2006 10:06 am
by feyd
I don't understand what you meant.

Posted: Tue Dec 19, 2006 10:17 am
by arukomp
I looked further in these functions descriptions and my question disappeared :) I have no questions now :)

Posted: Tue Dec 19, 2006 3:55 pm
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;
}

?>