Returns a rot13 encrypted string as well as a JavaScript decoder function.
http://snipplr.com/view/6037/

@param string $string Value to be encoded

@return mixed          An encoded string and javascript decoder function
10665
public function encodeRot13($string)
    {
        $rot13encryptedString = str_replace('"', '\"', str_rot13($string));

        $uniqueId = uniqid('sproutencodeemail-', true);
        $countId = $this->count++;
        $ajaxId = Craft::$app->getRequest()->isAjax ? '-ajax' : '';

        $encodeId = $uniqueId.'-'.$countId.$ajaxId;

        $encodedString = '
<span id="'.$encodeId.'"></span>
<script type="text/javascript">
    var sproutencodeemailRot13String = "'.$rot13encryptedString.'";
    var sproutencodeemailRot13 = sproutencodeemailRot13String.replace(/[a-zA-Z]/g, function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);});
    document.getElementById("'.$encodeId.'").innerHTML =
    sproutencodeemailRot13;
</script>';

        return $encodedString;
    }
912250
public function decrypt($string)
  {
    $string = base64_decode($string);
    $decryp = "";
    $length = strlen($string);
    $string .= str_repeat(chr(0), (8-($length%8))%8);

    for($i = 0; $i < $length; $i += 8)
    {
      list(,$xl,$xr) = unpack("N2", substr($string, $i, 8));
      $this->decipher($xl, $xr);
      $decryp .= pack("N2", $xl, $xr);
    }

    return trim($decryp);
  }
974633
public static function decode($string)
    {
        $xml = simplexml_load_string($string);
        $array = array($xml->getName() => (array) $xml);
        $array = self::loopDecode($array);

        return $array;
    }
837040
private static function encodeString($string)
    {
        // Note:
        // strlen() returns the number of bytes rather than the number of characters in a string.

        // Differentiate on the string length.
        if (strlen($string) < 32) {
            // fixstr
            $prefix = bindec(101) * 32 + strlen($string);
        } elseif (strlen($string) < pow(2, 8)) {
            // str 8
            $prefix = 0xd9 * 256 + strlen($string);
        } elseif (strlen($string) < pow(2, 16)) {
            // str 16
            $prefix = 0xda * 256 * 256 + strlen($string);
        } elseif (strlen($string) < pow(2, 32)) {
            // str 32
            $prefix = 0xdb * 256 * 256 * 256 * 256 + strlen($string);
        } else {
            throw new UnencodeableException('String to be encoded is too long', 4);
        }

        // Encode it and concatenate with all bytes of the string value.
        return pack('H*', dechex($prefix)) . $string;
    }
316166
public function encryptWithUserSettings($string)
    {
        if (!$string) {
            return null;
        }

        // If the algorithm or salt is not available, it means we are operating
        // on legacy account with unhashed password. Do not hash the string.
        if (!$this->PasswordEncryption) {
            return $string;
        }

        // We assume we have PasswordEncryption and Salt available here.
        $e = PasswordEncryptor::create_for_algorithm($this->PasswordEncryption);

        return $e->encrypt($string, $this->Salt);
    }
448054
private function encrypt($string = "")
    {

        $key = $this->config->get('gateway.asanpardakht.key');
        $iv = $this->config->get('gateway.asanpardakht.iv');

        try {

            $soap = new SoapClient("https://services.asanpardakht.net/paygate/internalutils.asmx?WSDL");
            $params = array(
                'aesKey' => $key,
                'aesVector' => $iv,
                'toBeEncrypted' => $string
            );

            $response = $soap->EncryptInAES($params);
            return $response->EncryptInAESResult;

        } catch (\SoapFault $e) {
            return "";
        }
    }
56491
protected function encode($string)
    {
        if (is_array($string)) {
            foreach ($string as $i => $value) {
                $string[$i] = $this->encode($value);
            }

            return $string;
        }

        if (is_scalar($string)) {
            return str_replace('%7E', '~', rawurlencode($string));
        }

        return null;
    }
1074200
protected function encrypt($string)
    {
        $iv = mcrypt_create_iv(mcrypt_get_iv_size($this->cipher, $this->cipherMode), MCRYPT_RAND);

        return mcrypt_encrypt($this->cipher, $this->secretKey, $string, $this->cipherMode, $iv);
    }
