LAMPIRAN A: LISTING PROGRAM
1. Form Cover (Cover.cs)
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data;
using System.Drawing; using System.Linq; using System.Text;
using System.Threading.Tasks; using System.Windows.Forms;
namespace RSAHackWithPollard {
public partial class Cover : Form {
public Cover() {
InitializeComponent(); }
private void enterBTN_Click(object sender, EventArgs e) {
RSAHackWithPollardRho rsaPollard = new RSAHackWithPollardRho();
this.Hide();
rsaPollard.Show(); }
private void exitBTN_Click(object sender, EventArgs e) {
Application.Exit(); }
//http://www.codeproject.com/Articles/20379/Disabling-Close- Button-on-Forms
private const int CP_NOCLOSE_BUTTON = 0x200; protected override CreateParams CreateParams {
get {
CreateParams myCp = base.CreateParams; myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
return myCp; }
}
/////////////////////////////////////////////////////////// }
}
2. Form Utama (RSAHackWithPollard.cs)
using System;
using System.Windows.Forms; using System.Numerics;
using System.Security.Cryptography; using System.IO;
//using Microsoft.Office.Interop.Word; //using System.Collections.Generic; //using System.ComponentModel; //using System.Data;
//using System.Drawing; //using System.Text; //using System.Linq;
namespace RSAHackWithPollard {
public partial class RSAHackWithPollardRho : Form {
public RSAHackWithPollardRho() {
InitializeComponent(); }
private void clearKey() {
pTB.Text = ""; qTB.Text = ""; eTB.Text = ""; nTB.Text = ""; totnTB.Text = ""; dTB.Text = "";
}
private void clearKey2() {
pTB2.Text = ""; qTB2.Text = ""; totnTB2.Text = ""; dTB2.Text = "";
}
private BigInteger GCD(BigInteger m, BigInteger n) {
BigInteger r = m % n; while (r != 0)
{
m = n; n = r; r = m % n; }
return n; }
/*
private BigInteger modExp(BigInteger x, BigInteger y, BigInteger n)
{
BigInteger z = 1;
for (BigInteger i = 1; i <= y; i++) z = (x * z) % n;
return z; }
*/
private static bool IsProbablePrime(BigInteger n, int t) {
if (n == 2 || n == 3) return true;
if (n < 2 || n % 2 == 0) return false;
BigInteger m = n - 1; int k = 0;
while (m % 2 == 0) {
m /= 2; k += 1; }
RandomNumberGenerator rng = RandomNumberGenerator.Create();
byte[] bytes = new byte[n.ToByteArray().LongLength]; BigInteger a;
for (int i = 0; i < t; i++) {
do {
rng.GetBytes(bytes);
a = new BigInteger(bytes); }
while (a < 2 || a >= n - 2);
BigInteger x = BigInteger.ModPow(a, m, n); if (x == 1 || x == n - 1)
continue;
for (int r = 1; r < k; r++) {
x = BigInteger.ModPow(x, 2, n); if (x == 1)
return false; if (x == n - 1) break; }
if (x != n - 1) return false; }
return true; }
private BigInteger generateRandomNumber(int keylength) {
Random random = new Random(); string r = "";
int i;
if (keylength == 1) {
do //
i = random.Next(1, 10); //angka akhir prima pasti ganjil 0-9
while (i % 2 == 0); // r += i.ToString();
return (BigInteger.Parse(r)); }
r += random.Next(1, 10).ToString(); //angka pertama 0-9
for (i = 1; i < keylength - 1; i++) //angka berikutnya
r += random.Next(0, 10).ToString(); // 0-9 do //
i = random.Next(1, 10); //angka akhir prima pasti ganjil 1-9
while (i % 2 == 0); // r += i.ToString();
return (BigInteger.Parse(r)); }
}
private BigInteger inversMod(BigInteger a, BigInteger b) {
BigInteger dividend = a % b; BigInteger divisor = b;
BigInteger last_x = BigInteger.One; BigInteger curr_x = BigInteger.Zero;
while (divisor.Sign > 0) {
BigInteger quotient = dividend / divisor; BigInteger remainder = dividend % divisor; if (remainder.Sign <= 0)
break;
BigInteger next_x = last_x - curr_x * quotient; last_x = curr_x;
curr_x = next_x;
dividend = divisor; divisor = remainder; }
if (divisor != BigInteger.One)
throw new Exception("Numbers a and b are not relatively primes");
return (curr_x.Sign < 0 ? curr_x + b : curr_x); }
private void generateKey() {
BigInteger p, q, n, e = 1, totn, d = 1; int digit = int.Parse(keyCB.Text) / 2; Random random = new Random();
ulang:
p = generateRandomNumber(digit);
while (!IsProbablePrime(p, p.ToString().Length)) p = generateRandomNumber(digit);
q = generateRandomNumber(digit);
while (!IsProbablePrime(q, q.ToString().Length) || p == q)
q = generateRandomNumber(digit);
if (n < 256) //encoding table goto ulang;
totn = (p - 1) * (q - 1);
while (e <= 1 || e >= totn || GCD(e, totn) != 1) e =
generateRandomNumber(random.Next(1, digit*2 + 1)); /*Buat Lambat
while (e * d % totn != 1) d++;
*/
d = inversMod(e, totn);
pTB.Text = p.ToString(); qTB.Text = q.ToString(); nTB.Text = n.ToString();
totnTB.Text = totn.ToString(); eTB.Text = e.ToString(); dTB.Text = d.ToString();
}
private string openFileDoc() {
OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Word 1997-2003|*.doc|Word 2007-2013|*.docx|Rich Text File|*.rtf|Open Document
Text|*odt|Text File|*.txt|All Files (*.*)|*.*"; openFileDialog1.Title = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
//////http://ilmukomputer.org/wp- content/uploads/2009/04/c_sharp_part11-generate-word-document.zip
//buat directive imports
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application(); //buat filename dan drive document yang akan di buka object fileNameO = @openFileDialog1.FileName;
//buat object boolean object objFalse = false; object objTrue = true; //buat object
object missing = System.Reflection.Missing.Value; object emptyData = string.Empty;
//buat directive
Microsoft.Office.Interop.Word.Document aDoc =
wordApp.Documents.Open(ref fileNameO, ref objFalse, ref objTrue,
ref objFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref objTrue,
ref missing, ref missing, ref missing, ref missing); //copy keseluruhan isi doc
aDoc.ActiveWindow.Selection.WholeStory(); aDoc.ActiveWindow.Selection.Copy();
//buat iDataObject untuk menyimpan data IDataObject data =
System.Windows.Forms.Clipboard.GetDataObject(); String filetext =
oString();
System.Windows.Forms.Clipboard.SetDataObject(string.E mpty);
MessageBox.Show("Load document Success!", openFileDialog1.FileName);
wordApp.Quit();
//mengembalikan data ke fungsi (load data ke dalam richtextbox)
return filetext; }
else
return ""; }
private void saveFileDoc(string teks) {
String filetext = teks;
SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "Word 1997-2003|*.doc|Word 2007-2013|*.docx|Rich Text File|*.rtf|All Files (*.*)|*.*"; saveFileDialog1.Title = "";
if (saveFileDialog1.ShowDialog() == DialogResult.OK) {
Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
try {
//oWord.Visible = true;
var oDoc = oWord.Documents.Add();
//Insert a paragraph at the beginning of the document.
var paragraph1 = oDoc.Content.Paragraphs.Add(); paragraph1.Range.Text = filetext;
oDoc.SaveAs(@saveFileDialog1.FileName); }
catch (Exception err) {
MessageBox.Show(err.Message); }
finally {
MessageBox.Show("Save document Success", saveFileDialog1.FileName);
oWord.Quit(); }
} }
private void genBTN_Click(object sender, EventArgs e) {
if (keyCB.Text != "" && int.Parse(keyCB.Text) >= 4) {
clearKey();
statusLabel.Text = "Status : Generating Keys"; DateTime startTime = DateTime.Now;
generateKey();
DateTime endTime = DateTime.Now;
timeLabel.Text = "Time : " +
elapsed.Hours.ToString("00") + ":" + elapsed.Minutes.ToString("00") + ":" +
elapsed.Seconds.ToString("00") + "." + elapsed.Milliseconds.ToString("000");
statusLabel.Text = "Status : Generating Keys Complete";
privateGB.Visible = true; publicGB.Visible = true; encryptGB.Visible = true; decryptGB.Visible = true; MessageBox.Show("Generating Key Success!!!");
}
else if (keyCB.Text != "" && int.Parse(keyCB.Text) < 4) MessageBox.Show("Error!!!\nKey Length must at least 4 digit");
else
MessageBox.Show("Error!!!\nPlease define the key length!");
}
private void encryptBTN_Click(object sender, EventArgs e) {
if (eTB.Text != "" && nTB.Text != "" && PtRTB.Text != "") {
statusLabel.Text = "Status : Encrypting Text"; string PlainText = PtRTB.Text;
CtRTB.Text = "";
DateTime startTime = DateTime.Now; BigInteger C;
foreach (char pt in PlainText) {
//C = modExp((int)pt, BigInteger.Parse(eTB.Text), BigInteger.Parse(nTB.Text));
C = BigInteger.ModPow((int)pt, BigInteger.Parse(eTB.Text), BigInteger.Parse(nTB.Text));
CtRTB.Text += C.ToString() + '\n'; }
DateTime endTime = DateTime.Now;
TimeSpan elapsed = endTime.Subtract(startTime); timeLabel.Text = "Time : " +
elapsed.Hours.ToString("00") + ":" + elapsed.Minutes.ToString("00") + ":" + elapsed.Seconds.ToString("00") + "." + elapsed.Milliseconds.ToString("000");
statusLabel.Text = "Status : Encrypting Text Complete";
exportCt2BTN.Visible = true;
MessageBox.Show("Encrypting Complete!!!"); }
else
MessageBox.Show("Either e,n or PlainText is Blank!!!");
}
private void decryptBTN_Click(object sender, EventArgs e) {
if(nTB.Text != "" && dTB.Text != "" && CtRTB.Text != "") {
PtRTB.Text = "";
string[] Pt = Cipher.Trim().Split('\n'); DateTime startTime = DateTime.Now;
BigInteger P;
foreach (string c in Pt) {
if (c == "") break;
//P = modExp(BigInteger.Parse(c), BigInteger.Parse(dTB.Text),
BigInteger.Parse(nTB.Text));
P = BigInteger.ModPow(BigInteger.Parse(c), BigInteger.Parse(dTB.Text),
BigInteger.Parse(nTB.Text));
PtRTB.Text += Convert.ToChar((int)P).ToString(); }
DateTime endTime = DateTime.Now;
TimeSpan elapsed = endTime.Subtract(startTime); timeLabel.Text = "Time : " +
elapsed.Hours.ToString("00") + ":" + elapsed.Minutes.ToString("00") + ":" + elapsed.Seconds.ToString("00") + "." + elapsed.Milliseconds.ToString("000");
statusLabel.Text = "Status : Decrypting Cipher Complete";
MessageBox.Show("Decrypting Complete!!!"); }
else
MessageBox.Show("Either d,n or CipherText is Blank!!!");
}
private void open1BTN_Click(object sender, EventArgs e) {
PtRTB.Text = openFileDoc(); }
private void export1BTN_Click(object sender, EventArgs e) {
saveFileDoc(PtRTB.Text); }
private void open2BTN_Click(object sender, EventArgs e) {
CtRTB.Text = openFileDoc(); exportCt2BTN.Visible = true; }
private void export2BTN_Click(object sender, EventArgs e) {
saveFileDoc(CtRTB.Text); }
private void pbBTN_Click(object sender, EventArgs e) {
string filetext = nTB.Text + '\n' + eTB.Text;
SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "Text File|*.txt|All Files (*.*)|*.*";
saveFileDialog1.Title = "";
{
try
{
File.WriteAllText(saveFileDialog1.FileName, filetext);
MessageBox.Show("Publishing public key Success!", saveFileDialog1.FileName);
}
catch (IOException err) {
MessageBox.Show(err.Message); }
} }
private void imBTN_Click(object sender, EventArgs e) {
OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Text File|*.txt|All Files (*.*)|*.*";
openFileDialog1.Title = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
try {
string text =
File.ReadAllText(openFileDialog1.FileName); string[] teks = text.Trim().Split('\n'); int i = 0;
foreach (string c in teks) {
if (i > 2) break; if (i == 0)
nTB2.Text = c; else
eTB2.Text = c; i++;
}
MessageBox.Show("Importing public key Success", openFileDialog1.FileName);
}
catch (IOException err) {
MessageBox.Show(err.Message); }
} }
private BigInteger pollardRho1(BigInteger N, BigInteger a) {
BigInteger x = a, y = x, i = 1; //jika input bil prima
if (IsProbablePrime(N, N.ToString().Length)) return N;
//jika input bukan bil prima else
{
BigInteger fx, fy, s; while(true)
fx = x * x + 1;
fy = (y * y + 1) * (y * y + 1) + 1; x = fx % N;
y = fy % N;
s = GCD(BigInteger.Abs(x-y), N); if (s != 1 && s != N)
{
stepRTB.Text = i.ToString(); return s;
} i++; }
} }
private BigInteger pollardRho2(BigInteger N, BigInteger a) {
BigInteger x = a, y = x, i = 1; //jika input bil prima
if (IsProbablePrime(N, N.ToString().Length)) return N;
//jika input bukan bil prima else
{
BigInteger fx, fy, s; while (true)
{
fx = (x * x) + (12 * x) + 11;
fy = (((y * y) + (12 * y) + 11) * ((y * y) + (12 * y) + 11)) + (12 * ((y * y) + (12 * y) + 11)) + 11;
x = fx % N; y = fy % N;
s = GCD(BigInteger.Abs(x - y), N); if (s != 1 && s != N)
{
stepRTB.Text = i.ToString(); return s;
} i++; }
} }
private BigInteger pollardRho3(BigInteger N, BigInteger a) {
BigInteger x = a, y = x, i = 1; //jika input bil prima
if (IsProbablePrime(N, N.ToString().Length)) return N;
//jika input bukan bil prima else
{
BigInteger fx, fy, s; while (true)
{
fx = ((x * x) + (2 * x) + 23);
y = fy % N;
s = GCD(BigInteger.Abs(x - y), N); if (s != 1 && s != N)
{
stepRTB.Text = i.ToString(); return s;
} i++; }
} }
private BigInteger pollardRho4(BigInteger N, BigInteger a) {
BigInteger x = a, y = x, i=1; //jika input bil prima
if (IsProbablePrime(N, N.ToString().Length)) return N;
//jika input bukan bil prima else
{
BigInteger fx, fy, s; while (true)
{
fx = 3 * (x * x) + (4 * x) + 1;
fy = 3 * ((3 * (y * y) + (4 * y) + 1) * (3 * (y * y) + (4 * y) + 1)) + (4 * (3 * (y * y) + (4 * y) + 1)) + 1;
x = fx % N; y = fy % N;
s = GCD(BigInteger.Abs(x - y), N); if (s != 1 && s != N)
{
stepRTB.Text = i.ToString(); return s;
} i++; }
} }
private BigInteger pollardRho5(BigInteger N, BigInteger a) {
BigInteger x = a, y = x, i = 1; //jika input bil prima
if (IsProbablePrime(N, N.ToString().Length)) return N;
//jika input bukan bil prima else
{
BigInteger fx, fy, s; while (true)
{
fx = ((x * x) - x + 1);
fy = (((y * y) - y + 1) * ((y * y) - y + 1)) - ((y * y) - y + 1) + 1;
x = fx % N; y = fy % N;
{
stepRTB.Text = i.ToString(); return s;
} i++; }
} }
private void generateKey2() {
BigInteger p2, q2, e2, totn2, d2 = 1; p2 = BigInteger.Parse(pTB2.Text); q2 = BigInteger.Parse(qTB2.Text); e2 = BigInteger.Parse(eTB2.Text);
totn2 = (p2 - 1) * (q2 - 1); /*Buat Lambat
while (e2 * d2 % totn2 != 1) d2++;
*/
d2 = inversMod(e2, totn2);
totnTB2.Text = totn2.ToString(); dTB2.Text = d2.ToString();
}
private void pollardrhoBTN_Click(object sender, EventArgs e) {
if (nTB2.Text != "" && fxCB2.Text != "" && seedTB2.Text != "")
{
BigInteger N2 = BigInteger.Parse(nTB2.Text), p2, q2; BigInteger a2 = BigInteger.Parse(seedTB2.Text); try
{
statusLabel2.Text = "Status : Factoring n using Pollard rho";
clearKey2();
DateTime startTime = DateTime.Now;
if (fxCB2.SelectedIndex == 0) {
p2 = pollardRho1(N2, a2); q2 = N2 / p2;
pTB2.Text = p2.ToString(); qTB2.Text = q2.ToString(); }
else if (fxCB2.SelectedIndex == 1) {
p2 = pollardRho2(N2, a2); q2 = N2 / p2;
pTB2.Text = p2.ToString(); qTB2.Text = q2.ToString(); }
else if (fxCB2.SelectedIndex == 2) {
p2 = pollardRho3(N2, a2); q2 = N2 / p2;
qTB2.Text = q2.ToString(); }
else if (fxCB2.SelectedIndex == 3) {
p2 = pollardRho4(N2, a2); q2 = N2 / p2;
pTB2.Text = p2.ToString(); qTB2.Text = q2.ToString(); }
else if (fxCB2.SelectedIndex == 4) {
p2 = pollardRho5(N2, a2); q2 = N2 / p2;
pTB2.Text = p2.ToString(); qTB2.Text = q2.ToString(); }
else {
MessageBox.Show("Choose Polynomial ( f(x) )!!!");
}
DateTime endTime = DateTime.Now;
TimeSpan elapsed = endTime.Subtract(startTime); timeLabel2.Text = "Time : " +
elapsed.Hours.ToString("00") + ":" + elapsed.Minutes.ToString("00") + ":" + elapsed.Seconds.ToString("00") + "." + elapsed.Milliseconds.ToString("000");
statusLabel2.Text = "Status : Factoring n using Pollard rho Complete";
privateGB2.Visible = true; publicGB2.Visible = true;
stepRTB.Visible = true; stepsLabel2.Visible = true;
MessageBox.Show("p and q have been Found\nFactoring n Success"); }
catch (Exception err) {
MessageBox.Show(err.Message); }
} else
MessageBox.Show("n or f(x) or seed is empty"); }
private void calcBTN2_Click(object sender, EventArgs e) {
if (nTB2.Text != "" && eTB2.Text != "" && pTB2.Text != "" && qTB2.Text != "")
{
statusLabel2.Text = "Status : Calculating d"; DateTime startTime = DateTime.Now;
generateKey2();
DateTime endTime = DateTime.Now;
TimeSpan elapsed = endTime.Subtract(startTime); timeLabel2.Text = "Time : " +
elapsed.Milliseconds.ToString("000");
statusLabel2.Text = "Status : Calculating d Complete";
label13.Visible = true; label16.Visible = true; totnTB2.Visible = true; dTB2.Visible = true;
decryptGB2.Visible = true; decryptedGB2.Visible = true;
stepRTB.Visible = false; stepsLabel2.Visible = false; MessageBox.Show("Calculating Private Key Complete!"); }
else
MessageBox.Show("A Textbox is blank, please fill in something!");
}
private void decryptBTN2_Click(object sender, EventArgs e) {
if (nTB2.Text != "" && dTB2.Text != "" && CtRTB2.Text != "")
{
statusLabel2.Text = "Status : Decrypting Cipher"; string Cipher2 = CtRTB2.Text;
PtRTB2.Text = "";
string[] Pt2 = Cipher2.Trim().Split('\n');
DateTime startTime = DateTime.Now; BigInteger P2;
foreach (string c2 in Pt2) {
if (c2 == "") break;
//P2 = modExp(BigInteger.Parse(c2), BigInteger.Parse(dTB2.Text),
BigInteger.Parse(nTB2.Text));
P2 = BigInteger.ModPow(BigInteger.Parse(c2), BigInteger.Parse(dTB2.Text),
BigInteger.Parse(nTB2.Text)); PtRTB2.Text +=
Convert.ToChar((int)P2).ToString(); }
DateTime endTime = DateTime.Now;
TimeSpan elapsed = endTime.Subtract(startTime); timeLabel2.Text = "Time : " +
elapsed.Hours.ToString("00") + ":" + elapsed.Minutes.ToString("00") + ":" + elapsed.Seconds.ToString("00") + "." + elapsed.Milliseconds.ToString("000");
statusLabel2.Text = "Status : Decrypting Cipher Complete";
MessageBox.Show("Decrypting Complete!!!"); }
else
MessageBox.Show("Either d,n or CipherText is Blank!!!");
stepRTB.Visible = false; stepsLabel2.Visible = false; }
CtRTB2.Text = openFileDoc();stepRTB.Visible = false; stepsLabel2.Visible = false;
}
private void exportPtBTN2_Click(object sender, EventArgs e) {
saveFileDoc(PtRTB2.Text); stepRTB.Visible = false; stepsLabel2.Visible = false;
}
private void pb2BTN_Click(object sender, EventArgs e) {
nTB2.Text = nTB.Text; eTB2.Text = eTB.Text; MessageBox.Show("Exporting n and e to Pollard Tab Complete");
}
private void exportCt2BTN_Click(object sender, EventArgs e) {
CtRTB2.Text = CtRTB.Text;
MessageBox.Show("Exporting CipherText to Pollard Tab Complete");
}
private void exitBTN_Click(object sender, EventArgs e) {
Application.Exit(); }
private void backBTN_Click(object sender, EventArgs e) {
Cover cover = new Cover(); this.Dispose(); cover.Show(); }
//http://www.codeproject.com/Articles/20379/Disabling-Close-Button-on-Forms
private const int CP_NOCLOSE_BUTTON = 0x200; protected override CreateParams CreateParams {
get {
CreateParams myCp = base.CreateParams; myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
return myCp; }
} ////// }