LISTING PROGRAM
1.
Bahasa Python
1.1.
Module
gate.py
import sys
sys.path.append("C:\skrip")
import pythonA
def gerbang(fungsi):
if(fungsi == "bangkitKunci"):
from pythonA import bangkitkanKunci
a = str(sys.argv[1]) global b
b = int(sys.argv[2]) global c
c = int(sys.argv[3]) global d
global e
e = str(sys.argv[5]) gerbang(a)
1.2.
Module
pythonA.py
import random, time
class Timer(object): '''
timer starts with class initialisation '''
def __init__(self): self.t1= time.time() self.t2= time.time()
def getElapsedlTime(self):
# gets total elapsed from class initialsation self.delta=time.time()-self.t1
return '{0:.3f}'.format(self.delta)
def getTimeDifference(self):
# gets time elapsed from previous reading (for first reading this is equal to total time elapsed getElapsedlTime()
self.delta=time.time()-self.t2 self.t2 = time.time()
return '{0:.3f}'.format(self.delta)
def differentRandom(n): x = []
for i in range(0, len(str(n))): a = random.randint(2,n-1) while a in x:
a = random.randint(2,n-1) x.append(a)
return x
def toBin(x):
return "{0:b}".format(x)
def ntobin(n):
return toBin(n-1)
def modExpSAM(x, y, z): b = toBin(y)
t = len(b) result = 1
for i in range(0, t):
result = (result * result) % z
def moduloEks(acak, y, bil):
syarat_tdk_terpenuhi = True while syarat_tdk_terpenuhi: p = random.randint(3,10000) if (p % 4 == 3) and isPrime(p): syarat_tdk_terpenuhi = False
syarat_tdk_terpenuhi = True while syarat_tdk_terpenuhi: q = random.randint(3,10000)
if (q != p) and (q % 4 == 3) and isPrime(q): syarat_tdk_terpenuhi = False
n = p * q return p, q, n
def cekKunci(key1, key2):
if (key1 % 4 == 3) and isPrime(key1) and (key2 % 4 == 3) and isPrime(key2) and (key1 != key2):
return "T" else:
def enkripsi(P, n): if P < n:
C = modExpSAM(P,2,n) return C
return False
def encryptAll(P, n): C = []
def programEnkripsi(plaintext, kunciPublik): measure=Timer()
P = str(plaintext) n = int(kunciPublik) C = encryptAll(P, n)
C.append(measure.getElapsedlTime()) return C
def autoDekripsi(C, p, q):
mp = modExpSAM(C, (p+1)//4, p)
P1 = (x + y) % n
def decryptCiphertext2(C, p, q): measure=Timer()
C = convertListStringCToInt(C) P = []
for i in range(0, len(C)):
minP = autoDekripsi(C[i], p , q) P.append(chr(minP))
stringP = ''.join(P)
et = measure.getElapsedlTime() cetak = []
cetak.append(stringP) cetak.append(et)
temp = tulisHasilDekripsi(stringP) return cetak
def decryptCiphertext(C, p, q): measure=Timer()
et = measure.getElapsedlTime() cetak = []
cetak.append(stringP) cetak.append(et)
for i in range(len(L)): temp += str(L[i]) if(i < len(L)-1): temp += ',' return temp
def toDec(self, P): temp = []
for i in range(len(P)): temp.append(ord(P[i])) return temp
def toASCII(self, string): string = self.toLI(string) temp = ""
for e in string: temp += chr(e) return temp
#hasil = isPrime(100) #print(hasil)
2.
Bahasa C#
2.1.
Fungsi Pembangkit Kunci pada
FormPembangkitKunci.cs
public void runPythonBangkitKunci(){
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = "c:\\Python33\\python.exe";
p.RedirectStandardOutput = true;
p.UseShellExecute = false;
p.Arguments = "c:\\skrip\\gate.py "+ "bangkitKunci 0 0 'x' 'x'"; // start the python program with two parameters
try{
using(Process exeProc = Process.Start(p)){
StreamReader s = exeProc.StandardOutput;
String output = s.ReadToEnd();
string []r = output.Split(new char[]{' '});
textBoxKunciP.Text = r[0].ToString();
textBoxKunciQ.Text = r[1].ToString();
}
}
catch{
MessageBox.Show("Gagal melakukan pembangkit kunci.", "Kesalahan Proses Pembangkit Kunci",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
void ButtonGenerateClick(object sender, EventArgs e)
{
if(!string.IsNullOrWhiteSpace(textBoxKunciP.Text) & !string.IsNullOrWhiteSpace(textBoxKunciQ.Text)){
if(MessageBox.Show("Kunci yang anda tulis sebelumnya akan terhapus. Yakin untuk melanjutkan?", "Peringatan:
Pembangkitan Kunci", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK){
runPythonBangkitKunci();
}
}
else{
runPythonBangkitKunci();
}
}
2.2.
Fungsi Cek Kunci pada
FormPembangkitKunci.cs
void ButtonCekKunciClick(object sender, EventArgs e){
if(string.IsNullOrWhiteSpace(textBoxKunciP.Text) | string.IsNullOrWhiteSpace(textBoxKunciQ.Text)){
MessageBox.Show("Pasangan kunci tidak boleh ada yang kosong.", "Kesalahan Kunci", MessageBoxButtons.OK,
textBoxKunciN.Text = "";
}
else if(!string.IsNullOrWhiteSpace(textBoxKunciP.Text) & !string.IsNullOrWhiteSpace(textBoxKunciQ.Text)){
string hasil = runPythonCekKunci();
if(String.Equals(hasil, "T")){
int a = int.Parse(textBoxKunciP.Text);
int b = int.Parse(textBoxKunciQ.Text);
int c = a*b;
textBoxKunciN.Text = c.ToString();
}
else{
MessageBox.Show("Kunci tidak memenuhi syarat.", "Kesalahan Kunci", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBoxKunciN.Text = "";
}
}
}
public string runPythonCekKunci(){
string hasil;
int kunci1 = int.Parse(textBoxKunciP.Text);
int kunci2 = int.Parse(textBoxKunciQ.Text);
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = "c:\\Python33\\python.exe";
p.RedirectStandardOutput = true;
p.UseShellExecute = false;
p.Arguments = "c:\\skrip\\gate.py "+ "cekKunci " + kunci1 + " " + kunci2 + " 'x' 'x'";
try{
StreamReader s = exeProc.StandardOutput;
String output = s.ReadToEnd();
string []r = output.Split(new char[]{' '});
hasil = r[0][0].ToString();
}
}
catch{
MessageBox.Show("Gagal dalam melakukan cek kunci.", "Kesalahan Cek Kunci", MessageBoxButtons.OK,
MessageBoxIcon.Error);
hasil = "";
}
return hasil;
}
2.3.Fungsi Enkrip Pesan
void Button1EnkripsiClick(object sender, EventArgse)
{
if(string.IsNullOrWhiteSpace(textBox1plaintext.Text) | stri
ng.IsNullOrWhiteSpace(textBox1keyotp.Text)){
MessageBox.Show("Kunci OTP atau Pesan tidak boleh
kosong.", "Kesalahan
Dekripsi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}else{
textBox2chiphertext.Text="";
//ambilplainteks
string plain=textBox1plaintext.Text;
//ambilpanjangplainteks
int panjangplain=textBox1plaintext.Text.Length;
//ambilkunci
string kunci=textBox1keyotp.Text;
int pk=kunci.Length;
string cipher="";
//cekpanjangkuncidgnplain
kunciotp= "";
if(pk<panjangplain){
//contohkunci=abc
//plain:farid
//kunciotp=abc
kunciotp +=kunci;
string tempplain=Regex.Replace(plain, @"\s+", "");
int pjgtempplain=tempplain.Length;
for(int i = 0; i<(panjangplain-pjgtempplain);i++){
//kunciotp=abcfa
tempplain+= (char)tempplain[i];
}
//MessageBox.Show(tempplain);
//proses menyamakan kunci == plain
//perulangan sesuai kurangnya kunci
for(int i = 0; i<(panjangplain-pk);i++){
//kunciotp=abcfa
kunciotp+= (char)tempplain[i];
}
}else{
kunciotp = kunci;
}
DateTime startTime = DateTime.Now;
MessageBox.Show("panjang karakter :
"+panjangplain.ToString());
//algoritma otp
for(int i=0; i<panjangplain;i++){
cipher+=(char) (plain[i] + kunciotp[i]);
}
void Button1EnkripkeyClick(object sender, EventArgs e) {
//cek kunci
if(string.IsNullOrWhiteSpace(textBoxkeyRabin.Text)){ MessageBox.Show("Kunci publik belum ada.", "Kesalahan
Enkripsi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//cek plain
else if(string.IsNullOrEmpty(textBox1keyotp.Text)){ MessageBox.Show("Tidak ada pesan (plaintext) yang
terdeteksi.", "Kesalahan Enkripsi", MessageBoxButtons.OK, MessageBoxIcon.Error);
} else{
//jika ada
string cipherText = runPythonEnkripsi(); }
textBox1cipherkey.Copy();
public string runPythonEnkripsi(){ string hasil;
string hasil2; //ambil kunci
int n = int.Parse(textBoxkeyRabin.Text); //ambil pesan
string plaintext =kunciotp; //ambil pnjang kunci
int pjgPlaintext = kunciotp.Length;
//
string pltxt1 = plaintext.Replace("\"","\\\""); int temporary = 0;
ProcessStartInfo p = new ProcessStartInfo(); p.FileName = "c:\\Python33\\python.exe"; p.RedirectStandardOutput = true;
p.UseShellExecute = false; // make sure we can read the output from stdout
p.Arguments = "c:\\skrip\\gate.py "+ "enkripsi " + n + " " + temporary + " \"" + pltxt1 + "\" 'x'";
try{
using(Process exeProc = Process.Start(p)){ StreamReader s = exeProc.StandardOutput; String output = s.ReadToEnd();
string r = output; // get the parameter hasil = r.ToString();
hasil2 = hasil.Replace(" ",",");
string elapsedTime = hasil2.Split(',').Last();
hasil2 = hasil2.Remove((hasil2.Length - elapsedTime.Length), elapsedTime.Length);
hasil2 = hasil2.Remove(hasil2.Length-1,1);
if(hasil2 == "False"){
MessageBox.Show("Kunci publik tidak memenuhi syarat.", "Kesalahan Melakukan Enkripsi", MessageBoxButtons.OK, MessageBoxIcon.Error);
hasil2 = ""; }
else{
textBox1cipherkey.Text = hasil2.ToString(); //Untuk menampilkan ASCII
try{
string kode = textBox1cipherkey.Text.ToString(); List<string> listHasilSplit = new
List<string>(kode.Split(','));
List<Int32> ci = listHasilSplit.ConvertAll(x => Convert.ToInt32(x));
List<char> liChar = new List<char>(ci.ConvertAll(x => Convert.ToChar(x)));
string charToString = string.Join("", liChar.ToArray());
kode = charToString;
} catch{
}
//End Tampil ASCII
MessageBox.Show("Berhasil melakukan enkripsi (" + elapsedTime + " detik)", "Informasi", MessageBoxButtons.OK,
MessageBoxIcon.Information); }
}
2.5.
Program dekripsi Kunci Pesan
void Button1dekripkeyotpClick(object sender, EventArgs e) {
if(string.IsNullOrWhiteSpace(kp.Text)|string.IsNullOrWhiteSpace(kq .Text)){
MessageBox.Show("Kunci private tidak boleh kosong.", "Kesalahan Dekripsi", MessageBoxButtons.OK,
else if(string.IsNullOrEmpty(ctotp.Text)){
MessageBox.Show("Tidak ada ciphertext terdeteksi.", "Kesalahan Dekripsi", MessageBoxButtons.OK,
MessageBoxIcon.Error); }
else{ try{
string ciphertext = ctotp.Text.ToString(); string[] bufferS = new
string[ctotp.Text.ToString().Length]; bufferS = ubahKeInt();
string result = ConvertStringArrayToStringJoin(bufferS);
List<string> listHasilSplit = new List<string>(ciphertext.Split(','));
List<Int32> ci =
listHasilSplit.ConvertAll(x => Convert.ToInt32(x));
List<char> liChar = new List<char>(ci.ConvertAll(x => Convert.ToChar(x)));
string charToString = string.Join("", liChar.ToArray());
ciphertext = charToString;
string plaintext = runPythonDekripsi(ciphertext); }
catch{
//MessageBox.Show("Ciphertext harus berupa angka yang dipisah dengan koma.", "Kesalahan melakukan dekripsi", MessageBoxButtons.OK, MessageBoxIcon.Error);
string ciphertext = ctotp.Text.ToString();
string plaintext = runPythonDekripsi(ciphertext); }
}
}
public string ConvertStringArrayToStringJoin(string[] array){ string result = string.Join(",", array);
return result; }
string[] ubahKeInt(){
string C = ctotp.Text.ToString(); char[] charArr = C.ToCharArray(); int val;
string[] bufferS = new string[charArr.Length]; for(int i = 0; i < charArr.Length; i++){
}
return bufferS; }
public string runPythonDekripsi(string strIntC){ string hasil;
string hasil2;
int kunciP = int.Parse(kp.Text); int kunciQ = int.Parse(kq.Text);
ProcessStartInfo p = new ProcessStartInfo(); p.FileName = "c:\\Python33\\python.exe"; p.RedirectStandardOutput = true;
p.UseShellExecute = false; // make sure we can read the output from stdout
p.Arguments = "c:\\skrip\\gate.py "+ "dek " + kunciP + " " + kunciQ + " " + "\"" + strIntC + "\" 'x'"; // start the python program with two parameters
try{
using(Process exeProc = Process.Start(p)){ StreamReader s =
exeProc.StandardOutput;
String output = s.ReadToEnd(); string r = output; // get the parameter
hasil = r.ToString();
string[] parts = hasil.Split(' ');
string elapsedTime = parts[parts.Length - 2];
hasil2 = hasil.Remove((hasil.Length - elapsedTime.Length), elapsedTime.Length);
hasil2 = hasil2.Remove(hasil2.Length-1,1);
hasil2 =
hasil2.Remove(hasil2.Length-1,1); // menghilangkan spasi di akhir kalimat
kotp.Text = hasil2.ToString();
int pjgPesanAsli = hasil2.Length;
MessageBox.Show("Berhasil melakukan dekripsi (" + elapsedTime + " detik)", "Informasi",
MessageBoxButtons.OK, MessageBoxIcon.Information); }
catch{
//throw;
strIntC = ctotp.Text.ToString();
p.FileName = "c:\\Python33\\python.exe"; p.RedirectStandardOutput = true;
p.UseShellExecute = false; // make sure we can read the output from stdout
p.Arguments = "c:\\skrip\\gate.py "+ "dek2 " + kunciP + " " + kunciQ + " " + "\"" + strIntC + "\" 'x'"; // start the python program with two parameters
try{
using(Process exeProc = Process.Start(p)){
StreamReader s = exeProc.StandardOutput;
String output = s.ReadToEnd(); string r = output; // get the parameter
hasil = r.ToString();
string[] parts = hasil.Split(' ');
string elapsedTime = parts[parts.Length - 2];
hasil2 = hasil.Remove((hasil.Length - elapsedTime.Length), elapsedTime.Length);
hasil2 = hasil2.Remove(hasil2.Length-1,1);
hasil2 =
hasil2.Remove(hasil2.Length-1,1); // menghilangkan spasi di akhir kalimat
kotp.Text = hasil2; int pjgPesanAsli = hasil2.Length;
MessageBox.Show("Berhasil melakukan dekripsi (" + elapsedTime + " detik)", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }
catch{
//throw;
p.FileName = "c:\\Python33\\python.exe";
p.RedirectStandardOutput = true; p.UseShellExecute = false; // make sure we can read the output from stdout
p.Arguments = "c:\\skrip\\gate.py "+ "dek2 " + kunciP + " " + kunciQ + " " + "\"" + strIntC + "\" 'x'"; // start the python program with two parameters
try{
using(Process exeProc = Process.Start(p)){
StreamReader s = exeProc.StandardOutput;
String output = s.ReadToEnd();
string r = output; // get the parameter
hasil = r.ToString();
string[] parts = hasil.Split(' ');
string elapsedTime = parts[parts.Length - 2];
hasil2 = hasil.Remove((hasil.Length - elapsedTime.Length), elapsedTime.Length);
hasil2 = hasil2.Remove(hasil2.Length-1,1);
hasil2 =
hasil2.Remove(hasil2.Length-1,1); // menghilangkan spasi di akhir kalimat
kotp.Text = hasil2; int pjgPesanAsli = hasil2.Length;
MessageBox.Show("Berhasil melakukan dekripsi (" + elapsedTime + " detik)", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }
catch{
MessageBox.Show("Kesalahan melakukan dekripsi.", "Kesalahan", MessageBoxButtons.OK,
MessageBoxIcon.Error);
hasil2 = ""; }
} }
return hasil2; }
2.6.
Perogram dekripsi pesan
void Button2dekripsiClick(object sender, EventArgs e) {
if(string.IsNullOrWhiteSpace(textBox1cipher.Text) | string.IsNullOrWhiteSpace(textBox1cipherkey.Text)){
MessageBox.Show("Kunci OTP dan Pesan Cipher tidak boleh kosong.", "Kesalahan Dekripsi", MessageBoxButtons.OK, MessageBoxIcon.Error);}
else{
//ambil cipher
string cipher=textBox1cipher.Text; //panjang cipher
int panjang= textBox1cipher.Text.Length; //ambil kunci
string kunci = kotp.Text; string pesan="";
DateTime waktuawal = DateTime.Now; //algoritma otp
for(int i=0; i<panjang;i++){
pesan += (char)(cipher[i]-kunci[i]); //MessageBox.Show(pesan.ToString()); }
MessageBox.Show("panjang : "+panjang.ToString()); MessageBox.Show("tes");
DateTime waktuakhir = DateTime.Now;
/*MessageBox.Show("Time : " + elapsed.Hours.ToString("00") + ":" +elapsed.Minutes.ToString("00") + ":" +
elapsed.Seconds.ToString("00") + "." +elapsed.Milliseconds.ToString("000"));*/
pesanbox.Text=pesan;
TimeSpan selisihwaktu = waktuakhir.Subtract(waktuawal); MessageBox.Show("Time : "
+selisihwaktu.Hours.ToString()+"