Skip to content

Instantly share code, notes, and snippets.

@sarfraznawaz2005
Last active October 12, 2015 18:32
Show Gist options
  • Save sarfraznawaz2005/88dfb10615a8d06660d4 to your computer and use it in GitHub Desktop.
Save sarfraznawaz2005/88dfb10615a8d06660d4 to your computer and use it in GitHub Desktop.
PHP Toggle Encypt Decrypt Function
<?php
function toggleEncryption($text, $key = '') {
// return text unaltered if the key is blank
if ($key == '') {
return $text;
}
// remove the spaces in the key
$key = str_replace(' ', '', $key);
if (strlen($key) < 8) {
exit('key error');
}
// set key length to be no more than 32 characters
$key_len = strlen($key);
if ($key_len > 32) {
$key_len = 32;
}
$k = array(); // key array
// fill key array with the bitwise AND of the ith key character and 0x1F
for ($i = 0; $i < $key_len; ++$i) {
$k[$i] = ord($key{$i}) & 0x1F;
}
// perform encryption/decryption
for ($i = 0; $i < strlen($text); ++$i) {
$e = ord($text{$i});
// if the bitwise AND of this character and 0xE0 is non-zero
// set this character to the bitwise XOR of itself
// and the ith key element, wrapping around key length
// else leave this character alone
if ($e & 0xE0) {
$text{$i} = chr($e ^ $k[$i % $key_len]);
}
}
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment