LISTING PROGRAM 1.
Bahasa Python
1.1. Modulegate.py
import sys sys.path.appendC:\skrip
import pythonA def gerbangfungsi:
iffungsi == bangkitKunci: from pythonA import bangkitkanKunci
p, q, n = bangkitkanKunci printp, q, n
eliffungsi == cekKunci: from pythonA import cekKunci
hasil = cekKuncib,c printhasil
eliffungsi == enkripsi: from pythonA import programEnkripsi
C = programEnkripsid, b for i in rangelenC:
printC[i], end =
eliffungsi == dek: from pythonA import decryptCiphertext
cetak = decryptCiphertextd, b, c for i in rangelencetak:
printcetak[i], end = eliffungsi == dek2:
from pythonA import decryptCiphertext2 cetak = decryptCiphertext2d, b, c
for i in rangelencetak: printcetak[i], end =
eliffungsi == cari: from pythonA import cariHitamDariPath
hasil = cariHitamDariPathd, b for i in rangelenhasil:
printhasil[i], end = ifi lenhasil-1:
print,, end = eliffungsi == embed:
from pythonA import jalankanSisip stroutput = jalankanSisipd, e, b
printstroutput eliffungsi == ekstrak:
from pythonA import jalankanEkstrak
stroutput = jalankanEkstrakd printstroutput
global a a = strsys.argv[1]
global b b = intsys.argv[2]
global c c = intsys.argv[3]
global d d = strsys.argv[4]
Universitas Sumatera Utara
global e e = strsys.argv[5]
gerbanga
1.2. ModulepythonA.py
import random, time class Timerobject:
timer starts with class initialisation def __init__self:
self.t1= time.time self.t2= time.time
def getElapsedlTimeself: gets total elapsed from class initialsation
self.delta=time.time-self.t1 return {0:.3f}.formatself.delta
def getTimeDifferenceself: 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}.formatself.delta def differentRandomn:
x = [] for i in range0, lenstrn:
a = random.randint2,n-1 while a in x:
a = random.randint2,n-1 x.appenda
return x def toBinx:
return {0:b}.formatx def ntobinn:
return toBinn-1 def modExpSAMx, y, z:
b = toBiny t = lenb
result = 1 for i in range0, t:
result = result result z ifb[i] == 1: result = result x z
return result
Universitas Sumatera Utara
def moduloEksacak, y, bil: z = 1
for j in range 1, y + 1: z = acak z bil
return z def fermatPrimex:
y = x - 1 panjang digit untuk pengetesan
iterasi = lenstrx 2 ifiterasi 3:
iterasi = 3 for i in range 0, iterasi:
acak = random.randint1, y ifmoduloEksacak, y, x = 1:
return False return True
def isPrimex: lowPrimes =
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73 ,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,15
7,163,167,173,179,181,191,193,197,199,211,223,227,229,233,2 39,241,251,257,263,269,271,277,281,283,293,307,311,313,317,
331,337,347,349,353,359,367,373,379,383,389,397,401,409,419 ,421,431,433,439,443,449,457,461,463,467,479,487,491,499,50
3,509,521,523,541,547,557,563,569,571,577,587,593,599,601,6 07,613,617,619,631,641,643,647,653,659,661,673,677,683,691,
701,709,719,727,733,739,743,751,757,761,769,773,787,797,809 ,811,821,823,827,829,839,853,857,859,863,877,881,883,887,90
7,911,919,929,937,941,947,953,967,971,977,983,991,997] if x in lowPrimes:
return True elif x 1 = 0:
return fermatPrimex return False
def bangkitkanKunci: syarat_tdk_terpenuhi = True
while syarat_tdk_terpenuhi: p = random.randint3,10000
if p 4 == 3 and isPrimep: syarat_tdk_terpenuhi = False
syarat_tdk_terpenuhi = True while syarat_tdk_terpenuhi:
q = random.randint3,10000 if q = p and q 4 == 3 and isPrimeq:
syarat_tdk_terpenuhi = False n = p q
return p, q, n def cekKuncikey1, key2:
if key1 4 == 3 and isPrimekey1 and key2 4 == 3 and isPrimekey2 and key1 = key2:
return T else:
return F
Universitas Sumatera Utara
def enkripsiP, n: if P n:
C = modExpSAMP,2,n return C
return False def encryptAllP, n:
C = [] for i in range0, lenP:
C.appendenkripsiordP[i],n if C[i] == False:
break; return C
def programEnkripsiplaintext, kunciPublik: measure=Timer
P = strplaintext n = intkunciPublik
C = encryptAllP, n C.appendmeasure.getElapsedlTime
return C def extEuclida,b:
A, B, x, lastx, y, lasty = a, b, 1, 0, 0, 1 while B 0:
hasilBagi = AB sisaBagi = A - hasilBagi B
A, B = B, sisaBagi S = x - hasilBagi lastx
x, lastx = lastx, S T = y - hasilBagi lasty
y, lasty = lasty, T return A, x, y
def CRTmp, mq, p, q: n = p q
M1 = n p M2 = n q
gcd, y1, y2 = extEuclidM1,M2 return mpM1y1, mqM2y2, n
def autoDekripsiC, p, q: mp = modExpSAMC, p+14, p
mq = modExpSAMC, q+14, q x, y, n = CRTmp,mq,p,q
P1 = x + y n P2 = x - y n
P3 = -x + y n P4 = -x - y n
return minP1, P2, P3, P4 def autoDekripsirabinC, p, q:
mp = modExpSAMC, p+14, p mq = modExpSAMC, q+14, q
x, y, n = CRTmp,mq,p,q
Universitas Sumatera Utara
P1 = x + y n P2 = x - y n
P3 = -x + y n P4 = -x - y n
return minP1, P2, P3, P4, P1, P2, P3, P4 def convertListStringCToIntC:
hasil = [] c = C.split,
for i in c: hasil.appendinti
return hasil def decryptCiphertext2C, p, q:
measure=Timer C = convertListStringCToIntC
P = [] for i in range0, lenC:
minP = autoDekripsiC[i], p , q P.appendchrminP
stringP = .joinP et = measure.getElapsedlTime
cetak = [] cetak.appendstringP
cetak.appendet temp = tulisHasilDekripsistringP
return cetak def decryptCiphertextC, p, q:
measure=Timer C = listC
ordo = [] for i in rangelenC:
ordo.appendordC[i] C = ordo
P = [] for i in range0, lenC:
minP = autoDekripsiC[i], p , q P.appendchrminP
stringP = .joinP et = measure.getElapsedlTime
cetak = [] cetak.appendstringP
cetak.appendet temp = tulisHasilDekripsistringP
return cetak class Convert:
def toLIself, string: temp = string.split,
temp2 = [] for e in temp:
temp2.appendinte return temp2
def toSIself, L: temp =
Universitas Sumatera Utara
for i in rangelenL: temp += strL[i]
ifi lenL-1: temp += ,
return temp def toDecself, P:
temp = [] for i in rangelenP:
temp.appendordP[i] return temp
def toASCIIself, string: string = self.toLIstring
temp = for e in string:
temp += chre return temp
hasil = isPrime100 printhasil
2. Bahasa C
2.1. Fungsi Pembangkit Kunci pada FormPembangkitKunci.cs