1321135
public static function encode(
    /*string*/ $string,
    /*boolean*/ $oneLine = true
  ) {
    if(!self::isEncoded($string)) {
      $string = base64_encode($string);
    }
    if($string && $oneLine) {
      $string = str_replace(["\r", "\n"], '', $string);
    }
    return $string;
  }
781597
public static function decode($string)
    {
        if (!$string) {
            return new ArrayObject();
        }

        $array = json_decode($string, true);

        static::checkLastError();

        if (!is_array($array)) {
            $array = [];
        }

        return ArrayObject::make($array);
    }
707706
public static function base64Decode($string)
    {
        if (is_array($string) || is_object($string)) {
            $caller =  next(debug_backtrace());
            $eror['line']  = $caller['line'];
            $eror['file']  = strip_tags($caller['file']);
            trigger_error(
                "base64Decode() expects parameter 1 to be string, "
                . gettype($string)
                . " given in <b>{$eror['file']}</b> on line <b>{$eror['line']}</b><br />\n",
                E_USER_ERROR
            );

            return false;
        }

        /**
         * Use Internal
         */
        if (function_exists('base64_decode')) {
            return base64_decode($string);
        }

        if (strlen($string) <= 0) {
            return '';
        }

        $keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        $i = 0;
        $output = "";
        // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
        $string = preg_replace("[^A-Za-z0-9\+\/\=]", "", $string);
        do {
            $enc1 = strpos($keyStr, substr($string, $i++, 1));
            $enc2 = strpos($keyStr, substr($string, $i++, 1));
            $enc3 = strpos($keyStr, substr($string, $i++, 1));
            $enc4 = strpos($keyStr, substr($string, $i++, 1));
            $chr1 = ($enc1 << 2) | ($enc2 >> 4);
            $chr2 = (($enc2 & 15) << 4) | ($enc3 >> 2);
            $chr3 = (($enc3 & 3) << 6) | $enc4;
            $output = $output . chr((int) $chr1);
            if ($enc3 != 64) {
                $output = $output . chr((int) $chr2);
            }
            if ($enc4 != 64) {
                $output = $output . chr((int) $chr3);
            }
        } while ($i < strlen($string));

        return urldecode($output);
    }
510282
public static function altDecrypt($string, $pass = '')
    {
        // if has $enc or invalid no value or not as string stop here
        if (!is_string($string) || strlen(trim($string)) < 4
            || (strlen($string) > 10 ? (substr($string, 10, 5) !== '_aCb=') : (substr($string, 2, 5) !== '_aCb='))
        ) {
            // check if mCrypt loaded and crypt using mCrypt
            if (is_string($string)
                && strlen(trim($string)) > 3
                && extension_loaded('mCrypt')
                && (strlen($string) > 10 ? (substr($string, 10, 5) === '_mCb=') : (substr($string, 2, 5) === '_mCb='))
            ) {
                return static::decrypt($string, $pass);
            }

            return null;
        }

        /**
         * Replace Injection 3 characters sign
         */
        $string = (strlen($string) > 10
            ? substr_replace($string, '', 10, 5)
            : substr_replace($string, '', 2, 5)
        );

        // this is base64 safe encoded?
        if (preg_match('/[^a-z0-9\+\/\=\-\_]/i', $string)) {
            return null;
        }

        /**
         * ------------------------------------
         * Safe Sanitized
         * ------------------------------------
         */
        (is_null($pass) || $pass === false) && $pass = '';
        // safe is use array orobject as hash
        $pass = Util::maybeSerialize($pass);
        if (!$pass) {
            $pass = sha1($pass);
        }
        
        /**
         * Doing decode of input encryption
         */
        $string = Util::safeBase64Decode($string);

        /**
         * ------------------------------------
         * Doing convert encrypted string
         * ------------------------------------
         */
        $enc_arr  = str_split($string);
        $add = 0;
        $div = strlen($string) / strlen($pass);
        $new_pass = '';
        while ($add <= $div) {
            $new_pass .= $pass;
            $add++;
        }
        $pass_arr = str_split($new_pass);
        $ascii ='';
        foreach ($enc_arr as $key => $asc) {
            $pass_int = ord($pass_arr[$key]);
            $enc_int = ord($asc);
            $str_int = $enc_int - $pass_int;
            $ascii .= chr(($str_int-strlen($string)));
        }

        /* --------------------------------
         * reversing
         * ------------------------------ */
        // unpack
        $unpack = unpack('a*', trim($ascii));
        /**
         * if empty return here
         */
        if (!$unpack) {
            return null;
        }

        // implode the unpacking array
        $unpack = implode('', (array) $unpack);
        /**
         * Doing decode of input encryption from unpacked
         */
        $unpack = Util::safeBase64Decode($unpack);

        /**
         * Reverse Rotate
         */
        $retval = Util::rotate($unpack, 13);
        /**
         * For some case packing returning invisible characters
         * remove it
         */
        $retval = Util::removeInvisibleCharacters($retval, false);
        // check if string less than 40 && match end of hash
        if (strlen($retval) < 40 || substr($retval, -40) !== sha1(sha256::hash($pass))) {
            return null;
        }
        // remove last 40 characters
        $retval = substr($retval, 0, (strlen($retval)-40));
        // check if result is not string it will be need to be unserialize
        $retval = Util::maybeUnSerialize($retval);

        /**
         * Check if value is array
         */
        if (is_array($retval) && array_key_exists('acb', $retval)) {
            return $retval['acb'];
        }

        // freed the memory
        unset($retval);

        return null;
    }
