2. Primitive Root (prim_root.py) - Teknik Pemecahan Kunci Privat Elgamal dengan Algoritma Baby Step Giant Step

Gratis

0
1
19
7 months ago
Preview
Full text

LISTING PROGRAM 1.

   Lehmann Primality Test (lehmann.py)

  from random import randint import timeit f = file("digit.txt",'r') data = f.read() n = int(data) #generate random integer and checking even integer def random_with_N_digits(n): range_start = 10**(n-1) range_end = (10**n)-1 l = randint(range_start, range_end) if l % 2 == 0 or l % 3 == 0: return random_with_N_digits(n) return l #prime check def lehman(l): persen = 50 probability = 0 for i in range(10): a = randint(1,l) if pow(a,(l-1)/2,l) == 1 or pow(a,(l-1)/2,l) == -1 : probability += persen persen = persen / 2 #print probability if probability >= 97 : return l else : return "fail" #jalankan fungsi def begin(): m = random_with_N_digits(n) x = lehman(m) if x == "fail": return begin() print "finish lehman",x return x

2. Primitive Root (prim_root.py)

  from random import randint import lehmann import sys import math

  #import prime number dari file lehmann.py prime = lehmann.begin() #generate random prime_roots def random_prime_roots(prime): l = randint(2, prime-1) print l return l #find distinct primes def prime_factors(n): i = 2 while i * i <= n: if pow(n,1,i) : i += 1 else: n //= i factors.append(i) print i if n > 1: factors.append(n) print n def prime_roots2(rand_int, p): for i in range(0, len(fixFactors)): a = pow(rand_int,(p-1)/fixFactors[i],p) if a == 1: print "failed" return "fail" return rand_int prime_factors(prime-1) [fixFactors.append(item) for item in factors if item not in fixFactors] #find prime root def begin_prime_root(): rand_int = random_prime_roots(prime) x = prime_roots2(rand_int,prime) if x == "fail": return begin_prime_root() return rand_int del sys.modules["lehmann"] del lehmann

3. Bangkit Kunci (generate_key.py)

  import timeit start = timeit.default_timer() import prim_root from random import randint import sys def random_secret_key(): l = randint(1, p-2) return l p = prim_root.prime alpha = prim_root.primitive_root a = random_secret_key() #bangkitkan kunci y = pow(alpha,a,p) end = timeit.default_timer() 4.

   Main Form (MainForm.py)

  from javax.swing import * from java.awt import * from EncryptionForm import frameEn from DecryptionForm import frameDe from BSGS import frameBSGS from About import frameAb from Help import frameHe def openEnForm(event): frameEn.visible = True def openDeForm(event): frameDe.visible = True def openBSGSForm(event): frameBSGS.visible = True def openAb(event): frameAb.visible = True def openHe(event): frameHe.visible = True def generateKey(): start = timeit.default_timer() import generate_key textAlpha.text = str(generate_key.alpha) textPrime.text = str(generate_key.p) textY.text = str(generate_key.y) end = timeit.default_timer() textTime.text = str((end - start) * 1000) textA.text = str(generate_key.a) del sys.modules["generate_key"] del generate_key def generateKeyAndDigit(event): variable = str(textDigit.text) f = file("digit.txt",'w') f.write(variable) f.close() generateKey() #create frame frame = JFrame("Main Form", size=(400,325)) #create menu menubar = JMenuBar() fileMenu = JMenu("File") fileMenu.setMnemonic(KeyEvent.VK_F)

fileExit = JMenuItem("About", actionPerformed = openAb) fileExit.setMnemonic(KeyEvent.VK_A) fileExit.setToolTipText("About Program") fileMenu.add(fileExit) fileHelp = JMenuItem("Help", actionPerformed = openHe) fileHelp.setMnemonic(KeyEvent.VK_H) fileHelp.setToolTipText("User Help") fileMenu.add(fileHelp) menubar.add(fileMenu) frame.setJMenuBar(menubar) #generate key panel = JPanel() panel.setLayout(None) frame.getContentPane().add(panel) gKey = JLabel("Public Key") gKey.setBounds(35,0,100,100) gKey.setFont(Font("Times New Roman",Font.BOLD,14)) panel.add(gKey) inputDigit = JLabel("Prime Digits") inputDigit.setBounds(5,25,100,100) inputDigit.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(inputDigit) textDigit = JTextField() textDigit.setBounds(85,65,50,20) panel.add(textDigit) buttonDigit = JButton("Enter", actionPerformed = generateKeyAndDigit) buttonDigit.setBounds(150,65,70,20) panel.add(buttonDigit) labelAlpha = JLabel("Alpha")

