Skip to content

Instantly share code, notes, and snippets.

@asterite
Created September 13, 2011 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asterite/1213771 to your computer and use it in GitHub Desktop.
Save asterite/1213771 to your computer and use it in GitHub Desktop.
My crypto algorithm
<?
/*
* Encrypts any number into a 10-digit cypher.
* Oh, 10 here means 10 in a random base.
*/
class Encrypter
{
function encrypt($num)
{
$str = "";
$x = rand(1, 9);
$str .= $x;
for($i = 0; $i < $x; $i++) {
$str .= rand(0, 9);
}
while($num != 0) {
$y = $num % 10;
$z = ($y + $x) % 10;
$str .= $z;
for($i = 0; $i < $y; $i++) {
$str .= rand(0, 9);
}
$num = (int)($num / 10);
}
return $str;
}
function decrypt($text)
{
$i = 0;
$m = 1;
$n = 0;
$x = intval($text{$i});
$i += $x + 1;
while($i < strlen($text)) {
$z = $text{$i};
$z -= $x;
if ($z < 0) $z += 10;
$n += $m * $z;
$i += $z + 1;
$m *= 10;
}
return $n;
}
}
$num = 1234;
$crypter = new Encrypter();
$crypted = $crypter->encrypt($num);
$decrypted = $crypter->decrypt($crypted);
echo "Original: $num, Encrypted: $crypted, Decrypted: $decrypted\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment