Implementasi Algoritma Rijndael Dan Rsa Pada Pengamanan Data Teks

Lampiran A-1

Listing Tampilan 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.Numerics;
namespace WindowsFormsApplication1
{
public partial class FormUtama : Form
{
public FormUtama()
{
InitializeComponent();
}
private void enkripsiToolStripMenuItem_Click(object sender, EventArgs e)

{
Enkripsi frm = new Enkripsi();
//frm.MdiParent = this;
frm.Show();
}
private void dekripsiToolStripMenuItem_Click(object sender, EventArgs e)
{
Dekripsi frm = new Dekripsi();
//frm.MdiParent = this;
frm.Show();
}
private void FormUtama_Load(object sender, EventArgs e)
{
//byte[] asli = new byte[16] { 0x32, 0x88, 0x31, 0xe0, 0x43, 0x5a,
0x31, 0x37, 0xf6, 0x30, 0x98, 0x07, 0xa8, 0x8d, 0xa2, 0x34 };
//byte[] key = new byte[16] { 0x2b, 0x28, 0xab, 0x09, 0x7e, 0xae,
0xf7, 0xcf, 0x15, 0xd2, 0x15, 0x4f, 0x16, 0xa6, 0x88, 0x3c };
//string ss = "";
//for (int i = 0; i < 16; i++)
//{

//
ss += asli[i].ToString("x2") + " ";
//}
//Console.WriteLine(ss);
//Rijndael rij = new Rijndael(key);
//byte[] cipher = rij.enkripsi(asli);
//byte[] decipher = rij.dekripsi(cipher);
//string s = "";
//s = "";
//for (int i = 0; i < asli.Length; i++)
//{
//
s += asli[i].ToString("x2") + " ";
//}
//Console.WriteLine("plain teks = " + s);
//s = "";
//for (int i = 0; i < cipher.Length; i++)
//{
//
s += cipher[i].ToString("x2") + " ";


Universitas Sumatera Utara

Lampiran A-2

//}
//Console.WriteLine("cipher teks = " + s);
//s = "";
//for (int i = 0; i < 16; i++)
//{
//
s += decipher[i].ToString("x2") + " ";
//}
//Console.WriteLine("decipher teks = " + s);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void label1_Click(object sender, EventArgs e)

{
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
About frm = new About();
//frm.MdiParent = this;
frm.Show();
}
}
}

Universitas Sumatera Utara

Lampiran A-3

