Skip to content

Instantly share code, notes, and snippets.

@CodelineRed
Last active January 19, 2022 09:12
Show Gist options
  • Save CodelineRed/c38f513f28d6f9b778912f110c565348 to your computer and use it in GitHub Desktop.
Save CodelineRed/c38f513f28d6f9b778912f110c565348 to your computer and use it in GitHub Desktop.
This is an email encrypt / decrypt extracted from TYPO3. It is useful to protecting your email against spam bots.
<?php
$email = 'software@insanitymeetshh.net';
$emailEncode = quoteJSvalue(encryptEmail($email, '-2'));
function quoteJSvalue($value) {
return strtr(
json_encode((string)$value, JSON_HEX_AMP|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_TAG),
[
'"' => '\'',
'\\\\' => '\\u005C',
' ' => '\\u0020',
'!' => '\\u0021',
'\\t' => '\\u0009',
'\\n' => '\\u000A',
'\\r' => '\\u000D'
]
);
}
function encryptCharcode($n, $start, $end, $offset) {
$n = $n + $offset;
if ($offset > 0 && $n > $end) {
$n = $start + ($n - $end - 1);
} elseif ($offset < 0 && $n < $start) {
$n = $end - ($start - $n - 1);
}
return chr($n);
}
function encryptEmail($string, $type) {
$out = '';
// obfuscates using the decimal HTML entity references for each character
if ($type === 'ascii') {
$stringLength = strlen($string);
for ($a = 0; $a < $stringLength; $a++) {
$out .= '&#' . ord(substr($string, $a, 1)) . ';';
}
} else {
// like str_rot13() but with a variable offset and a wider character range
$len = strlen($string);
$offset = (int)$type;
for ($i = 0; $i < $len; $i++) {
$charValue = ord($string[$i]);
// 0-9 . , - + / :
if ($charValue >= 43 && $charValue <= 58) {
$out .= encryptCharcode($charValue, 43, 58, $offset);
} elseif ($charValue >= 64 && $charValue <= 90) {
// A-Z @
$out .= encryptCharcode($charValue, 64, 90, $offset);
} elseif ($charValue >= 97 && $charValue <= 122) {
// a-z
$out .= encryptCharcode($charValue, 97, 122, $offset);
} else {
$out .= $string[$i];
}
}
}
return $out;
}
?>
<html>
<head>
<script type="text/javascript">
/*<![CDATA[*/
/*_scriptCode*/
// decrypt helper function
function decryptCharcode(n,start,end,offset) {
n = n + offset;
if (offset > 0 && n > end) {
n = start + (n - end - 1);
} else if (offset < 0 && n < start) {
n = end - (start - n - 1);
}
return String.fromCharCode(n);
}
// decrypt string
function decryptString(enc,offset) {
var dec = "";
var len = enc.length;
for(var i=0; i < len; i++) {
var n = enc.charCodeAt(i);
if (n >= 0x2B && n <= 0x3A) {
dec += decryptCharcode(n,0x2B,0x3A,offset); // 0-9 . , - + / :
} else if (n >= 0x40 && n <= 0x5A) {
dec += decryptCharcode(n,0x40,0x5A,offset); // A-Z @
} else if (n >= 0x61 && n <= 0x7A) {
dec += decryptCharcode(n,0x61,0x7A,offset); // a-z
} else {
dec += enc.charAt(i);
}
}
return dec;
}
// decrypt spam-protected emails
function linkTo_UnCryptMailto(s) {
location.href = 'mailto:' + decryptString(s,2);
//alert(decryptString(s,2));
}
/*]]>*/
</script>
</head>
<body>
<?php echo $email . '<br/>'; ?>
<?php echo '<a href="javascript:linkTo_UnCryptMailto(' . $emailEncode . ');">' . $emailEncode . '</a>'; ?>
</body>
</html>
@inam4
Copy link

inam4 commented Apr 19, 2021

can you give a python version of this?

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