labelAlpha.setBounds(5,50,100,100) labelAlpha.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelAlpha) textAlpha = JTextField() textAlpha.editable = False textAlpha.setBounds(85,90,140,20) panel.add(textAlpha) labelPrime = JLabel("P") labelPrime.setBounds(5,75,100,100) labelPrime.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelPrime) textPrime = JTextField() textPrime.editable = False textPrime.setBounds(85,115,140,20) panel.add(textPrime) labelY = JLabel("Alpha^A") labelY.setBounds(5,100,100,100) labelY.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelY) textY = JTextField() textY.editable = False textY.setBounds(85,140,140,20) panel.add(textY) labelTime = JLabel("Time") labelTime.setBounds(5,200,100,100) labelTime.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelTime) textTime = JTextField() textTime.setBounds(85,240,140,20) textTime.editable = False panel.add(textTime)

gKey2 = JLabel("Private Key") gKey2.setBounds(35,130,100,100) gKey2.setFont(Font("Times New Roman",Font.BOLD,14)) panel.add(gKey2) labelA = JLabel("A") labelA.setBounds(5,155,100,100) labelA.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelA) textA = JTextField() textA.editable = False textA.setBounds(85,195,140,20) panel.add(textA) buttonEncrypt = JButton("Encryption", actionPerformed = openEnForm) buttonEncrypt.setBounds(250,55,100,30) panel.add(buttonEncrypt) buttonDecrypt = JButton("BSGS", actionPerformed = openBSGSForm) buttonDecrypt.setBounds(250,105,100,30) panel.add(buttonDecrypt) buttonBSGS = JButton("Decryption", actionPerformed = openDeForm) buttonBSGS.setBounds(250,155,100,30) panel.add(buttonBSGS) rot = ImageIcon("one_piece_anime_key_art.jpg") rotLabel = JLabel(rot) rotLabel.setBounds(0, 0, rot.getIconWidth(), rot.getIconHeight()) panel.add(rotLabel) #frame visiblity and close operation

if __name__ == "__main__": frame.setLocationRelativeTo(None) frame.visible = True frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE)

5. Encryption Form (EncryptionForm.py)

  from javax.swing import * from java.awt.event import * from java.lang import * from java.awt import * from random import randint import sys import zipfile import re import timeit import math #create frame frameEn = JFrame("Encryption Form", size=(350,300)) #random number b generator def random_number(prime): N = math.ceil(math.sqrt(prime)) l = randint(0, N) #print l return l #browse file function def browseFile(event): chooseFile = JFileChooser() #filter = FileNameExtensionFilter("c files", ["c"]) #chooseFile.addChoosableFileFilter(filter) ret = chooseFile.showDialog(frameEn, "Choose File") if ret == JFileChooser.APPROVE_OPTION: fileC = chooseFile.getSelectedFile() textFL.text = fileC.getCanonicalPath() def saveFile(): saveFile = JFileChooser() saveFile.showDialog(frameEn, "Save To") def readFile(file): if file[-4:] == ".txt": #filename = file.getCanonicalPath() f = open(file, "r") text = f.read() f.close() return text else: docx = zipfile.ZipFile(file) content = docx.read('word/document.xml') cleaned = re.sub('<(.|\n)*?>','',content) pesan =[] pesan = list(cleaned) pesan = pesan[2::] return pesan def encryptMessage(event): start = timeit.default_timer() message = list(readFile(textFL.text)) p = int(textPrime.text) alpha = int(textAlpha.text) y = int(textY.text) #conveting characters to ASCII converted_message = [] for i in message: converted_message.append(ord(i)) #encrypting converted message alpha_exp_b = [] malpha_exp_b = [] n = len(converted_message) b = []

  #bilangan acak b for i in range(0,n): b.append(random_number(p)) #list for alpha_exp_b for i in range(0,n): alpha_exp_b.append(pow(alpha,b[i],p)) print pow(alpha,b[i],p),"alphaB" #list for malpha_exp_b for i in range(0,n): malpha = pow(y,b[i]) malpha = malpha * converted_message[i] malpha_exp_b.append(pow(malpha,1,p)) print pow(malpha,1,p), "malphaB" #put file to encrypted.txt variable1 = alpha_exp_b variable2 = malpha_exp_b f = file("encrypted.txt",'w') for i in range(0, len(variable1)): f.write(str(variable1[i]) + " ") f.write(str(variable2[i]) + " ") f.close() end = timeit.default_timer() textTime.text = str((end - start) * 1000) JOptionPane.showMessageDialog(frameEn, "Enkripsi Berhasil!", "Info", JOptionPane.INFORMATION_MESSAGE) print alpha_exp_b print malpha_exp_b #Encryption Form panel = JPanel() panel.setLayout(None) #panel.setBackground(Color(66,66,66)) frameEn.getContentPane().add(panel) enc = JLabel("Encryption") enc.setBounds(35,0,100,100) enc.setFont(Font("Times New Roman",Font.BOLD,14)) panel.add(enc) inputPT = JLabel("Plaintext") inputPT.setBounds(5,25,100,100) inputPT.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(inputPT) textFL = JTextField() textFL.setBounds(85,65,140,20) panel.add(textFL) buttonBrowse = JButton("Browse", actionPerformed = browseFile ) buttonBrowse.setBounds(230,65,100,20) panel.add(buttonBrowse) labelPK = JLabel("Input Your Public Key") labelPK.setBounds(35,50,150,100) labelPK.setFont(Font("Times New Roman",Font.BOLD,14)) panel.add(labelPK) labelAlpha = JLabel("Alpha") labelAlpha.setBounds(5,75,100,100) labelAlpha.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelAlpha) textAlpha = JTextField() textAlpha.setBounds(85,115,140,20) panel.add(textAlpha) labelPrime = JLabel("P") labelPrime.setBounds(5,100,100,100) labelPrime.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelPrime) textPrime = JTextField() textPrime.setBounds(85,140,140,20)

