LISTING PROGRAM
PROSES ENKRIPSI :
/*
* Created by SharpDevelop. * User: Rahmi Suliani Lubis * Date: 10/05/2017 * Time: 15.32 *
* To change this template use Tools | Options | Coding | Edit Standard Headers. */
using System;
using System.Collections;
using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.IO;
using System.Linq; using System.Text;
using System.Windows.Forms;
namespace skripsi_program {
///<summary>
/// Description of MainForm. ///</summary>
public partial class MainForm : Form {
int[] kuncizigzag = new int[8]; int[] cipherzigzag = new int[8]; string pesanawal,pesanenkrip; int p,q,n,t,ee,rn,d;
Random random = new Random();
public MainForm() {
//
// The InitializeComponent() call is required for Windows Forms designer support. //
InitializeComponent();
// TODO: Add constructor code after the InitializeComponent() call. //
}
public int modexp(int x, int y, int n) {
int z = 1;
for (int i = 0; i < y ; i++){ z = (x * z) % n; }
return z; }
public int modInverse(int a, int n) {
int i = n, v = 0, d = 1; while (a>0)
{
int t = i/a, x = a; a = i % x; i = x; x = d; d = v - t*x; v = x; } v %= n; if (v<0) v = (v+n)%n; return v; }
public int GCD(int a, int b) {
int Remainder; while( b != 0 ) {
Remainder = a % b; a = b;
b = Remainder; }
return a; }
string tipe;
OpenFileDialog dialog;
void Button_browseClick(object sender, EventArgs e) {
dialog = new OpenFileDialog();
dialog.Filter = "File Word|*.doc|File RTF |*.rtf|FILE TXT|*.txt"; dialog.Title = "Open File : ";
dialog.RestoreDirectory = true;
if (dialog.ShowDialog() == DialogResult.OK) {
tipe = dialog.FileName.Substring(dialog.FileName.Length - 3, 3); if (tipe == "doc")
{
label8.Text = dialog.FileName;
label12.Text = System.IO.Path.GetFileName(dialog.FileName); }
else if (tipe == "rtf") {
label8.Text = dialog.FileName;
label12.Text = System.IO.Path.GetFileName(dialog.FileName); }
else if (tipe == "txt") {
label8.Text = dialog.FileName;
label12.Text = System.IO.Path.GetFileName(dialog.FileName); }
loadText(); }
}
/// ///////////////////////////////////////////////////////////////////////////////////////// ///////////
private void loadText(){ if (tipe == "doc") {
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object objTrue = true;
object missing = System.Reflection.Missing.Value; object emptyData = string.Empty;
Microsoft.Office.Interop.Word.Document aDoc =
wordApp.Documents.Open(ref filenameO, ref objFalse, ref objTrue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref objTrue, ref missing, ref miss ing, ref missing, ref missing);
aDoc.ActiveWindow.Selection.WholeStory(); aDoc.ActiveWindow.Selection.Copy();
IDataObject data = System.Windows.Forms.Clipboard.GetDataObject(); String fileText =
data.GetData(System.Windows.Forms.DataFormats.Text).ToString(); System.Windows.Forms.Clipboard.SetDataObject(string.Empty);
preview.Text = fileText;
}
if (tipe == "txt") {
preview.Text = File.ReadAllText(dialog.FileName); }
if (tipe == "rtf") {
preview.LoadFile(dialog.FileName); }
}
/// ///////////////////////////////////////////////////////////////////////////////////////// ////////
/// /////////////////////////////////////////////////////////////////////////////////////
void PembangkitKunciClick(object sender, EventArgs e) {
int number;
for (int i = 0; i < 8; i++) {
number = random.Next(1,9);
if (!kuncizigzag.Contains(number)) //If it's not contains, add number to array; kuncizigzag[i] = number;
else //If it contains, restart random process i--;
}
for (int i = 0; i < kuncizigzag.Length; i++) { zigzagKey.Text += kuncizigzag[i] + " "; }
cari :
p = lehmann(random.Next(1,99)); q = lehmann(random.Next(1,99)); n = p*q;
if (p != q /*&& n <=255*/) {
} else {
goto cari; }
t = (p-1)*(q-1); cariee:
ee = lehmann(random.Next(1,99)); if (ee < t && GCD(ee,t) == 1) {
} else {
goto cariee; }
d = modInverse(ee,t); pRsa.Text = p.ToString(); qRsa.Text = q.ToString(); nRsa.Text = n.ToString(); eRsa.Text = ee.ToString(); dRsa.Text = d.ToString();
/// ///////////////////////////////////////////////////////////////////////////////////////// ///
void EnkripButtonClick(object sender, EventArgs e) {
Stopwatch q = new Stopwatch(); q.Start();
textBox1.Text = ""; hasil.Text = "";
pesanawal = preview.Text;
while (pesanawal.Length % 8 != 0) {
pesanawal += " "; }
//jadi array
char[,] pesanmatriks = new char[8,pesanawal.Length/8]; int k=0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < pesanawal.Length / 8; j++) {
pesanmatriks[i,j] = pesanawal[k]; k++;
} }
//jadi zigzag
string[] pesanhasil = new string[8]; for (int i = 0; i < 8; i++)
{
for (int j = 0; j < pesanawal.Length / 8; j++) {
int a = i; if (i == 7) {
if (j % 2 == 0) {
pesanhasil[i] += pesanmatriks[a,j]; }
else
pesanhasil[i] += pesanmatriks[0,j]; }
else {
{
pesanhasil[i] += pesanmatriks[a,j]; }
else
pesanhasil[i] += pesanmatriks[a+1,j]; }
} }
//enkripsi pesan for (int i = 0; i < 8; i++) {
pesanenkrip += pesanhasil[kuncizigzag[i]-1]; }
//enkripsi kunci
for (int i = 0; i < kuncizigzag.Length; i++) {
cipherzigzag[i] = modexp(kuncizigzag[i],ee,n); textBox1.Text += cipherzigzag[i].ToString() + " "; }
// for (int i = 0; i < 8; i++) // {
// for (int j = 0; j < pesanawal.Length / 8; j++) // {
// hasil.Text += pesanmatriks[i,j] +"\t"; // }
// hasil.Text += "\n"; // }
//
// for (int i = 0; i < 8; i++) // {
// hasil.Text += pesanhasil[i] + "\n"; // }
hasil.Text = pesanenkrip; pesanawal = "";
pesanenkrip = ""; q.Stop();
}
void ButtonSimpanClick(object sender, EventArgs e) {
SaveFileDialog simpan = new SaveFileDialog();
simpan.Filter = "File Word (MsWord)|*.doc|FILE TXT|*.txt"; simpan.FileName = "*.doc";
if (simpan.ShowDialog() == DialogResult.OK) {
File.WriteAllText(simpan.FileName, hasil.Text.ToString()); MessageBox.Show("Berhasil Disimpan");
}
SaveFileDialog simpancipherkey = new SaveFileDialog(); simpan.Filter = "File key (CipherKey)|*.key";
simpan.FileName = "*.key";
if (simpan.ShowDialog() == DialogResult.OK) {
File.WriteAllText(simpan.FileName, textBox1.Text.ToString()); MessageBox.Show("Berhasil Disimpan");
} }
void SimpanClick(object sender, EventArgs e) {
SaveFileDialog simpan = new SaveFileDialog(); simpan.Filter = "key Files( *.key)|*.key"; simpan.FileName = "*.key";
if (simpan.ShowDialog() == DialogResult.OK) {
string filename=simpan.FileName.Substring(0,simpan.FileName.Length- 3) + "key"; FileStream fstream=new FileStream(filename,FileMode.Create);
StreamWriter sw = new StreamWriter(fstream); SeekOrigin seekorigin = new SeekOrigin(); sw.BaseStream.Seek(0, seekorigin); //sw.WriteLine(zigzagKey.Text); sw.WriteLine(pRsa.Text); sw.WriteLine(qRsa.Text); sw.WriteLine(nRsa.Text); sw.WriteLine(dRsa.Text); sw.Write(eRsa.Text); sw.Flush();
MessageBox.Show("kunci berhasil disimpan");
} }
void Button1Click(object sender, EventArgs e) {
FormDekripsi f1 = new FormDekripsi(); f1.Show();
this.Hide(); }
public UInt64 sma(UInt64 x, UInt64 H, UInt64 n) {
UInt64 r;
int[] bin = new int[32]; int i;
r = x; i = 0;
/* ubah H ke Biner */ while( H > 0 ) {
if (H % 2 == 0) {
bin[i] = 0; }
else {
bin[i] = 1; }
H = H/2; i++; } i--; while(i>0) {
r = (r * r) % n; if( bin[--i] == 1 ) {
r = (r * x) % n; }
public int lehmann(int n) {
int a = 0,i = 0; UInt64 l; for(i=1;i<=2;i++) {
a = random.Next(2,n-2);
l = sma(Convert.ToUInt64(a),Convert.ToUInt64(((n-1)/2)),Convert.ToUInt64(n)); if (l != 1)
{
break; }
}
if(i == 2 && (n%2!=0 && n%5!=0 && n%3!=0 && n%7!=0)) return n;
else
return lehmann(random.Next(4,99)); }
void TextBox1TextChanged(object sender, EventArgs e) {
} } }
PROSES DEKRIPSI :
/*
* Created by SharpDevelop. * User: Rahmi Suliani Lubis * Date: 26/05/2017 * Time: 14.34 *
* To change this template use Tools | Options | Coding | Edit Standard Headers. */
using System;
using System.Windows.Forms;
namespace skripsi_program {
///<summary>
/// Description of FormDekripsi. ///</summary>
public partial class FormDekripsi : Form {
int[] kuncizigzag = new int[8]; string[] cipherzigzag = new string[8]; string pesanawal,pesanenkrip; int p,q,n,t,ee,rn,d;
Random random = new Random(); public FormDekripsi()
{ //
// The InitializeComponent() call is required for Windows Forms designer support. //
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call. //
}
void Button1Click(object sender, EventArgs e) {
MainForm f1 = new MainForm(); f1.Show();
this.Hide(); }
public int modexp(int x, int y, int n) {
int z = 1;
for (int i = 0; i < y ; i++){ z = (x * z) % n; }
return z; }
Stopwatch q = new Stopwatch(); q.Start();
d = Convert.ToInt32(dRsa.Text); n = Convert.ToInt32(nRsa.Text); cipherzigzag = cipherKey.Text.Split(' '); //dekripsi kunci
for (int i = 0; i < cipherzigzag.Length-1; i++) {
kuncizigzag[i] = modexp(Convert.ToInt32(cipherzigzag[i]),d,n); //textBox1.Text += kuncizigzag[i].ToString() + " ";
}
pesanawal = preview.Text; //jadi array
char[,] pesanmatriks = new char[8,pesanawal.Length/8]; int k=0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < pesanawal.Length / 8; j++) {
pesanmatriks[i,j] = pesanawal[k]; k++;
} }
//jadi zigzag
char[,] pesanhasil = new char[8,pesanawal.Length/8]; for (int i = 0; i < 8; i++)
{
for (int j = 0; j < pesanawal.Length / 8; j++) {
int a = i;
if (kuncizigzag[i]-1 == 7) {
if (j % 2 == 0) {
pesanhasil[kuncizigzag[i]-1,j] = pesanmatriks[a,j]; }
else
pesanhasil[0,j] = pesanmatriks[a,j]; }
else {
{
pesanhasil[kuncizigzag[i]-1,j] = pesanmatriks[a,j]; }
else
pesanhasil[kuncizigzag[i]-1+1,j] = pesanmatriks[a,j]; }
} }
//dekripsi
for (int i = 0; i < 8; i++) {
for (int j = 0; j < pesanawal.Length / 8; j++) {
pesanenkrip += pesanhasil[i,j]; }
}
while (pesanenkrip[pesanenkrip.Length-1] == ' ') {
pesanenkrip = pesanenkrip.Remove(pesanenkrip.Length-1); }
// //
//// for (int i = 0; i < 8; i++) //// {
//// for (int j = 0; j < pesanawal.Length / 8; j++) //// {
//// hasil.Text += pesanmatriks[i,j] +"\t"; //// }
//// hasil.Text += "\n"; //// }
////
//// for (int i = 0; i < 8; i++) //// {
//// hasil.Text += pesanhasil[i] + "\n"; //// }
//
hasil.Text = pesanenkrip; pesanawal = "";
pesanenkrip = ""; q.Stop();
}
string tipe;
OpenFileDialog dialog;
void Button_browseClick(object sender, EventArgs e) {
dialog = new OpenFileDialog();
dialog.Filter = "File Word|*.doc|FILE TXT|*.txt"; dialog.Title = "Open File : ";
dialog.RestoreDirectory = true;
if (dialog.ShowDialog() == DialogResult.OK) {
tipe = dialog.FileName.Substring(dialog.FileName.Length - 3, 3); if (tipe == "doc")
{
lokasi.Text = dialog.FileName;
namaFile.Text = System.IO.Path.GetFileName(dialog.FileName); }
else if (tipe == "rtf") {
lokasi.Text = dialog.FileName;
namaFile.Text = System.IO.Path.GetFileName(dialog.FileName); }
else if (tipe == "txt") {
lokasi.Text = dialog.FileName;
namaFile.Text = System.IO.Path.GetFileName(dialog.FileName); }
loadText(); } }
private void loadText(){ if (tipe == "doc") {
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object filenameO = lokasi.Text.ToString(); object objFalse = false;
object objTrue = true;
Microsoft.Office.Interop.Word.Document aDoc =
wordApp.Documents.Open(ref filenameO, ref objFalse, ref objTrue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref objTrue, ref missing, ref miss ing, ref missing, ref missing);
aDoc.ActiveWindow.Selection.WholeStory(); aDoc.ActiveWindow.Selection.Copy();
IDataObject data = System.Windows.Forms.Clipboard.GetDataObject(); String fileText =
data.GetData(System.Windows.Forms.DataFormats.Text).ToString(); System.Windows.Forms.Clipboard.SetDataObject(string.Empty);
preview.Text = fileText;
}
if (tipe == "txt") {
preview.Text = File.ReadAllText(dialog.FileName); }
if (tipe == "rtf") {
preview.LoadFile(dialog.FileName); }
}
void ButtonSimpanClick(object sender, EventArgs e) {
SaveFileDialog simpan = new SaveFileDialog(); simpan.Filter = "File Key|*.zigzagkey";
simpan.FileName = "*.zigzagkey";
if (simpan.ShowDialog() == DialogResult.OK) {
File.WriteAllText(simpan.FileName, hasil.Text.ToString()); MessageBox.Show("Berhasil Disimpan");
} }
{
dialog = new OpenFileDialog(); dialog.Filter = "File Kunci|*.key"; dialog.Title = "Open File : "; dialog.RestoreDirectory = true;
loadKey();
if (dialog.ShowDialog() == DialogResult.OK) {
tipe = dialog.FileName.Substring(dialog.FileName.Length - 3, 3); if (tipe == "key")
{
string [] lines = File.ReadAllLines(dialog.FileName); pRsa.Text = lines[0];
qRsa.Text = lines[1]; nRsa.Text = lines[2]; dRsa.Text = lines[3]; eRsa.Text = lines[4]; }
} }
private void loadKey(){
dialog = new OpenFileDialog(); dialog.Filter = "File Kunci|*.key"; dialog.Title = "Open File : "; dialog.RestoreDirectory = true;
if (dialog.ShowDialog() == DialogResult.OK) {
tipe = dialog.FileName.Substring(dialog.FileName.Length - 3, 3); if (tipe == "key")
{
string [] lines = File.ReadAllLines(dialog.FileName); cipherKey.Text = lines[0];
} } }
void Button2Click(object sender, EventArgs e) {
this.Hide(); }
} }
COVER
/*
* Created by SharpDevelop. * User: Rahmi Suliani Lubis * Date: 13/06/2017 * Time: 19.14 *
* To change this template use Tools | Options | Coding | Edit Standard Headers. */
using System;
using System.Drawing;
using System.Windows.Forms;
namespace skripsi_program {
///<summary>
/// Description of cover. ///</summary>
public partial class cover : Form {
public cover() {
//
// The InitializeComponent() call is required for Windows Forms designer support. //
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call. //
}
void Label1Click(object sender, EventArgs e) {
}
void PictureBox1Click(object sender, EventArgs e) {
}
void Button1Click(object sender, EventArgs e) {
MainForm f1 = new MainForm(); f1.Show();
this.Hide(); }