_Modulus = 238328; $this->errMsg = ""; $this->_enMult = $vintMult; $this->_enAdd = $vintAdd; $this->_deMult = $this->EuclidAlg($this->_enMult,$this->_Modulus); $this->_deAdd = ($this->BigMod ($this->ClockMod(-1 * $this->_enAdd, $this->_Modulus), $this->_deMult)) % $this->_Modulus; } function ModShift($vArray,$vintMult,$vintAdd){ /*************************************************************************** * Converts an array of integers using a given multiply and add key. * This function can be used for either encyphering or decyphering * returns an array of new values ***************************************************************************/ for($n = 0 ; $n < count($vArray); $n++){ $vArray[$n] = ($this->BigMod($vArray[$n],$vintMult) + $vintAdd) % $this->_Modulus; } return $vArray; } function Cypher($vstrPlain){ /*************************************************************************** * Converts plain text to cypher text * i) removes all spaces and prefixes with 0's if required * ii) converts each group of 3 characters to an integer * iii)shifts these integers * iv) converts the integers to groups of 3 characters * * returns a string of cypher text ***************************************************************************/ $strRetVal = ""; $tempArray = array(); $strRetVal = trim(str_replace(" ",'',$vstrPlain)); while(strlen($strRetVal)% 3 != 0){ $strRetVal = '0'.$strRetVal; } for($n = 0 ; $n < strlen($strRetVal)/3 ; $n++){ $temp = substr($strRetVal , 3*$n, 3); $tempArray[$n] = $this->toTrigraph($temp); } //encrypt array $tempArray = $this->ModShift($tempArray,$this->_enMult,$this->_enAdd); $strRetVal = ""; for($n = 0 ; $n < count($tempArray); $n++){ $strRetVal .= $this->fromTrigraph($tempArray[$n]); } return $strRetVal; } function DeCypher($vstrCyph){ /*************************************************************************** * Converts cypher text to plain text * i) removes all spaces * ii) converts each group of 3 characters to an integer * iii)shifts these integers * iv) converts the integers to groups of 3 characters * v) removes any leading zeros * returns a string of plain text ***************************************************************************/ $strRetVal = ""; $tempArray = array(); $strRetVal = trim(str_replace(" ",'',$vstrCyph)); for($n = 0 ; $n < strlen($strRetVal)/3 ; $n++){ $temp = substr($strRetVal , 3*$n, 3); $tempArray[$n] = $this->toTrigraph($temp); } //decrypt array $tempArray = $this->ModShift($tempArray,$this->_deMult,$this->_deAdd); $strRetVal = ""; for($n = 0 ; $n < count($tempArray); $n++){ $strRetVal .= $this->fromTrigraph($tempArray[$n]); } //remove leading zeros while(substr($strRetVal,0,1)=='0' && strlen($strRetVal)> 1){ $strRetVal = substr($strRetVal,1); } return $strRetVal; } function toTrigraph($vstrTri){ /*************************************************************************** * converts a three character string to an integer * eg "pQ1" to 197657 * returns the integer value ***************************************************************************/ $RetVal = ""; $RetVal = $this->myAsc(substr($vstrTri, 0,1)) * 3844 + $this->myAsc(substr($vstrTri, 1,1)) * 62 + $this->myAsc(substr($vstrTri, -1)); return $RetVal; } function fromTrigraph($vintTri){ /*************************************************************************** * converts an integer to a three character string * eg 197657 to "pQ1" * returns a three character string ***************************************************************************/ $RetVal = ""; $RetVal = $this->myChar(floor($vintTri / 3844)) .$this->myChar(floor(($vintTri % 3844) / 62)) .$this->myChar($vintTri % 62); return $RetVal; } function myAsc($vstrChar){ /*************************************************************************** * converts a single character (AlphaNumeric) to an integer * based on Ascii values * returns the integer ***************************************************************************/ $RetVal = ""; if (ereg("[0-9]",$vstrChar)) $RetVal = ord($vstrChar) - 48; if (ereg("[A-Z]",$vstrChar)) $RetVal = ord($vstrChar) - 55; if (ereg("[a-z]",$vstrChar)) $RetVal = ord($vstrChar) - 61; return $RetVal; } function myChar($vintChar){ /*************************************************************************** * converts an integer between 0 - 61 * to the equivalent string character ***************************************************************************/ $RetVal = ""; if($vintChar < 10){ return $vintChar; } if($vintChar < 36){ return Chr($vintChar + 55); } if($vintChar < 62){ return Chr($vintChar + 61); } return $RetVal; } function EuclidAlg($vintA, $vintB){ /*************************************************************************** * implementation of Euclids extended algorithrim for finding the * multiplicative inverse of a number with respect to a given modulus * $vintB The modulus * $vintA The number of which the inverse is required * sets an error message if the inputs are not relatively prime * returns the inverse ***************************************************************************/ $intSmall=1; //temp value to prevent div by zero warning $intInvSmall = 1; $intInvBig = 0; if ($vintA >= $vintB){ $intBig = $vintA; $intSmall = $vintB; }else{ $intSmall = $vintA; $intBig = $vintB; } $intBigStatic = $intBig; do{ $intTemp = $intBig % $intSmall; $intFacTemp = $intInvBig - $intInvSmall * ($intBig - $intTemp) / $intSmall; $intInvBig = $intInvSmall; $retval = $this->ClockMod($intInvSmall, $intBigStatic); $intInvSmall = $intFacTemp; $intRemain = $intSmall; $intBig = $intSmall; $intSmall = $intTemp; }while($intTemp > 0); if ($intRemain != 1){ $this->errMsg = "Not relatively prime"; } return $retval; } function ClockMod($lhs, $rhs){ /*************************************************************************** * A wrapper for modular divison which does not return negative numbers * returns the remainder or modulus + remainder ***************************************************************************/ $intAns = $lhs % $rhs; if ($intAns < 0) { $intAns += $rhs; } return $intAns; } function BigMod($intVar1 ,$intVar2){ /*************************************************************************** * wrapper for multiplication and modular division which prevents overflow * ($intVar1*$intVar2) % mod * returns the remainder ***************************************************************************/ $temp = log($intVar1) + log($intVar2); $temp = exp($temp - log($this->_Modulus)); //$temp now is (Var1 * Var2)/mod $retval = ($temp - floor($temp)) * $this->_Modulus; //decimal part * mod = remainder return round($retval); } } //end of class ?>