Created
September 13, 2011 13:10
-
-
Save asterite/1213771 to your computer and use it in GitHub Desktop.
My crypto algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/* | |
* 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