Here is the code I am using:
Actual Log In Page:
<?php
require_once('validateLogInForm.php');
$crypt = new encryption_class;
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = &$_POST['username'];
$password = &$_POST['password'];
$pswdlen = (int)$_POST['pswdlen'];
$adj = &$_POST['adj'];
$crypt->setAdjustment($adj);
$mod = &$_POST['mod'];
$crypt->setModulus($mod);
} else {
$username = NULL;
$password = NULL;
$pswdlen = 16;
}
$adj = $crypt->getAdjustment();
$mod = $crypt->getModulus();
$errors = array();
//database login
$dsn = 'mysql:host=XXX;dbname=XXX';
$username='XXX';
$password='XXX';
//variable for errors
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
//try to run code
try {
//object to open database
$db = new PDO($dsn,$username,$password, $options);
//check username against password
$SQL = $db->prepare("Select USER_PASSWORD FROM user WHERE user_name = :UserName");
$SQL->bindValue(':UserName', $username);
$SQL->execute();
$username = $SQL->fetch();
if($username === false)
{
$password = null;
}
else
{
$password = $username['USER_PASSWORD'];
include index.php;
}
return $password;
$SQL->closeCursor();
$db = null;
} catch(PDOException $e){
$error_message = $e->getMessage();
echo("<p>Database Error: $error_message</p>");
exit();
}
function showForm($formMessage){
echo $formMessage;
}
?>
Validate Page
<?php
require_once('logIn.php');
class encryption_class {
var $scramble1; // 1st string of characters
var $scramble2; // 2nd string of characters
var $errors; // array of error messages
var $adj; // 1st adjustment value
var $mod; // 2nd adjustment value
//create encryption class
function encryption_class ()
{
$this->errors = array();
//this scrambles user input
$this->scramble1 = '! #$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~';
$this->scramble2 = 'f^jAE]okIOzU[2&q1{3`h5w_794p@6s8?BgP>dFV=m D<TcS%Ze|r:lGK/uCy.Jx)HiQ!#$~(;Lt-R}Ma,NvW+Ynb*0X';
if (strlen($this->scramble1) <> strlen($this->scramble2)) {
trigger_error('** SCRAMBLE1 is not same length as SCRAMBLE2 **', E_USER_ERROR);
}
$this->adj = 1.75; // this value is added to the rolling fudgefactors
$this->mod = 3; // if divisible by this the adjustment is made negative
}
function decrypt ($password, $source)
// decrypt string into its original form
{
$this->errors = array();
// convert $password into a sequence of numbers
$fudgefactor = $this->_convertPassword($password);
if ($this->errors) return;
//if no user input show error message
if (empty($source)) {
$this->errors[] = 'Please Enter A Valid User Name and Password';
return;
}
$target = null;
$factor2 = 0;
for ($i = 0; $i < strlen($source); $i++) {
// extract a (multibyte) character from $source
if (function_exists('mb_substr')) {
$char2 = mb_substr($source, $i, 1);
} else {
$char2 = substr($source, $i, 1);
}
// identify its position in $scramble2
$num2 = strpos($this->scramble2, $char2);
if ($num2 === false) {
$this->errors[] = "Source string contains an invalid character ($char2)";
return;
}
// get a value using $fudgefactor
$adj = $this->_applyFudgeFactor($fudgefactor);
$factor1 = $factor2 + $adj; // $factor1 value
$num1 = $num2 - round($factor1); // generate $scramble1
$num1 = $this->_checkRange($num1); // check range value
$factor2 = $factor1 + $num2; // $factor2 value
// get character from $scramble1
if (function_exists('mb_substr')) {
$char1 = mb_substr($this->scramble1, $num1, 1);
} else {
$char1 = substr($this->scramble1, $num1, 1);
}
// add to $target string
$target .= $char1;
}
return rtrim($target);
}
function encrypt ($password, $source, $sourcelen = 0)
// encrypt string into big mess
{
$this->errors = array();
// convert $password into a sequence of numbers
$fudgefactor = $this->_convertPassword($password);
if ($this->errors) return;
if (empty($source)) {
$this->errors[] = 'Please Enter A Valid User Name and Password';
return;
}
// pad $source with spaces
$source = str_pad($source, $sourcelen);
$target = null;
$factor2 = 0;
for ($i = 0; $i < strlen($source); $i++) {
// extract a character from $source
if (function_exists('mb_substr')) {
$char1 = mb_substr($source, $i, 1);
} else {
$char1 = substr($source, $i, 1);
}
// identify its position in $scramble1
$num1 = strpos($this->scramble1, $char1);
if ($num1 === false) {
$this->errors[] = "Source string contains an invalid character ($char1)";
return;
}
// get value using $fudgefactor
$adj = $this->_applyFudgeFactor($fudgefactor);
$factor1 = $factor2 + $adj; // $factor1 value
$num2 = round($factor1) + $num1; // generate $scramble2
$num2 = $this->_checkRange($num2); // check range value
$factor2 = $factor1 + $num2; // $factor2 value
// extract character from $scramble2
if (function_exists('mb_substr')) {
$char2 = mb_substr($this->scramble2, $num2, 1);
} else {
$char2 = substr($this->scramble2, $num2, 1);
}
// add to $target string
$target .= $char2;
}
return $target;
}
function getAdjustment ()
// return the adjustment value
{
return $this->adj;
}
function getModulus ()
// return the modulus value
{
return $this->mod;
}
function setAdjustment ($adj)
// set the adjustment value
{
$this->adj = (float)$adj;
}
function setModulus ($mod)
// set the modulus value
{
$this->mod = (int)abs($mod); // must be a positive whole number
}
function _applyFudgeFactor (&$fudgefactor)
// return value based on the contents of $fudgefactor
{
$fudge = array_shift($fudgefactor); // get 1st number from array
$fudge = $fudge + $this->adj; // add in new value
$fudgefactor[] = $fudge; // add it to the end of array
if (!empty($this->mod)) { //was a mod entered?
if ($fudge % $this->mod == 0) { // is it divisible by modifier
$fudge = $fudge * -1; // if so then make it negative
}
}
return $fudge;
}
function _checkRange ($num)
// check that $num is valid
{
$num = round($num); // round up to nearest whole number
$limit = strlen($this->scramble1);
while ($num >= $limit) {
$num = $num - $limit; // value too high, so reduce it
} // while
while ($num < 0) {
$num = $num + $limit; // value too low, so increase it
} // while
return $num;
}
function _convertPassword ($password)
// convert $password into numbers
{
if (empty($password)) {
$this->errors[] = 'Please Enter A Valid User Name and Password';
return;
}
$array[] = strlen($password); // first entry in array is length of $password
$tot = 0;
for ($i = 0; $i < strlen($password); $i++) {
// get a character from $password
if (function_exists('mb_substr')) {
$char = mb_substr($password, $i, 1);
} else {
$char = substr($password, $i, 1);
}
// find its position in $scramble1
$num = strpos($this->scramble1, $char);
if ($num === false) {
$this->errors[] = "Key contains an invalid character ($char)";
return;
}
$array[] = $num; // store this in array
$tot = $tot + $num; // accumulate total
}
$array[] = $tot; // insert total
return $array;
}
}
?>

New Topic/Question
Reply




MultiQuote






|