kripto: Added tugas 1.

Signed-off-by: Willy Sudiarto Raharjo <willysr@gmail.com>
This commit is contained in:
Willy Sudiarto Raharjo
2023-08-27 10:41:06 +07:00
parent a4c33aa29b
commit 7b616f52c2
4 changed files with 339 additions and 0 deletions
+109
View File
@@ -0,0 +1,109 @@
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") // Cek apakah halaman diload melalui metode POST?
{
include "functions.php"; // Untuk fungsi produce_error
$inputPlainText = strip_tags($_POST['plaintext']);
if (empty($inputPlainText)) // Cek apakah ada plaintext?
{
produce_error("Input plaintext masih kosong");
return;
}
$inputKey = strip_tags($_POST['key']);
if (empty($inputKey)) // Cek apakah ada kunci?
{
produce_error("Input kunci masih kosong");
return;
}
$plainTextLength = strlen($inputPlainText); // ambil panjang karakter plaintext
$keyLength = strlen($inputKey); // ambil panjang kunci
if (preg_match("/[^a-zA-Z]/",$inputPlainText) || preg_match("/[^a-zA-Z]/",$inputKey)) // Jika ada karakter selain alphabet
{
produce_error("Terdapat karakter selain alphabet");
return;
}
// Tampilkan struktur dasar HTML
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3c.org/TR/html4/loose.dtd\">\n";
echo "<html>\n";
echo "<head>\n";
echo "<title>Willy Sudiarto Raharjo - Tugas 2 Keamanan Komputer - Semester Gasal 2016/2017</title>\n";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
echo "<meta name=\"author\" content=\"Willy Sudiarto Raharjo\">\n";
echo "<style type=\"text/css\">\n";
echo "@import url(index.css);\n";
echo "</style>\n";
echo "</head>\n";
echo "<body>\n";
echo "<div class=\"Result-Title\">\n";
echo "Hasil Enkripsi: \n";
echo "</div>\n";
$resultingCipherText = "";
echo "plaintext: $inputPlainText<br/>";
echo "kunci: $inputKey<br/>";
/*
/* Pergeseran sebanyak panjang kunci
*/
for ($i = 0; $i < $keyLength; $i++)
{
/*
/* Jika loop pertama, gunakan input awal
/* Jika bukan loop pertama, gunakan hasil perhitungan sebelumnya
*/
if ($i == 0)
{
$originalPlainText = $inputPlainText;
}
else
{
$originalPlainText = $resultingCipherText;
}
/*
/* Jumlah pergeseran dari karakter ke-i dari kunci
/* Hasilnya dikurangi 97 karena karakter a kode ASCInya 97
/* Kita ingin a = 0, b = 1, c = 2, dst
*/
$numberOfMovement = ord($inputKey[$i]) - 97;
echo "Karakter ke-" . ($i+1) . " kunci - ";
/*
/* Pergeseran sebanyak panjang plaintext
*/
for ($j = 0; $j < $plainTextLength; $j++)
{
/*
/* Algoritma:
/* 1. Ambil kode ASCII dari teks menggunakan fungsi ord
/* 2. Konversi ke bilangan 0 - 25 dengan mengurangi 97
/* 3. Tambahkan jumlah pergeseran dari karakter ke-i dari kunci
/* 4. Pastikan hasilnya diantara 0-25 dengan cara modulus 26
/* 5. Kembalikan hasilnya ke karakter ASCII dengan fungsi chr
*/
$originalPlainText[$j] = chr((((ord($originalPlainText[$j]) - 97) + $numberOfMovement) % 26) + 97);
echo $originalPlainText[$j];
}
echo "<br/>\n";
/*
/* Simpan hasilnya ke variabel resultingCipherText untuk nilai awal loop berikutnya
*/
$resultingCipherText = $originalPlainText;
}
echo "ciphertext: <strong>$resultingCipherText</strong>";
echo "<div class=\"Link\">\n";
echo ":: <a href=\"index.php\" class=\"url\">Kembali ke halaman input</a> ::\n";
echo "</div>\n";
echo "<div class=\"Info\">\n";
echo "Willy Sudiarto Raharjo\n";
echo "</div>\n";
echo "</body>\n";
echo "</html>\n";
}
else // Jika ternyata diakses melalui metode GET (kemungkinan besar pemanggilan langsung)
{
header("location:index.php"); // Redirect ke halaman input
exit; // hanya memastikan bahwa tidak menjalankan perintah lain
}
?>
+29
View File
@@ -0,0 +1,29 @@
<?php
function produce_error($string)
{
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3c.org/TR/html4/loose.dtd\">\n";
echo "<html>\n";
echo "<head>\n";
echo "<title>Willy Sudiarto Raharjo - Tugas 2 Keamanan Komputer - Semester Gasal 2016/2017</title>\n";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
echo "<meta name=\"author\" content=\"Willy Sudiarto Raharjo\">\n";
echo "<style type=\"text/css\">\n";
echo "@import url(index.css);\n";
echo "</style>\n";
echo "</head>\n";
echo "<body>\n";
echo "<div class=\"Error-Title\">\n";
echo "Kesalahan\n";
echo "</div>\n";
echo "<div class=\"Error-Notes\">\n";
echo $string . "\n";
echo "</div>\n";
echo "<div class=\"Link\">\n";
echo ":: <a href=\"index.php\" class=\"url\">Kembali ke halaman input</a> ::\n";
echo "</div>\n";
echo "<div class=\"Info\">\n";
echo "Willy Sudiarto Raharjo\n";
echo "</div>\n";
echo "</body>\n";
echo "</html>\n";
}
+112
View File
@@ -0,0 +1,112 @@
body {
margin: 0.5em 0.5em;
font-family: Serif;
font-size: 12pt;
text-align: justify;
background-color: #FFF;
color: #000;
}
.Title, .Info, .Result-Title {
border-top: 2px solid #000;
border-bottom: 2px solid #000;
text-align: center;
font-weight: bold;
font-size: 14pt;
margin-bottom: 1em;
background: #d8e4f1;
}
form {
font-weight: bold;
font-size: 2em;
}
textarea:focus {
background: #C4E7F4;
}
input {
margin-top: 1em;
}
.Notes-Title {
color: #F00;
font-size: 1.33em;
}
.Notes {
line-height: 1.33em;
word-spacing: 1px;
}
li {
font-weight: bold;
line-height: 1.5em;
}
.Link {
margin-top: 1em;
margin-bottom: 2em;
text-align: center;
font-family: Sans-Serif;
font-weight: bold;
font-size : 14px;
}
p {
margin: 0;
padding-top: 0.5em;
font-weight: bold;
}
div.result {
padding-bottom: 0.5em;
border-bottom: 2px solid #558864;
}
a {
text-decoration:none;
color: #4064FE;
}
a.url:link {
color: #4064FE;
}
a.url:visited {
color: #4064FE;
}
a.url:hover {
text-decoration: underline;
color: #4064FE;
}
a.url:active {
text-decoration: underline;
color: #4064FE;
}
.Error-Title {
border-top: 2px solid #F00;
border-bottom: 2px solid #F00;
text-align: center;
font-weight: bold;
font-size: 14pt;
color: #FFF;
margin-bottom: 1em;
background: #F00;
}
.Error-Notes {
text-align: center;
color: #F00;
font-size: 16pt;
}
.LastUpdate {
font-size: 80%;
margin-top: 1em;
text-align: right;
}
+89
View File
@@ -0,0 +1,89 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/html4/loose.dtd">
<html>
<head>
<title>Willy Sudiarto Raharjo - Tugas 1 Kriptografi - Semester Gasal 2023/2024</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="author" content="Willy Sudiarto Raharjo">
<style type="text/css">
@import url(index.css);
</style>
</head>
<body>
<div class="Title">
TUGAS 1 KRIPTOGRAFI SEMESTER GASAL 2023/2024
</div>
<p>Anda diminta untuk membuat sebuah program yang bisa menerima input berupa plaintext dan mengubahnya menjadi ciphertext dengan ketentuan sebagai berikut:</p>
<ol>
<li>Input berupa plain ASCII huruf kecil (a-z) tanpa ada angka dan spasi</li>
<li>Seluruh isi teks akan diproses per karakter kunci
<ul>
<li>Kunci A berarti tidak ada perubahan</li>
<li>Kunci B berarti geser 1 posisi (a->b, b->c, z->a, dst)</li>
<li>Kunci C berarti geser 2 posisi (a->c, b->d, z->b, dst)</li>
<li>dan seterusnya</li>
</ul>
</li>
</ol>
Contoh:
<pre>
Plaintext: aku
Kunci: diam
Output:
Karakter ke-1 kunci: dnx (d)
Karakter ke-2 kunci: lvf (i)
Karakter ke-3 kunci: lvf (a)
Karakter ke-4 kunci: xhr (m)
ciphertext: <strong>xhr</strong></pre>
<form action="check.php" method="POST">
Input Plaintext: <br />
<textarea cols="60" rows="10" name="plaintext"></textarea><br />
Input Kunci: <br/>
<input type="text" name="key"></input><br/>
<input type="submit" name="submit" value="Encrypt">
</form>
<div class="Notes-Title">
Catatan :
</div>
<div class="Notes">
<ol>
<li>Input boleh huruf besar/kecil</li>
<li>Input hanya berupa alphabet</li>
<li>Input tidak boleh mengandung spasi</li>
</ol>
</div>
<p class="LastUpdate">
<?php
$filename = 'index.php';
if (file_exists($filename))
{
date_default_timezone_set("Asia/Jakarta");
$tanggal = date("d", filemtime($filename));
if (substr($tanggal,0,1)==0)
$tanggal=substr($tanggal,-1);
$bulan = date("m", filemtime($filename));
switch ($bulan)
{
case 1 : $bulan_id="Januari"; break;
case 2 : $bulan_id="Februari"; break;
case 3 : $bulan_id="Maret"; break;
case 4 : $bulan_id="April"; break;
case 5 : $bulan_id="Mei"; break;
case 6 : $bulan_id="Juni"; break;
case 7 : $bulan_id="Juli"; break;
case 8 : $bulan_id="Agustus"; break;
case 9 : $bulan_id="September"; break;
case 10 : $bulan_id="Oktober"; break;
case 11 : $bulan_id="November"; break;
case 12 : $bulan_id="Desember"; break;
}
$tahun = date("Y", filemtime($filename));
$jam = date("H:i:s", filemtime($filename));
echo "Update Terakhir : " . $tanggal . " " . $bulan_id . " " . $tahun . " :: " . $jam . "\n";
}
?>
</p>
<div class="Info">
Willy Sudiarto Raharjo
</div>
</body>
</html>