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

Gratis

0
1
19
11 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 &gt;= 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 &lt;= n: if pow(n,1,i) : i += 1 else: n //= i factors.append(i) print i if n &gt; 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)

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('&lt;(.|\n)*?&gt;','',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)