ListingTampilan Proses 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.Numerics;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Enkripsi : Form
{
private BigInteger E, n, d;
private Rijndael rij;
private RSA rsa;
private String lokasiFileBuka = "";
private String statusSimpan = "";
public Enkripsi()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (txtTeks.Text != "" || txtKey.Text != "")
{
int panjangEnkripsi = 0;
int penanda = 0;
panjangEnkripsi = (int)Math.Ceiling(((double)txtTeks.Text.Length / 16));
byte[] asli = new byte[16];
byte[] key = Encoding.ASCII.GetBytes(txtKey.Text.PadRight(16, '
'));
rij = new Rijndael(key);
for (int i = 0; i < panjangEnkripsi; i++)
{
string teksTemp = "";
if (i == panjangEnkripsi - 1)
{
int berapaBykPotong = txtTeks.Text.Length - penanda;

teksTemp = txtTeks.Text.Substring(penanda,
berapaBykPotong);
teksTemp = teksTemp.PadRight(16, ' ');
}
else
{
teksTemp = txtTeks.Text.Substring(penanda, 16);
}
asli = Encoding.ASCII.GetBytes(teksTemp);
byte[] cipher = rij.enkripsi(asli);

Universitas Sumatera Utara

Lampiran A-4

for (int j = 0; j < cipher.Length; j++)
{
txtTeksEnk.Text += (char)cipher[j];
}
penanda += 16;

}
}
}
private void btnEnkripKey_Click(object sender, EventArgs e)
{
if (txtKey.Text != "")
{
rsa = new RSA();
rsa.enkripsi(txtKey.Text);
E = rsa.e;
n = rsa.n;
d = rsa.d;
txtD.Text = d.ToString();
txtE.Text = E.ToString();
txtN1.Text = n.ToString();
txtN2.Text = n.ToString();
txtKeyEnk.Text = rsa.cipher;
}
}
private void button1_Click_1(object sender, EventArgs e)

{
openFileDialog1.Title = "Buka File Text.";
openFileDialog1.Filter = "Txt File (*.txt)|*.txt";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.FileName = "";
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
System.IO.Stream strm;
strm = openFileDialog1.OpenFile();
lokasiFileBuka = openFileDialog1.FileName.ToString();
System.IO.FileStream inp = System.IO.File.OpenRead(lokasiFileBuka);
System.IO.StreamReader input = new System.IO.StreamReader(inp);
try
{
while (input.Peek() > -1)
{
string teks = input.ReadToEnd();
txtTeks.Text = teks;

}
}
finally
{
input.Close();
}
if (strm != null)
{
strm.Close();
}

Universitas Sumatera Utara

Lampiran A-5

}
private void btnSimpan_Click(object sender, EventArgs e)
{
saveFileDialog1.Title = "Simpan File Teks TerEnkripsi.";
saveFileDialog1.Filter = "Txt File (*.txt)|*.txt";

saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.FileName = "";
statusSimpan = "rijndael";
saveFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
if (statusSimpan == "rijndael")
{
StringBuilder sb = new StringBuilder();
sb.Append(txtTeksEnk.Text);
using (StreamWriter outfile = new StreamWriter(saveFileDialog1.FileName,
false))
{
outfile.Write(sb.ToString());
}
MessageBox.Show("Berhasil!");
}
else if (statusSimpan == "rsa")
{
String teksYangInginDisimpan = "";
teksYangInginDisimpan += txtKeyEnk.Text + Environment.NewLine;
teksYangInginDisimpan += d.ToString() + Environment.NewLine;
teksYangInginDisimpan += n.ToString();
StringBuilder sb = new StringBuilder();
sb.Append(teksYangInginDisimpan);
using (StreamWriter outfile = new StreamWriter(saveFileDialog1.FileName,
false))
{
outfile.Write(sb.ToString());
}
MessageBox.Show("Berhasil!");
}
}
private void button2_Click(object sender, EventArgs e)
{
saveFileDialog1.Title = "Simpan File Key TerEnkripsi, dan kunci publik RSA.";
saveFileDialog1.Filter = "Txt File (*.txt)|*.txt";
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.FileName = "";
statusSimpan = "rsa";
saveFileDialog1.ShowDialog();
}

Universitas Sumatera Utara

Lampiran A-6

private void button3_Click(object sender, EventArgs e)
{
txtD.Clear();
txtE.Clear();
txtKey.Clear();
txtKeyEnk.Clear();
txtTeks.Clear();
txtTeksEnk.Clear();
txtN1.Clear();
txtN2.Clear();
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

Listing Tampilan 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.Numerics;
namespace WindowsFormsApplication1
{
public partial class Dekripsi : Form
{
private RSA rsa;
private Rijndael rij;
private String statusBukaFile = "";
public Dekripsi()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
}
private void Dekripsi_Load(object sender, EventArgs e)
{
}

Universitas Sumatera Utara

Lampiran A-7

private void btnEnkripKey_Click(object sender, EventArgs e)
{
if (txtKeyEnk.Text != "" || txtD.Text != "" || txtN2.Text != "")
{
rsa = new RSA();
rsa.d = (BigInteger)Convert.ToInt64(txtD.Text);
rsa.n = (BigInteger)Convert.ToInt64(txtN2.Text);
rsa.dekripsi(txtKeyEnk.Text);
txtKey.Text = rsa.decipher;
}
}
private void btnEnkripTeks_Click(object sender, EventArgs e)
{
if (txtKey.Text != "" || txtTeksEnk.Text != "")
{
int panjangEnkripsi = 0;
int penanda = 0;
panjangEnkripsi = (int)Math.Ceiling(((double)txtTeksEnk.Text.Length / 16));
byte[] cipher = new byte[16];
byte[] key = Encoding.ASCII.GetBytes(txtKey.Text.PadRight(16, '
'));
rij = new Rijndael(key);
for (int i = 0; i < panjangEnkripsi; i++)
{
int len = txtTeksEnk.Text.Length;
string teksTemp = txtTeksEnk.Text.Substring(penanda, 16);
int z = 0;
foreach (char c in teksTemp)
{
cipher[z] = (byte)System.Convert.ToInt32(c);
z++;
}
byte[] decipher = rij.dekripsi(cipher);
for (int j = 0; j < decipher.Length; j++)
{
txtTeks.Text += (char)decipher[j];
}
penanda += 16;
}
//byte[] cipher = new byte[txtTeksEnk.Text.Length];
//byte[] key = Encoding.ASCII.GetBytes(txtKey.Text.PadRight(16, '
'));
//rij = new Rijndael(key);
//int z = 0;
//foreach (char c in txtTeksEnk.Text)
//{
// cipher[z] = (byte)System.Convert.ToInt32(c);
// z++;
//}
//byte[] decipher = rij.dekripsi(cipher);

//for (int i = 0; i < decipher.Length; i++)
//{
//
txtTeks.Text += (char)decipher[i];
//}
}
}
private void button1_Click(object sender, EventArgs e)

Universitas Sumatera Utara

Lampiran A-8

{
openFileDialog1.Title = "Buka File Text TerEnkripsi.";
openFileDialog1.Filter = "Txt File (*.txt)|*.txt";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.FileName = "";
statusBukaFile = "rijndael";
openFileDialog1.ShowDialog();
}
private void button2_Click_1(object sender, EventArgs e)
{
openFileDialog1.Title = "Buka File Text TerEnkripsi.";
openFileDialog1.Filter = "Txt File (*.txt)|*.txt";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.FileName = "";
statusBukaFile = "rsa";
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
if (statusBukaFile == "rijndael")
{
System.IO.Stream strm;
strm = openFileDialog1.OpenFile();
System.IO.FileStream inp =
System.IO.File.OpenRead(openFileDialog1.FileName.ToString());
System.IO.StreamReader input = new System.IO.StreamReader(inp);
try
{
while (input.Peek() > -1)
{
string teks = input.ReadToEnd();
txtTeksEnk.Text = teks;
}
}
finally
{
input.Close();
}
if (strm != null)
{
strm.Close();
}
}
else if (statusBukaFile == "rsa")
{
System.IO.Stream strm;
strm = openFileDialog1.OpenFile();
System.IO.FileStream inp =
System.IO.File.OpenRead(openFileDialog1.FileName.ToString());
System.IO.StreamReader input = new System.IO.StreamReader(inp);
int indeks = 0;
try
{
while (input.Peek() > -1)
{
string teks = input.ReadLine();
if (indeks == 0)
{

Universitas Sumatera Utara

Lampiran A-9

txtKeyEnk.Text = teks;
}
else if (indeks == 1)
{
txtD.Text = teks;
}
else if (indeks == 2)
{
txtN2.Text = teks;
}
indeks++;
}
}
finally
{
input.Close();
}
if (strm != null)
{
strm.Close();
}
}
}
private void groupBox2_Enter(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
txtD.Clear();
txtKey.Clear();
txtKeyEnk.Clear();
txtTeks.Clear();
txtTeksEnk.Clear();
txtN2.Clear();
}
}
}

Universitas Sumatera Utara

Lampiran A-10

ListingAlgoritma Rijndael
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace WindowsFormsApplication1
{
class Rijndael
{
private byte[] key;
private byte[,] Sbox;
private byte[,] iSbox;
private byte[,,] w;
private byte[,] Rcon;
private byte[,] State;
public Rijndael(byte[] key)
{
this.key = new byte[16];
key.CopyTo(this.key, 0);
buatSbox();
buatInvSbox();
buatRcon();
keyExpansion();
}
public byte[] enkripsi(byte[] teks)
{
byte[] hasil = new byte[16];
this.State = new byte[4, 4];
for (int i = 0; i < 16; i++)
{
this.State[i / 4, i % 4] = teks[i];
}
addRoundKey(0);
for (int putaran = 1; putaran < 10; putaran++)
{
subBytes();
shiftRows();
mixCollumns();
addRoundKey(putaran);
}
subBytes();
shiftRows();
addRoundKey(10);
for (int i = 0; i < 16; i++)
{
hasil[i] = this.State[i / 4, i % 4];
}
return hasil;
}

public byte[] dekripsi(byte[] cipher)

Universitas Sumatera Utara

Lampiran A-11

{
byte[] hasil = new byte[16];
this.State = new byte[4, 4];
for (int i = 0; i < 16; i++)
{
this.State[i / 4, i % 4] = cipher[i];
}
addRoundKey(10);
for (int putaran = 9; putaran >= 1; putaran--)
{
invShiftRows();
invSubBytes();
addRoundKey(putaran);
invMixCollumns();
}

invShiftRows();
invSubBytes();
addRoundKey(0);
for (int i = 0; i < 16; i++)
{
hasil[i] = this.State[i / 4, i % 4];
}
return hasil;
}
public void addRoundKey(int putaran)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
this.State[i, j] = (byte)((int)this.State[i, j] ^
(int)this.w[putaran, i, j]);
}
}
}
private void subBytes()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
this.State[i, j] = this.Sbox[(this.State[i, j] >> 4),
(this.State[i, j] & 0x0f)];
}
}
}
private void invSubBytes()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
this.State[i, j] = this.iSbox[(this.State[i, j] >> 4),
(this.State[i, j] & 0x0f)];
}

