Listing Program 1. Form Menu Utama
Listing Program 1. Form Menu Utama
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using hybrid.Controller;
namespace KriptografiHybrid.Controller
{
public partial class mainForm : Form
{
2. Form Enkripsi
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using hybrid.Lib;
using System.Diagnostics;
namespace KriptografiHybrid.Controller
{
public partial class encryptForm : Form
{
long jum;
int nckey;
int n = 0;
long[] nil_w = new long[1000];
int[,] tempval = new int[4, 4];
int a;
int[,] kunci = new int[4, 4];
private String fileExt;
DoubleMatrix k = null;
/// <summary>
/// Set default
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void encryptForm_Load(object sender, EventArgs e)
{
cbMatrixKunci.SelectedIndex = 0;
}
/// <summary>
/// Cari file yang akan di enkripsi
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPilihSumberFile_Click(object sender, EventArgs e)
{
OpenFileDialog FileDialog = new OpenFileDialog();
if (FileDialog.ShowDialog() == DialogResult.OK)
{
txtSumberFile.Text = FileDialog.FileName.ToString();
fileExt =
Path.GetExtension(FileDialog.FileName).Substring(1);
txtExtensiSumberFile.Text = fileExt;
}
}
/// <summary>
/// Generate matrix kunci
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGenerateKunci_Click(object sender, EventArgs e)
{
cbJumlahW.Items.Clear();
if (cbMatrixKunci.Text == "2x2")
{
a = 2;
for (int i = 2; i < ((32 / 2) + 1); i++)
{
if (32 % i == 0)
{
cbJumlahW.Items.Add(i.ToString());
}
}
}
else if (cbMatrixKunci.Text == "3x3")
else if (a == 3)
{
}
else if (selectedIndexKeyMatrix == 3) // 4x4
{
k = new DoubleMatrix(4, 4);
// baris 0
k[0, 0] = kunci[0, 0];
k[1, 0] = kunci[0, 1];
k[2, 0] = kunci[0, 2];
k[3, 0] = kunci[0, 3];
// baris 1
k[0, 1] = kunci[1, 0];
k[1, 1] = kunci[1, 1];
k[2, 1] = kunci[1, 2];
k[3, 1] = kunci[1, 3];
// baris 2
k[0, 2] = kunci[2, 0];
k[1, 2] = kunci[2, 1];
k[2, 2] = kunci[2, 2];
k[3, 2] = kunci[2, 3];
// baris 3
k[0, 3] = kunci[3, 0];
k[1, 3] = kunci[3, 1];
k[2, 3] = kunci[3, 2];
k[3, 3] = kunci[3, 3];
}
Console.WriteLine(k);
}
/// <summary>
/// Ambil tujuan file enkripsi
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPilihTujuanFile_Click(object sender, EventArgs e)
{
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = fileExt;
saveDialog.AddExtension = true;
saveDialog.Filter = fileExt + " (*." + fileExt + ")|" +
fileExt;
if (saveDialog.ShowDialog() == DialogResult.OK)
{
/// <summary>
/// Lakukan enkripsi cipher hill
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEnkripsi_Click(object sender, EventArgs e)
{
//lama enkripsi
textBox4.Text = stopwatch.ElapsedMilliseconds + " ms
".ToString();
//ukuran file
FileBitReader fileAsli = new FileBitReader(txtSumberFile.Text);
textBox6.Text = fileAsli.getFileSize() + " byte ".ToString();
fileAsli.close();
//ukuran cipherfile
FileBitReader fileTerenkrip = new
FileBitReader(txtTujuanFile.Text);
textBox3.Text = fileTerenkrip.getFileSize() +" byte
".ToString();
/// <summary>
/// Pilih jumlah w
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cbJumlahW_SelectedIndexChanged(object sender,
EventArgs e)
{
//int bitnil = 0;
if (cbMatrixKunci.Text == "2x2")
{
// bitnil = 32;
}
else if (cbMatrixKunci.Text == "3x3")
{
// bitnil = 72;
}
else if (cbMatrixKunci.Text == "4x4")
{
// bitnil = 128;
}
//else
}
// bitnil = 32;
// }
/// <summary>
/// Bangkitkan w,q,r
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGenerate_Click(object sender, EventArgs e)
{
if (cbJumlahW.Text == "")
{
MessageBox.Show("piih terlebih dahulu banyak item w");
}
else
{
Random rand = new Random();
long temp = 0;
n = Convert.ToInt32(cbJumlahW.Text);
nil_w[0] = 1;
jum = nil_w[0];
txtW.Text = nil_w[0].ToString();
for (int i = 1; i < n; i++)
{
for (int j = 0; j < i; j++)
{
temp += nil_w[j];
}
nil_w[i] = temp + 1;
jum += nil_w[i];
txtW.Text += " " + nil_w[i].ToString();
temp = 0;
}
{
long r = Convert.ToInt32(txtR.Text), q =
Convert.ToInt64(txtQ.Text);
n = Convert.ToInt32(cbJumlahW.Text);
long[] nilpublic = new long[n];
txtKunciPublik.Text = "";
for (int i = 0; i < n; i++)
{
nilpublic[i] = ((nil_w[i] * r) % q);
txtKunciPublik.Text += nilpublic[i].ToString() + "
";
}
}
{
txtKunciPrivate.Text = txtW.Text;
}
}
// dr sini
}
/// <summary>
/// Pilih tujuan key disimpan
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPilihTujuanKey_Click(object sender, EventArgs e)
{
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "cipherkey";
saveDialog.AddExtension = true;
saveDialog.Filter = "cipherkey (*.cipherkey)|cipherkey";
if (saveDialog.ShowDialog() == DialogResult.OK)
{
txtTujuanKey.Text = saveDialog.FileName.ToString();
}
}
/// <summary>
/// Lakukan enkripsi pada key (Knapsack)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_EnkripsiKunci_Click(object sender, EventArgs e)
/// <summary>
/// Keluar dari form enkripsi
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBatal_Click(object sender, EventArgs e)
{
Close();
}
3. Form Dekripsi
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using hybrid.Lib;
using System.Diagnostics;
namespace KriptografiHybrid.Controller
{
public partial class decryptForm : Form
{
// long jum;
//int n = 0;
long[] nil_w = new long[1000];
int[,] tempval = new int[4, 4];
//int a;
int[,] kunci = new int[4, 4];
private String fileExt;
// DoubleMatrix k = null;
#region VAR ############
// private Boolean bIsExit = false;
#endregion
#region KONSTRUKTOR ###################
public decryptForm()
{
InitializeComponent();
}
#endregion
/// <summary>
/// Pilih file cipher key dan baca w,q,r,cipherkey ke textbox
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPilihFileCipherKey_Click(object sender, EventArgs
e)
{
OpenFileDialog openKey = new OpenFileDialog();
openKey.Filter = "cipherkey(*.cipherkey)|*.cipherkey";
if (openKey.ShowDialog() == DialogResult.OK)
{
string read = "";
StreamReader objReader = new
StreamReader(openKey.FileName);
txtFileCipherKey.Text = openKey.FileName;
int count = 0;
while (read != null)
{
if (count == 0)
{
txt_Q.Text = read;
}
else if (count == 1)
{
txt_R.Text = read;
}
else if (count == 3)
{
txt_KunciPrivat.Text = read;
}
else if (count == 4)
{
txtCipherKey.Text = read;
}
count++;
read = objReader.ReadLine();
}
objReader.Close();
}
}
/// <summary>
/// Lakukan dekripsi knapsack terhadap cipherkey, q, w, r
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_DekripsiCipherkey_Click(object sender, EventArgs
e)
{
try
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
String result = Knapsack.decrypt(txt_KunciPrivat.Text,
txtCipherKey.Text, txt_Q.Text, txt_R.Text);
txt_CipherMatrix.Text = result;
stopwatch.Stop();
MessageBox.Show("Dekripsi Key Berhasil! ");
textBox2.Text = stopwatch.ElapsedMilliseconds + " ms
".ToString();
}
catch (Exception TerjadiKesalahan)
{
MessageBox.Show("Error\n" + TerjadiKesalahan.ToString());
}
/// <summary>
/// Pilih sumber file yang sudah terenkripsi
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <summary>
/// Pilih dimana file hasil dekripsi disimpan (Cipherhill)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPilihTujuanFile_Click(object sender, EventArgs e)
{
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = fileExt;
saveDialog.AddExtension = true;
saveDialog.Filter = fileExt + " (*." + fileExt + ")|"+fileExt;
if (saveDialog.ShowDialog() == DialogResult.OK)
{
txtTujuanFileDekripsi.Text =
saveDialog.FileName.ToString();
}
}
/// <summary>
/// Lakukan proses dekripsi dan simpan file hasil dekripsi
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Dekripsi_Click(object sender, EventArgs e)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
String key = txt_CipherMatrix.Text; // 45 34 54 23 45 23 45 23
//buang spasi terakhir
key = key.Substring(0, key.Length - 1); // posisi terakhir
adalah spasi
//lama dekripsi
textBox1.Text = stopwatch.ElapsedMilliseconds + " ms
".ToString();
}
//MessageBox.Show(identifier);
writer.close();
reader.close();
}
else
{
MessageBox.Show("Key yang anda masukkan salah! Key harus
berupa 2x2; 3x3; 4x4");
}
}
#endregion
}
}
}
namespace hybrid.Controller
{
public partial class HelpForm : Form
{
public HelpForm()
{
InitializeComponent();
}
}
}
5. Form Tentang
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
namespace hybrid.Controller
{
public partial class programmerForm : Form
{
public programmerForm()
{
InitializeComponent();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace hybrid.Lib
{
class HillCipher
{
public static void encrypt(FileBitReader reader, FileBitWriter
writer, DoubleMatrix k)
{
// ambil jenis matrix
int jenisMatrix; // total baris = total kolom
jenisMatrix = k.RowCount;
// ambil ukuran file
long fileSize = reader.getFileSize();
// variabel count jumlah byte yang sudah dibaca
long readedByte = 0;
// lakukan pembacaan sampai seluruh file terbaca
while (readedByte < fileSize) {
// ambil sebanyak jumlah matrix dan ciptakan matrix
DoubleMatrix matrixP = new DoubleMatrix(1, jenisMatrix);
int readedP = 0;
while (readedP < jenisMatrix)
{
if (readedByte < fileSize)
{
matrixP[0, readedP] = reader.readByte();
readedByte++;
}
else
{
matrixP[0, readedP] = 0;
}
readedP++;
if (x == 0) {
throw new Exception("Tidak bisa menemukan nilai invers Det
K");
}
// pembuktian
DoubleMatrix bukti = k * invK;
bukti.mod(256);
for (int baris = 0; baris < invK.RowCount; baris++)
{
for (int kol = 0; kol < invK.ColumnCount; kol++)
{
if (baris == kol)
{
if (bukti[kol, baris] != 1) {
7. Class Knapsack
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
//int to Biner
temp = "";
for (int i = 0; i < jnsMatrix; i++)
{
for (int j = 0; j < jnsMatrix; j++)
{
temp += Knapsack.int2bin(kunci[i, j], 8);
}
}
//MessageBox.Show(temp.Text);
//Pisah Menjadi Blok Biner
indeks = 0;
kTempPub = "";
nckey = (temp.Length / jlhW);
int counter = 0;
string[] sesiKeyCipher = new string[nckey];
//MessageBox.Show(temp.Text+" "+temp.TextLength);
indeks = 0;
kTempPub = "";
int counter = 0;
string[] sesiKeyCipher = new string[100];
int[] KeyCipher = new int[100];
temp = "";
nInv = (1 + (q * k)) / r;
dataDec[i] = 0;
for (int j = 0; j < n_bit; j++)
{
dataDec[i] +=
(Convert.ToInt32(dataBit[i][j].ToString()) *
kPrivate[j]);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace hybrid.Lib
{
class FileBitReader
{
private String fileName;
private FileStream fileIn;
private BufferedStream buffFileIn;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace hybrid.Lib
{
class FileBitWriter
{
private String fileName;
private FileStream fileOut;
private BufferedStream buffFileOut;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace hybrid.Lib
{
public class DoubleMatrix
{
public double determinant() {
if (this.ColumnCount != this.RowCount)
{
throw new Exception("Determinan berlaku pada matrix NxN");
}
if (this.ColumnCount == 1)
{
return this[0, 0];
}
if (this.ColumnCount != 2)
{
throw new NotImplementedException("Fungsi hanya bisa
melakukan determinant n=2");
}
int n = this.RowCount;
if (n == 2) {
//(kunci[0, 0] * kunci[1, 1]) - (kunci[1, 0] * kunci[0,
1]);
return (this[0, 0] * this[1, 1]) - (this[0, 1] * this[1,
0]);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace hybrid.Lib
{
class GCD
{
public static long GCD1(long a, long b)
{
while (b != 0)
{
long tmp = b;
b = a % b;
a = tmp;
}
return a;
}
}
}
Keterangan :
No : Nilai byte
DETAIL PROFIL
Telp/Handphone : - / 082304593490
e-mail :[email protected]/[email protected]
SEBAGAI SEMINAR