panel.add(textPrime) labelY = JLabel("Alpha^A") labelY.setBounds(5,125,100,100) labelY.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelY) textY = JTextField() textY.setBounds(85,165,140,20) panel.add(textY) buttonEncrypt = JButton("Encrypt", actionPerformed = encryptMessage) buttonEncrypt.setBounds(230,200,100,20) panel.add(buttonEncrypt) labelTime = JLabel("Time") labelTime.setBounds(5,200,100,100) labelTime.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelTime) textTime = JTextField() textTime.setBounds(85,240,140,20) textTime.editable = False panel.add(textTime) rot = ImageIcon("one_piece_anime_key_art.jpg") rotLabel = JLabel(rot) rotLabel.setBounds(0, 0, rot.getIconWidth(), rot.getIconHeight()) panel.add(rotLabel) 6.

   Decryption Form (DecryptionForm.py)

  from javax.swing import * from java.awt.event import * from java.lang import * from java.awt import * import timeit from Decrypt_Result import *

  #create frame frameDe = JFrame("Decryption Form", size=(350,300)) #browse file location def browseFile(event): chooseFile = JFileChooser() ret = chooseFile.showDialog(frameDe, "Choose File") if ret == JFileChooser.APPROVE_OPTION: fileC = chooseFile.getSelectedFile() #text = readFile(fileC) textFL.text = fileC.getCanonicalPath() def readFile(file): #filename = file.getCanonicalPath() f = open(file, "r") text = f.read() f.close() return text def decryptMessage(event): start = timeit.default_timer() message = readFile(textFL.text) message = message.split(' ') message.pop() messageInt = [] for item in message: messageInt.append(int(item)) alpha_exp_b = [] malpha_exp_b = [] alpha_exp_b = messageInt[::2] malpha_exp_b = messageInt[1::2] print alpha_exp_b print malpha_exp_b p = int(textPrime.text) a = int(textA.text)

  #hitung p-1-a p_min_a = p - 1 - a n = len(alpha_exp_b) decrypt = [] decrypt_result = [] #find alpha exp b exp p min a for i in range(0,n): decrypt.append(pow(alpha_exp_b[i],p_min_a,p)) #find malpha exp b * decrypt mod p for i in range(0,n): times = malpha_exp_b[i] * decrypt[i] decrypt_result.append(pow(times,1,p)) print decrypt_result pesan = ''.join(chr(i) for i in decrypt_result) end = timeit.default_timer() textTime.text = str((end - start) * 1000) #print pesan frameDR.visible = True textArea.text = pesan #Decryption form panel = JPanel() panel.setLayout(None) #panel.setBackground(Color(66,66,66)) frameDe.getContentPane().add(panel) dec = JLabel("Decryption") dec.setBounds(35,0,100,100) dec.setFont(Font("Times New Roman",Font.BOLD,14)) panel.add(dec) inputCT = JLabel("Ciphertext") inputCT.setBounds(5,25,100,100) inputCT.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(inputCT) textFL = JTextField() textFL.setBounds(85,65,140,20) panel.add(textFL) buttonBrowse = JButton("Browse", actionPerformed = browseFile) buttonBrowse.setBounds(230,65,100,20) panel.add(buttonBrowse) labelPK = JLabel("Input Your Key") labelPK.setBounds(35,75,150,100) labelPK.setFont(Font("Times New Roman",Font.BOLD,14)) panel.add(labelPK) labelPrime = JLabel("P") labelPrime.setBounds(5,100,100,100) labelPrime.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelPrime) textPrime = JTextField() textPrime.setBounds(85,140,140,20) panel.add(textPrime) buttonDecrypt = JButton("Decrypt", actionPerformed = decryptMessage) buttonDecrypt.setBounds(230,150,100,20) panel.add(buttonDecrypt) labelA = JLabel("A") labelA.setBounds(5,125,100,100) labelA.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelA) textA = JTextField() textA.setBounds(85,165,140,20) panel.add(textA) labelTime = JLabel("Time")

