Skip to content

Instantly share code, notes, and snippets.

@LogIN-
Last active January 29, 2023 18:29
Show Gist options
  • Save LogIN-/e451ab0e8738138bc60b to your computer and use it in GitHub Desktop.
Save LogIN-/e451ab0e8738138bc60b to your computer and use it in GitHub Desktop.
PHP custom encode decode functions
<?php
// Updated code from comments
function encode($value) {
if (!$value) {
return false;
}
$key = sha1('EnCRypT10nK#Y!RiSRNn');
$strLen = strlen($value);
$keyLen = strlen($key);
$j = 0;
$crypttext = '';
for ($i = 0; $i < $strLen; $i++) {
$ordStr = ord(substr($value, $i, 1));
if ($j == $keyLen) {
$j = 0;
}
$ordKey = ord(substr($key, $j, 1));
$j++;
$crypttext .= strrev(base_convert(dechex($ordStr + $ordKey), 16, 36));
}
return $crypttext;
}
function decode($value) {
if (!$value) {
return false;
}
$key = sha1('EnCRypT10nK#Y!RiSRNn');
$strLen = strlen($value);
$keyLen = strlen($key);
$j = 0;
$decrypttext = '';
for ($i = 0; $i < $strLen; $i += 2) {
$ordStr = hexdec(base_convert(strrev(substr($value, $i, 2)), 36, 16));
if ($j == $keyLen) {
$j = 0;
}
$ordKey = ord(substr($key, $j, 1));
$j++;
$decrypttext .= chr($ordStr - $ordKey);
}
return $decrypttext;
}
?>
@harryqt
Copy link

harryqt commented May 25, 2020

Beautified version.

function encode($value) {
    if (!$value) {
        return false;
    }

    $key = sha1('EnCRypT10nK#Y!RiSRNn');
    $strLen = strlen($value);
    $keyLen = strlen($key);
    $j = 0;
    $crypttext = '';

    for ($i = 0; $i < $strLen; $i++) {
        $ordStr = ord(substr($value, $i, 1));
        if ($j == $keyLen) {
            $j = 0;
        }
        $ordKey = ord(substr($key, $j, 1));
        $j++;
        $crypttext .= strrev(base_convert(dechex($ordStr + $ordKey), 16, 36));
    }

    return $crypttext;
}


function decode($value) {
    if (!$value) {
        return false;
    }

    $key = sha1('EnCRypT10nK#Y!RiSRNn');
    $strLen = strlen($value);
    $keyLen = strlen($key);
    $j = 0;
    $decrypttext = '';

    for ($i = 0; $i < $strLen; $i += 2) {
        $ordStr = hexdec(base_convert(strrev(substr($value, $i, 2)), 36, 16));
        if ($j == $keyLen) {
            $j = 0;
        }
        $ordKey = ord(substr($key, $j, 1));
        $j++;
        $decrypttext .= chr($ordStr - $ordKey);
    }

    return $decrypttext;
}

@lewatt23
Copy link

Beautified version.

function encode($value) {
    if (!$value) {
        return false;
    }

    $key = sha1('EnCRypT10nK#Y!RiSRNn');
    $strLen = strlen($value);
    $keyLen = strlen($key);
    $j = 0;
    $crypttext = '';

    for ($i = 0; $i < $strLen; $i++) {
        $ordStr = ord(substr($value, $i, 1));
        if ($j == $keyLen) {
            $j = 0;
        }
        $ordKey = ord(substr($key, $j, 1));
        $j++;
        $crypttext .= strrev(base_convert(dechex($ordStr + $ordKey), 16, 36));
    }

    return $crypttext;
}


function decode($value) {
    if (!$value) {
        return false;
    }

    $key = sha1('EnCRypT10nK#Y!RiSRNn');
    $strLen = strlen($value);
    $keyLen = strlen($key);
    $j = 0;
    $decrypttext = '';

    for ($i = 0; $i < $strLen; $i += 2) {
        $ordStr = hexdec(base_convert(strrev(substr($value, $i, 2)), 36, 16));
        if ($j == $keyLen) {
            $j = 0;
        }
        $ordKey = ord(substr($key, $j, 1));
        $j++;
        $decrypttext .= chr($ordStr - $ordKey);
    }

    return $decrypttext;
}

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment