Exemple #1
0
 protected function encryptString($str)
 {
     if ($this->bits % 8 != 0) {
         throw new \RuntimeException("Number of bits should be divisable by 8 when encrypting a string!");
     }
     $step = $this->bits / 8;
     $size = strlen($str);
     if (!$size) {
         return '';
     }
     $result = '';
     $compensation = 0;
     for ($offset = 0; $offset < $size; $offset += $step) {
         $substr = substr($str, $offset, $step);
         if (strlen($substr) != $step) {
             $compensation = $step - strlen($substr);
             $substr .= str_repeat("", $compensation);
         }
         $unpacked = unpack('C*', $substr);
         $val = 0;
         $first = true;
         foreach ($unpacked as $num) {
             if ($first) {
                 $first = false;
             } else {
                 $val <<= 8;
             }
             $val += $num & 0xff;
         }
         $encrypted = $this->encrypt($val);
         for ($i = 0; $i < $step; ++$i) {
             $partition = $encrypted & 0xff;
             $packed = pack('C', $partition);
             $result .= $packed;
             $encrypted = CommonUtils::unsignedRightShift($encrypted, 8);
         }
     }
     return pack('C', $compensation) . $result;
 }