Last active
March 17, 2021 02:40
-
-
Save binsarjr/934626d5e7e67fb027cccd4e5fddd303 to your computer and use it in GitHub Desktop.
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
<?php | |
function caesar_cipher_encode(string $text, int $key = 1) | |
{ | |
for ($i=0;$i<strlen($text);$i++) { | |
$code[$i] = ord($text[$i]); //rubah ASCII ke desimal | |
$b[$i] = ($code[$i] + $key) % 256; //proses enkripsi | |
$c[$i] = chr($b[$i]); //rubah desimal ke ASCII | |
} | |
$encoded = ''; | |
for ($i=0;$i<strlen($text);$i++) { | |
$encoded .= $c[$i]; | |
} | |
return $encoded; | |
} | |
function caesar_cipher_decode(string $text, int $key = 1) | |
{ | |
for ($i=0;$i<strlen($text);$i++) { | |
$kode[$i]=ord($text[$i]); // rubah ASII ke desimal | |
$b[$i]=($kode[$i] - $key) % 256; // proses dekripsi Caesar | |
$c[$i]=chr($b[$i]); //rubah desimal ke ASCII | |
} | |
$decoded = ''; | |
for ($i=0;$i<strlen($text);$i++) { | |
$decoded .= $c[$i]; | |
} | |
return $decoded; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment