LISTING PROGRAM
Form „Halaman 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; namespace MetodeKraitchik {
public partial class HalamanUtamaForm : Form {
public HalamanUtamaForm() {
InitializeComponent(); }
private void pemecahanKunciButton_Click(object sender, EventArgs e)
{
PemecahanForm form = new PemecahanForm(); form.ShowDialog();
}
private void dekripsiButton_Click(object sender, EventArgs e) {
dekripsiForm form = new dekripsiForm(); form.ShowDialog();
}
private void tentangButton_Click(object sender, EventArgs e) {
tentangForm form = new tentangForm(); form.ShowDialog();
}
private void bantuanButton_Click(object sender, EventArgs e) {
bantuanForm form = new bantuanForm(); form.ShowDialog();
} } }
Form „Pemecahan Kunci RSA‟
using System;
using System.Collections.Generic; using System.ComponentModel;
using System.Data; using System.Drawing; using System.Linq; using System.Numerics; using System.Text; using System.Threading.Tasks; using System.Threading; using System.Windows.Forms; using System.Diagnostics; namespace MetodeKraitchik {
public partial class PemecahanForm : Form {
DateTime start, finish;
BigInteger D, E, p, q;
BigInteger[] hasil = new BigInteger[2]; public PemecahanForm()
{
InitializeComponent(); }
public static bool IsSquare(BigInteger X) { BigInteger sqr = AkarkanBigInteger.SRO(X); if (sqr * sqr == X) { return true; } return false; }
public static BigInteger[] Kraitchik(BigInteger N) {
BigInteger[] hasil = new BigInteger[2]; bool found = false;
BigInteger x = AkarkanBigInteger.SRO(N); BigInteger k = 1; while (!found) { if (IsSquare(x * x - k * N) && x * x - k * N != 0) { BigInteger y = AkarBigInteger.SRO(x * x - k * N); if ((x + y) - k * N != 0 && (x - y) - k * N != 0) { hasil[0] = (x + y) / k; hasil[1] = x - y;
if (0 < hasil[0] && hasil[0] < N && 0 < hasil[1] && hasil[1] < N) found = true; } k++; } x++; } return hasil; }
public static BigInteger GCD(BigInteger m, BigInteger n) { BigInteger r = n % m; while (r != 0) { r = m % n; m = n; n = r; } return m; }
public static BigInteger nilaiE(BigInteger totienK, BigInteger nK) { BigInteger E = 2; while (GCD(totienK, E) != 1) { E++; } return E; }
public static BigInteger nilaiD(BigInteger EK, BigInteger totienK)
//modular Inverse Extended Euclidean GCD {
BigInteger x = 1, y = 0, xLast = 0, yLast = 1; BigInteger a = EK, b = totienK;
BigInteger c, d, q, r; while (a != 1) { q = b / a; r = b % a; c = xLast - q * x; d = yLast - q * y; xLast = x; yLast = y; x = c; y = d; b = a; a = r; } x = (x + totienK) % totienK; return x; }
public static BigInteger faktorPrima(BigInteger n) { BigInteger k = 2; while (k * k <= n) { if (n % k == 0) { n /= k; } else { ++k; }
}
return n; }
private void button1_Click(object sender, EventArgs e) { try { if (textBox1.Text.Length <= 64) { textBox3.Text = ""; textBox4.Text = ""; start = DateTime.Now; timer1.Enabled = true; hasil = Kraitchik(BigInteger.Parse(textBox1.Text)); finish = DateTime.Now; } else {
MessageBox.Show("Panjang Kunci maksimal 64 digit!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} } catch {
MessageBox.Show("Silakan Input Kunci Publik (n)", "Missing data", MessageBoxButtons.OK,
MessageBoxIcon.Error); }
}
private void timer1_Tick(object sender, EventArgs e) {
TimeSpan selisih = finish.Subtract(start); textBox3.Text += hasil[0].ToString() + "\n"; textBox4.Text += hasil[1].ToString();
textBox2.Text = selisih.TotalMilliseconds + " ms"; timer1.Enabled = false;
MessageBox.Show("Pemfaktoran Kunci Publik (n) Berhasil", "Success", MessageBoxButtons.OK,
MessageBoxIcon.Information); }
private void button2_Click(object sender, EventArgs e) { textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; richTextBox3.Text = ""; }
private void button3_Click(object sender, EventArgs e) {
form.ShowDialog(); }
private void button4_Click(object sender, EventArgs e) { try { richTextBox3.Text = ""; p = BigInteger.Parse(textBox3.Text); q = BigInteger.Parse(textBox4.Text); BigInteger n = BigInteger.Multiply(p, q);
BigInteger totien = BigInteger.Multiply(p - 1, q - 1);
E = nilaiE(totien, n);
richTextBox3.Text += "Kunci Publik " + "(" + "e =" + E.ToString() + ", " + "n =" + n.ToString() + ")" + "\n\n";
d = nilaiD(E, totien);
richTextBox3.Text += "Kunci Privat " + "(" + "d =" + d.ToString() + ", " + "n =" + n.ToString() + ")" + "\n\n";
richTextBox3.Text += "Panjang Kunci Publik (n) = " + textBox1.Text.Length + "\n\n";
richTextBox3.Text += "Totien (n) = " + totien.ToString() + "\n\n";
BigInteger S = p - q;
richTextBox3.Text += "Selisih Faktor Kunci (p-q) = " + S.ToString() + "\n\n"; BigInteger G = GCD(p - 1, q - 1); richTextBox3.Text += "GCD (p-1, q-1) = " + G.ToString() + "\n\n"; BigInteger Fp = faktorPrima(p - 1);
richTextBox3.Text += "Faktor Prima p-1 = " + Fp.ToString() + "\n\n";
BigInteger Fq = faktorPrima(q - 1);
richTextBox3.Text += "Faktor Prima q-1 = " + Fq.ToString() ;
} catch {
MessageBox.Show("Silakan Input Kunci Publik (n)", "Missing data", MessageBoxButtons.OK,
MessageBoxIcon.Error); }
} } }
Form „Dekripsi RSA‟
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 MetodeKraitchik {
public partial class dekripsiForm : Form {
string[] baris = null; public dekripsiForm() {
InitializeComponent(); }
private void bantuanButton_Click(object sender, EventArgs e) {
bantuanForm form = new bantuanForm(); form.ShowDialog();
}
public int[] dekripsiRSA(BigInteger[] a) {
BigInteger n = BigInteger.Parse(textBox1.Text); BigInteger d = BigInteger.Parse(textBox2.Text); int[] p = new int[a.Length];
for (int i = 0; i < a.Length; i++) {
p[i] = (int)BigInteger.ModPow(a[i], d, n); }
return p; }
public static string convertAscii(int[] p) {
string temp = "";
for (int i = 0; i < p.Length; i++) { char a=(char)p[i]; temp += a.ToString(); } return temp; }
private void button1_Click(object sender, EventArgs e) {
OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog1.ShowDialog() == DialogResult.OK) {
baris =
string gabung = "";
foreach (string line in baris) { gabung += line + ""; } textBox3.Text = openFileDialog1.FileName; } }
private void button2_Click(object sender, EventArgs e) {
try {
BigInteger[] daftar = new BigInteger[baris.Length]; int i = 0;
foreach (string line in baris) {
daftar[i] = BigInteger.Parse(line); i++;
}
int[] hasildekrip = dekripsiRSA(daftar); string hasilx = convertAscii(hasildekrip);
if (saveFileDialog1.ShowDialog() == DialogResult.OK) {
File.WriteAllText(@saveFileDialog1.FileName,hasil x);
MessageBox.Show("Dekripsi Berhasil", "Sukses", MessageBoxButtons.OK,MessageBoxIcon.Information); MessageBox.Show(hasilx); textBox4.Text = saveFileDialog1.FileName; } } catch {
MessageBox.Show("Input kunci privat (n,d) atau Input File terenkripsi", "Error Input",
MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
private void button3_Click(object sender, EventArgs e) { textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; } } }