Dcrypt\Rc4::crypt PHP Method

crypt() public static method

Perform (en/de)cryption
public static crypt ( string $str, string $key ) : string
$str string String to be encrypted
$key string Key to use for encryption
return string
    public static function crypt($str, $key)
    {
        $s = self::initializeState($key);
        $i = $j = 0;
        $res = '';
        $size = Str::strlen($str);
        for ($y = 0; $y < $size; $y++) {
            $i = ($i + 1) % 256;
            $j = ($j + $s[$i]) % 256;
            $x = $s[$i];
            $s[$i] = $s[$j];
            $s[$j] = $x;
            $res .= $str[$y] ^ \chr($s[($s[$i] + $s[$j]) % 256]);
        }
        return $res;
    }

Usage Example

Beispiel #1
0
 public function testVector()
 {
     /*
      * Test that known cypher text decrypts properly
      */
     $cyphertext = hex2bin('140ad3d278a229ff3c487d');
     $plain = 'Hello World';
     $key = 'asdf';
     $this->assertEquals($plain, Rc4::crypt($cyphertext, $key));
 }