labelTime.setBounds(5,200,100,100) labelTime.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelTime) textTime = JTextField() textTime.setBounds(85,240,140,20) textTime.editable = False panel.add(textTime) rot = ImageIcon("one_piece_anime_key_art.jpg") rotLabel = JLabel(rot) rotLabel.setBounds(0, 0, rot.getIconWidth(), rot.getIconHeight()) panel.add(rotLabel)

7. Baby-step Giant-step (BSGS.py)

  from javax.swing import * from java.awt.event import * from java.lang import * from java.awt import * import timeit import math #create frame frameBSGS = JFrame("Baby-step Giant-step Form", size=(350,300)) #baby step giant step algorithm def hack(event): start = timeit.default_timer() p = int(textPrime.text) alpha = int(textAlpha.text) y = int(textY.text) x = 0 #find inverse modulo def inverse_modulo(): n = 1 while x == 0: if (n * alpha) % p == 1: break else: n += 1 return n N = int(math.ceil(math.sqrt(p-1))) N = N + 1 #print N A = [] B = [] for j in range(0,N+1): value = pow(alpha,j,p) A.append(value) inv = inverse_modulo() for k in range(0,N+1): power1 = pow(inv,N*k,p) value = pow(y*power1,1,p) B.append(value) x1,x2 =0,0 for r in A: if r == value: x1 = A.index(r) x2 = N*k print x1,x2 break if x1 + x2 != 0: break end = timeit.default_timer() textTime.text = str((end - start) * 1000) A = x1 + x2

textA.text = str(A) print end - start JOptionPane.showMessageDialog(frameBSGS, "Hack Done!", "Info", JOptionPane.INFORMATION_MESSAGE) #Decryption panel = JPanel() panel.setLayout(None) frameBSGS.getContentPane().add(panel) labelPK = JLabel("Input Your Public Key") labelPK.setBounds(35,0,150,100) labelPK.setFont(Font("Times New Roman",Font.BOLD,14)) panel.add(labelPK) labelAlpha = JLabel("Alpha") labelAlpha.setBounds(5,25,100,100) labelAlpha.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelAlpha) textAlpha = JTextField() textAlpha.setBounds(85,65,140,20) panel.add(textAlpha) labelPrime = JLabel("P") labelPrime.setBounds(5,50,100,100) labelPrime.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelPrime) textPrime = JTextField() textPrime.setBounds(85,90,140,20) panel.add(textPrime) labelY = JLabel("Alpha^A") labelY.setBounds(5,75,100,100) labelY.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelY)

textY = JTextField() textY.setBounds(85,115,140,20) panel.add(textY) buttonDecrypt = JButton("Find", actionPerformed = hack) buttonDecrypt.setBounds(230,200,100,20) panel.add(buttonDecrypt) labelSK = JLabel("Private Key") labelSK.setBounds(35,100,150,100) labelSK.setFont(Font("Times New Roman",Font.BOLD,14)) panel.add(labelSK) labelA = JLabel("A") labelA.setBounds(5,125,100,100) labelA.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelA) textA = JTextField() textA.editable = False textA.setBounds(85,165,140,20) panel.add(textA) labelTime = JLabel("Time") labelTime.setBounds(5,200,100,100) labelTime.setFont(Font("Times New Roman",Font.BOLD,12)) panel.add(labelTime) textTime = JTextField() textTime.setBounds(85,240,140,20) textTime.editable = False panel.add(textTime) rot = ImageIcon("one_piece_anime_key_art.jpg") rotLabel = JLabel(rot) rotLabel.setBounds(0, 0, rot.getIconWidth(), rot.getIconHeight()) panel.add(rotLabel)

