Skip to content

Instantly share code, notes, and snippets.

@binsarjr
Last active March 17, 2021 02:40
Show Gist options
  • Save binsarjr/934626d5e7e67fb027cccd4e5fddd303 to your computer and use it in GitHub Desktop.
Save binsarjr/934626d5e7e67fb027cccd4e5fddd303 to your computer and use it in GitHub Desktop.
<?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