Implemetasi PHP Algoritma Kriptografi Monoalfabet

Algoritma Kriptografi Monoalfabet merupakan salah satu algoritma kriptografi klasik yang sederhana. Konsepnya adalah mengubah semua huruf alfabet dari plain text menjadi huruf alfabet yang lain sesuai sesuai dengan acak huruf yang dsebut key (kunci).

perhatikan tabel berikut :
Array Alfabet asliABCDE..Z
Array KunciGUBKS..Q

algoritma ini akan mengubah huruf sesuai dengan letak array-nya. jika dalam tabel diatas maka huruf "A" akan diganti "G", huruf "B" akan diganti "U" dst.

PHP
<?php
function monoalfabet_encode($string){
$value = "";
$str = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s",
"t","u","v","w","x","y","z"," ",",","?","!");
$key = array("d","h","a","r","m","b","k","t","i","c","e","n","l","o","f","g","j","p","q",
"s","u","v","w","x","y","z"," ",",","?","!");

//memecah string menjadi array sebelum di lakukan encode

$string = strtolower($string);
$strCount = strlen($string);
$chrCount = count($str);

for($i=0;$i<$strCount;$i++) {
$strReplace[$i] = substr($string, $i, 1);
for($j=0;$j<$chrCount;$j++){
if($strReplace[$i]==$str[$j]) {
$strNow[$i] = $key[$j];
$value = $value.$strNow[$i];
}
}
}
return $value;
}

function monoalfabet_decode($string){
$value = "";
$str = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s",
"t","u","v","w","x","y","z"," ",",","?","!");
$key = array("d","h","a","r","m","b","k","t","i","c","e","n","l","o","f","g","j","p","q",
"s","u","v","w","x","y","z"," ",",","?","!");

//memecah string menjadi array sebelum di lakukan decode

$string = strtolower($string);
$strCount = strlen($string);
$chrCount = count($key);

for($i=0;$i<$strCount;$i++) {
$strReplace[$i] = substr($string, $i, 1);
for($j=0;$j<$chrCount;$j++){
if($strReplace[$i]==$key[$j]) {
$strNow[$i] = $str[$j];
$value = $value.$strNow[$i];
}
}
}
return $value;
}

$kalimat = "Saya suka sama dia, tapi dia tidak suka sama saya";
echo "Kalimat : ".$kalimat."<br><br>";

$kalimatbaru = monoalfabet_encode($kalimat);
echo "Kalimat setelah enkripsi : ".$kalimatbaru,"<br><br>";

$kalimatlama = monoalfabet_decode($kalimatbaru);
echo "Kalimat setelah deskripsi : ".$kalimatlama,"<br><br>";
?>
Previous
Next Post »