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
how to encrypt data and then decrypt to use it
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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;
}
?>