src/AESKW.php
public static function unwrap(string $kek, string $key, bool $padding_enabled = false): string
    {
        $P = \str_split($key, 8);
        $A = $P[0];
        $N = \count($P);

        if (2 > $N) {
            throw new \InvalidArgumentException('Bad data');
        }
        if (2 === $N) {
            $B = self::decrypt($kek, $P[0].$P[1]);
            $unwrapped = self::getLSB($B);
            $A = self::getMSB($B);
        } else {
            $R = $P;
            for ($j = 5; $j >= 0; --$j) {
                for ($i = $N - 1; $i >= 1; --$i) {
                    $t = $i + $j * ($N - 1);
                    $B = self::decrypt($kek, (self::toXBits(64, $t) ^ $A).$R[$i]);
                    $A = self::getMSB($B);
                    $R[$i] = self::getLSB($B);
                }
            }
            unset($R[0]);

            $unwrapped = \implode('', $R);
        }
        if (false === self::checkInitialValue($unwrapped, $padding_enabled, $A)) {
            throw new \InvalidArgumentException('Integrity check failed!');
        }

        return $unwrapped;
    }
public static function unwrap(string $kek, string $key, bool $padding_enabled = false): string
    {
        $P = \str_split($key, 8);
        $A = $P[0];
        $N = \count($P);

        if (2 > $N) {
            throw new \InvalidArgumentException('Bad data');
        }
        if (2 === $N) {
            $B = self::decrypt($kek, $P[0].$P[1]);
            $unwrapped = self::getLSB($B);
            $A = self::getMSB($B);
        } else {
            $R = $P;
            for ($j = 5; $j >= 0; --$j) {
                for ($i = $N - 1; $i >= 1; --$i) {
                    $t = $i + $j * ($N - 1);
                    $B = self::decrypt($kek, (self::toXBits(64, $t) ^ $A).$R[$i]);
                    $A = self::getMSB($B);
                    $R[$i] = self::getLSB($B);
                }
            }
            unset($R[0]);

            $unwrapped = \implode('', $R);
        }
        if (false === self::checkInitialValue($unwrapped, $padding_enabled, $A)) {
            throw new \InvalidArgumentException('Integrity check failed!');
        }

        return $unwrapped;
    }
@param string $kek             The Key Encryption Key
@param string $key             The key to unwrap
@param bool   $padding_enabled If false, the AIV check must be RFC3394 compliant, else it must be RFC5649 or RFC3394 compliant

@return string The key unwrapped