Dokumen baru

Download (19 Halaman)
Gratis

Dokumen yang terkait

BAB 2 TINJAUAN PUSTAKA 1.Sistem Saraf - Gambaran Psikologis dan Kognitif pada Pasien Gangguan Sistem Saraf di RSUP H. Adam Malik Medan
0
1
18
BAB 2 TINJAUAN PUSTAKA - Stres dan Kualitas Tidur pada Lansia di Kecamatan Porsea dapat diselesaikan dengan baik
1
1
22
Kurikulum Spektrum 2018 Kurikulum SMK Terbaru - Berkas Edukasi
0
0
70
BAB II TINJAUAN PUSTAKA 2.1. BRONKOSKOPI 2.1.1. SEJARAH BRONKOSKOPI - Perbandingan Kenyamanan Pasien Yang Dilakukan Bronkoskopi Serat Optik Lentur Dengan Anastesi Lokal Secara Spray dan Nebul di RSUP H. Adam Malik Medan
0
0
31
BAB I PENDAHULUAN 1.1 LATAR BELAKANG - Perbandingan Kenyamanan Pasien Yang Dilakukan Bronkoskopi Serat Optik Lentur Dengan Anastesi Lokal Secara Spray dan Nebul di RSUP H. Adam Malik Medan
0
0
7
BAB 2 TINJAUAN PUSTAKA 2.1. Kanker 2.1.1 Pengertian - Resiliensi dan Mekanisme Koping Orangtua Anak Penderita Kanker di RSUP H. Adam Malik Medan
0
0
18
SK Dirjen Pendis Nomor 5161 Tahun 2018 tentang Juknis Penilaian Hasil Belajar pada MI (Madrasah Ibtidaiyah) - Berkas Edukasi
1
2
81
SK Dirjen Pendis Nomor 5162 Tahun 2018 tentang Juknis Penilaian Hasil Belajar pada MTs (Madrasah Tsanawiyah) - Berkas Edukasi
1
0
88
SK Dirjen Pendis Nomor 3751 Tahun 2018 tentang Juknis Penilaian Hasil Belajar pada MA (Madrasah Aliyah) - Berkas Edukasi
0
3
86
BAB II GAMBARAN UMUM LOKASI PENELITIAN PRAKTIK KERJA LAPANGAN MANDIRI(PKLM) A. Sejarah Singkat Berdirinya Kantor Pelayanan Pajak Pratama Medan Polonia - Pelaksanaan Surat Teguran Dalam Upaya Untuk Meningkatkan Penerimaan Pajak Negara Di Kantor Pelayanan P
0
0
14
BAB I PENDAHULUAN A. Latar Belakang Praktik Kerja Lapangan Mandiri - Pelaksanaan Surat Teguran Dalam Upaya Untuk Meningkatkan Penerimaan Pajak Negara Di Kantor Pelayanan Pajak Pratama Medan Polonia
0
0
16
BAB II TINJAUAN PUSTAKA 2.1. Defenisi Kanker Payudara - Karakteristik Penderita Kanker Payudara Yang Dirawat Inap Di Rsu Dr. Pirngadi Medan Tahun 2011-2013
0
0
34
BAB II GAMBARAN UMUM MENGENAIASHIGARU - Fungsi Dan Tugas Ashigaru Dalam Situasi Perang Pada Zaman Kamakura Hingga Zaman Edo
0
0
14
BAB II TINJAUAN PUSTAKA 2.1. Landasan Teori 2.2 . Kinerja Perusahaan - Pengaruh Pertumbuhan Rasio Keuangan ( Current Ratio, Debt To Eqiuty Ratio , Total Asset Turn Over ) dan Ukuran Perusahaan Terhadap Kinerja Perusahaan pada Perusahaan Manufaktur yang Te
0
0
24
Buku BNPB Panduan Kesiapsiagaan Bencana untuk Keluarga - Berkas Edukasi
0
0
64
Show more