Universitas Sumatera Utara

Lampiran A-12

}
}
private void shiftRows()
{
byte[,] temp = new byte[4, 4];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
temp[i, j] = this.State[i, j];
}
}
for (int i = 1; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
this.State[i, j] = temp[i, ((i + j) % 4)];
}
}
}
private void invShiftRows()
{
byte[,] temp = new byte[4, 4];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
temp[i, j] = this.State[i, j];
}
}
for (int i = 1; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
this.State[i, ((i + j) % 4)] = temp[i, j];
}
}
}
private void mixCollumns()
{
byte[,] temp = new byte[4, 4];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
temp[i, j] = this.State[i, j];
}
}
for (int i = 0; i < 4; i++)
{
this.State[0, i] = (byte)
((int)gfmultby02(temp[0, i]) ^ (int)gfmultby03(temp[1, i]) ^
(int)gfmultby01(temp[2, i]) ^ (int)gfmultby01(temp[3, i]));
this.State[1, i] = (byte)
((int)gfmultby01(temp[0, i]) ^ (int)gfmultby02(temp[1, i]) ^
(int)gfmultby03(temp[2, i]) ^ (int)gfmultby01(temp[3, i]));
this.State[2, i] = (byte)

Universitas Sumatera Utara

Lampiran A-13

((int)gfmultby01(temp[0, i]) ^ (int)gfmultby01(temp[1, i]) ^
(int)gfmultby02(temp[2, i]) ^ (int)gfmultby03(temp[3, i]));
this.State[3, i] = (byte)
((int)gfmultby03(temp[0, i]) ^ (int)gfmultby01(temp[1, i]) ^
(int)gfmultby01(temp[2, i]) ^ (int)gfmultby02(temp[3, i]));
}
}

private void invMixCollumns()
{
byte[,] temp = new byte[4, 4];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
temp[i, j] = this.State[i, j];
}
}
for (int i = 0; i < 4; i++)
{
this.State[0, i] = (byte)
((int)gfmultby0e(temp[0, i]) ^ (int)gfmultby0b(temp[1,
(int)gfmultby0d(temp[2, i]) ^ (int)gfmultby09(temp[3, i]));
this.State[1, i] = (byte)
((int)gfmultby09(temp[0, i]) ^ (int)gfmultby0e(temp[1,
(int)gfmultby0b(temp[2, i]) ^ (int)gfmultby0d(temp[3, i]));
this.State[2, i] = (byte)
((int)gfmultby0d(temp[0, i]) ^ (int)gfmultby09(temp[1,
(int)gfmultby0e(temp[2, i]) ^ (int)gfmultby0b(temp[3, i]));
this.State[3, i] = (byte)
((int)gfmultby0b(temp[0, i]) ^ (int)gfmultby0d(temp[1,
(int)gfmultby09(temp[2, i]) ^ (int)gfmultby0e(temp[3, i]));
}
}

i]) ^

i]) ^

i]) ^

i]) ^

private static byte gfmultby01(byte b)
{
return b;
}
private static byte gfmultby02(byte b)
{
if (b < 0x80)
return (byte)(int)(b 4, word[0] & 0x0f];
result[1] = this.Sbox[word[1] >> 4, word[1] & 0x0f];
result[2] = this.Sbox[word[2] >> 4, word[2] & 0x0f];
result[3] = this.Sbox[word[3] >> 4, word[3] & 0x0f];
return result;
}
private byte[] rotWord(byte[] word)
{
byte[] result = new byte[4];
result[0] = word[1];
result[1] = word[2];
result[2] = word[3];
result[3] = word[0];
return result;
}
private void keyExpansion()
{
this.w = new byte[11, 4, 4];
int indeks = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
w[0, i, j] = key[indeks];
indeks++;
}
}
byte[] temp = new byte[4];
for (int putaran = 1; putaran < 11; putaran++)
{
for (int i = 0; i < 4; i++)
{
temp[i] = w[putaran - 1, i, 3];
}
temp = rotWord(temp);
temp = subWord(temp);
for (int i = 0; i < 4; i++)
{
w[putaran, i, 0] = (byte)((int)w[putaran - 1, i, 0] ^
(int)temp[i] ^ (int)Rcon[putaran-1, i]);
}
for (int j = 1; j < 4; j++)
{
for (int i = 0; i < 4; i++)
{
w[putaran, i, j] = (byte)((int)w[putaran - 1, i, j] ^
(int)w[putaran, i, j - 1]);
}
}
}
}
}
}

