예제 #1
0
파일: DES.php 프로젝트: chansolo/TeamPass
 /**
  * This is where the actual encrypt/decryption takes place. Since
  * encryption and decryption are the same algorithm in DES, we only
  * need one function to do both.
  *
  * @param string $data The string to be encrypted or decrypted
  * @return boolean Returns true
  */
 protected function des(&$data)
 {
     $l = array();
     $r = array();
     // step two: Initial Permutation (IP) of plaintext
     $data = $this->ip($data);
     // divide the permuted block IP into a left half L0 of 32 bits,
     // and a right half R0 of 32 bits
     $l[0] = substr($data, 0, 32);
     $r[0] = substr($data, 32, 32);
     for ($n = 1; $n <= 16; ++$n) {
         $l[$n] = $r[$n - 1];
         if ($this->operation() == parent::DECRYPT) {
             $f = $this->F($r[$n - 1], $this->sub_keys[16 - $n]);
         } else {
             $f = $this->F($r[$n - 1], $this->sub_keys[$n - 1]);
         }
         // XOR F with Ln
         $r[$n] = $this->xorBin($l[$n - 1], $f);
     }
     // now we combine L[16] and R[16] back into a 64-bit string, but we reverse
     // L[16] and R[16] so that it becomes R[16]L[16]
     $data = $r[16] . $l[16];
     // now do the final permutation
     $data = $this->fp($data);
     $data = parent::bin2Str($data);
     return true;
 }