Contoh Program Algoritma Kriptografi Polialfabet

Algoritma kriptografi Polialfabet akan menggantikan tiap karakter dengan lebih dari 1 karakter. Algoritma ini menggunakan kunci (key) yang karaketernya tidak boleh kembar. Algoritma ini termasuk dalam algoritma kriptografi klasik.

Perhatikan tabel berikut :
Array Alfabet Asli A B C D E F G .. Z
Array Alfabet Kunci 1 K R I P T O A .. Z
Array Alfabet Kunci 2 G R A F I B C .. Z

PHP
<?php
function polialfabet_encode($string){
$value_temp = "";
$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"," ",",","?","!");
$key2 = array("i","n","d","o","e","s","a","b","c","f","g","h","j","k","l","m","p","q","r",
"t","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_temp = $value_temp.$strNow[$i];
}
}
}

$string = strtolower($value_temp);
$strCount = strlen($value_temp);
$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] = $key2[$j];
$value = $value.$strNow[$i];
}
}
}

return $value;
}

function polialfabet_decode($string){
$value_temp = "";
$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"," ",",","?","!");
$key2 = array("i","n","d","o","e","s","a","b","c","f","g","h","j","k","l","m","p","q","r",
"t","u","v","w","x","y","z"," ",",","?","!");

//memecah string menjadi array sebelum di lakukan decode

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

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

$string = strtolower($value_temp);
$strCount = strlen($value_temp);
$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 = polialfabet_encode($kalimat);
echo "Kalimat setelah enkripsi : ".$kalimatbaru,"<br><br>";

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