Universitas Sumatera Utara

Lampiran A-17

Listing Algoritma RSA

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
namespace WindowsFormsApplication1
{
class RSA
{
public BigInteger e, n, d;
public string cipher, decipher;
static Random random = new Random(DateTime.Now.Millisecond);
public double waktuEnkripsi;
static BigInteger GenerateLargePrime(int length)
{
Primality primality = new Primality();
string numbers = "";
for (int i = 0; i < length; i++)
{
numbers += random.Next(0, 10);
}
BigInteger number = BigInteger.Parse(numbers);
if (primality.IsPrimePseudo(number))
{
return number;
}
else
{
return GenerateLargePrime(length);
}
}
public static BigInteger GCD(BigInteger m, BigInteger n)
{
BigInteger r = n % m;
while (r != 0)
{
r = m % n;
m = n;
n = r;
}
return m;
}

Universitas Sumatera Utara

Lampiran A-18

public static BigInteger nilaiD(BigInteger ex, BigInteger totien)
{
BigInteger d = 0;
BigInteger temp_d;
for (int k = 2; ; k++)
{
d = (totien * k + 1) / ex;
temp_d = (totien * k + 1) % ex;
if (temp_d == 0)
{
break;
}
}
return d;
}
public void enkripsi(string teks)
{
DateTime mulai = DateTime.Now;
TimeSpan selang;
BigInteger p, q, Ex;
while (true)
{
p = GenerateLargePrime(128);
q = GenerateLargePrime(128);
if (p.ToString().Length == q.ToString().Length && p < q)
{
break;
}
}
n = BigInteger.Multiply(p, q);
BigInteger totien = BigInteger.Multiply((p - 1), (q - 1));
Ex = 0;
for (BigInteger i = 2; i 256)
a = a % 256;
decipher += (char)a;
s = "";
}
i++;
}
}
}
}

Universitas Sumatera Utara