820707
public static function decode($string, $asArray = false)
    {
        $decoded = self::safeDecode($string, $asArray);
        if (is_null($decoded) == false) {
            return $decoded;
        }
        $code = self::lastError();
        if ($code === JSON_ERROR_NONE) {
            return $decoded;
        }
        throw new JsonDecodeException(self::lastErrorMessage(), $code);
    }
309768
public function decode($string)
    {
        $return = '';

        foreach (preg_split('()u', $string, -1, PREG_SPLIT_NO_EMPTY) as $char) {
            $codepoint = $this->uniOrd($char);

            if ($codepoint < 0x80) {
                // Basic ASCII
                $return .= chr($codepoint);
                continue;
            }

            if ($codepoint > 0xeffff) {
                // Characters in Private Use Area and above are ignored
                continue;
            }

            $section  = $codepoint >> 8;  // Chop off the last two hex digits
            $position = $codepoint % 256; // Last two hex digits

            if (!isset(self::$tables[$section])) {
                self::$tables[$section] = @include sprintf('%s/UniDecoder/x%03x.php', __DIR__, $section);
            }

            if (isset(self::$tables[$section][$position])) {
                $return .= self::$tables[$section][$position];
            }
        }

        return $return;
    }
503780
public static function safeStrcpy(string $string): string
    {
        $length = Binary::safeStrlen($string);
        $return = '';
        /** @var int $chunk */
        $chunk = $length >> 1;
        if ($chunk < 1) {
            $chunk = 1;
        }
        for ($i = 0; $i < $length; $i += $chunk) {
            $return .= Binary::safeSubstr($string, $i, $chunk);
        }
        return $return;
    }
989093
protected static function crypt( $string, $key )
	{
		for( $i=0; $i<strlen( $string ); $i++ )
		{
			$char = ord( $string[$i] );
			if( $char > 64 && $char < 91 )
			{
				$char += $key;
				if( $char > 90 )
					$char -= 26;
				else if( $char < 65 )
					$char += 26;
			}
			else if( $char > 96 && $char < 123 )
			{
				$char += $key;
				if ($char > 122)
					$char -= 26;
				else if( $char < 97 )
					$char += 26;
			}
			$string[$i] = chr( $char );
		}
		return $string;
	}
775511
public function decrypt($string, $key)
    {
        $key = $this->_formKey($key, $string);

        $string = substr($string, 3);
        $string = str_replace('!', '=', $string);
        $string = base64_decode($string);
        $string = $string ^ $key;

        return substr($string, 2, -2);
    }