Added
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
parent
0157e48d62
commit
e597885b3a
58
5-rsa/alice.rb
Executable file
58
5-rsa/alice.rb
Executable file
@ -0,0 +1,58 @@
|
|||||||
|
#!/usr/bin/ruby
|
||||||
|
|
||||||
|
#####################################
|
||||||
|
#
|
||||||
|
# Marcin Woźniak
|
||||||
|
# s434812
|
||||||
|
#
|
||||||
|
#####################################
|
||||||
|
|
||||||
|
require 'socket'
|
||||||
|
|
||||||
|
load 'modul1.rb'
|
||||||
|
|
||||||
|
sock = TCPSocket.new("localhost",3000)
|
||||||
|
puts sock.gets
|
||||||
|
|
||||||
|
# Generate public and priv key
|
||||||
|
while true
|
||||||
|
p = generatePrime(100)
|
||||||
|
q = generatePrime(100)
|
||||||
|
if primalityTest(p) && primalityTest(q)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
n = p * q
|
||||||
|
phi = (p-1)*(q-1)
|
||||||
|
|
||||||
|
while true
|
||||||
|
e = SecureRandom.random_number(0..phi)
|
||||||
|
if nwd(e,phi) == 1
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
d = reciprocal_Phi_p(e,phi)
|
||||||
|
|
||||||
|
pubKey = [n,e]
|
||||||
|
privKey = [n,d]
|
||||||
|
|
||||||
|
puts
|
||||||
|
puts "privKey " + privKey.inspect
|
||||||
|
puts "pubKey " + pubKey.inspect
|
||||||
|
puts
|
||||||
|
|
||||||
|
# Sending pubKey
|
||||||
|
sock.puts pubKey[0]
|
||||||
|
sock.puts pubKey[1]
|
||||||
|
|
||||||
|
# Getting cipher
|
||||||
|
cipher = sock.gets.gsub(/\n$/, '')
|
||||||
|
message = betterExponentiation(cipher.to_i,d.to_i,n.to_i)
|
||||||
|
|
||||||
|
puts "Cipher: " + cipher.inspect
|
||||||
|
puts "Message: " + message.inspect
|
||||||
|
|
||||||
|
# Close socket
|
||||||
|
sock.close
|
48
5-rsa/bob.rb
Executable file
48
5-rsa/bob.rb
Executable file
@ -0,0 +1,48 @@
|
|||||||
|
#!/usr/bin/ruby
|
||||||
|
#
|
||||||
|
######################################
|
||||||
|
#
|
||||||
|
# Marcin Woźniak
|
||||||
|
# s434812
|
||||||
|
#
|
||||||
|
#####################################
|
||||||
|
|
||||||
|
require 'socket'
|
||||||
|
|
||||||
|
load 'modul1.rb'
|
||||||
|
|
||||||
|
|
||||||
|
while true
|
||||||
|
|
||||||
|
# Reading a message
|
||||||
|
#message = File.read("message.txt")
|
||||||
|
message="823789137891789217389173981378913789137289"
|
||||||
|
|
||||||
|
# Turning on server
|
||||||
|
sock = TCPServer.new(3000)
|
||||||
|
client = sock.accept
|
||||||
|
|
||||||
|
#Greetings
|
||||||
|
client.puts "Hej Alice"
|
||||||
|
|
||||||
|
# Receiving public key
|
||||||
|
n = client.gets.gsub(/\n$/, '')
|
||||||
|
e = client.gets.gsub(/\n$/, '')
|
||||||
|
pubKeyAlice=[n,e]
|
||||||
|
|
||||||
|
puts
|
||||||
|
puts "pubKey Alice: " + pubKeyAlice.inspect
|
||||||
|
puts
|
||||||
|
|
||||||
|
# Solving a cipher
|
||||||
|
cipher = betterExponentiation(message.to_i,e.to_i,n.to_i)
|
||||||
|
|
||||||
|
puts "Cipher: " + cipher.inspect
|
||||||
|
puts "Message " + message.inspect
|
||||||
|
|
||||||
|
# Sending cipher to Alice
|
||||||
|
client.puts cipher
|
||||||
|
|
||||||
|
# Close socket
|
||||||
|
sock.close
|
||||||
|
end
|
240
5-rsa/modul1.rb
Executable file
240
5-rsa/modul1.rb
Executable file
@ -0,0 +1,240 @@
|
|||||||
|
#!/usr/bin/ruby
|
||||||
|
|
||||||
|
#####################################
|
||||||
|
#
|
||||||
|
# Marcin Woźniak
|
||||||
|
# s434812
|
||||||
|
#
|
||||||
|
#####################################
|
||||||
|
|
||||||
|
require 'openssl'
|
||||||
|
require 'securerandom'
|
||||||
|
require 'prime'
|
||||||
|
require 'thread'
|
||||||
|
|
||||||
|
def nwd(a, b)
|
||||||
|
b == 0 ? a : nwd(b, a.modulo(b))
|
||||||
|
end
|
||||||
|
|
||||||
|
def extended_euklides(a, b)
|
||||||
|
return 1, 0 if b == 0
|
||||||
|
|
||||||
|
q, r = a.divmod b
|
||||||
|
s, t = extended_euklides(b, r)
|
||||||
|
|
||||||
|
return t, s - q * t
|
||||||
|
end
|
||||||
|
|
||||||
|
# Zad. 1.1
|
||||||
|
def random_gen_Zn(n,k)
|
||||||
|
if 2**(k-1) < n && k > 0
|
||||||
|
if k == 1
|
||||||
|
min = 0
|
||||||
|
max = 1
|
||||||
|
else
|
||||||
|
min = 2**(k-1)
|
||||||
|
max = (2**k)-1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
while true do
|
||||||
|
r = SecureRandom.random_number(min..max)
|
||||||
|
if r < n
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return r
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
# Zad. 1.2
|
||||||
|
def reciprocal_Phi_p(n,p)
|
||||||
|
u = extended_euklides(n,p)[0]
|
||||||
|
v = extended_euklides(n,p)[1]
|
||||||
|
|
||||||
|
if u * n % p == 1
|
||||||
|
return u
|
||||||
|
else
|
||||||
|
return v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Zad. 1.3
|
||||||
|
def betterExponentiation(x,k,n)
|
||||||
|
if n == 0
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
b = k.to_s(2).reverse
|
||||||
|
l = b.count "[0-1]"
|
||||||
|
y = 1
|
||||||
|
i = l - 1
|
||||||
|
|
||||||
|
while i >= 0
|
||||||
|
y = y**2 % n
|
||||||
|
if b[i]=="1"
|
||||||
|
y = y * x % n
|
||||||
|
end
|
||||||
|
i = i - 1
|
||||||
|
end
|
||||||
|
return y
|
||||||
|
end
|
||||||
|
|
||||||
|
# Zad. 1.4
|
||||||
|
def remSqEuler(a,p)
|
||||||
|
ans = betterExponentiation(a,(p-1)/2,p)
|
||||||
|
|
||||||
|
if ans == 1 && Prime.prime?(p)
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Zad. 1.5
|
||||||
|
def squareRootFp(p,b)
|
||||||
|
if p % 4 == 3 && remSqEuler(p,b) == true
|
||||||
|
a = betterExponentiation(b, (p+1)/4, p)
|
||||||
|
return a
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Zad. 1.6
|
||||||
|
def primalityTest(n)
|
||||||
|
if n == 1
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
if n == 2 || n == 3
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
counter = 10
|
||||||
|
while (counter != 0) do
|
||||||
|
b = SecureRandom.random_number(2..n-2) # Tez dziala n-1
|
||||||
|
if betterExponentiation(b,n-1,n) != 1
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
counter = counter - 1
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def randomNumber(k)
|
||||||
|
randomNumberArray=[]
|
||||||
|
randomNumberArray << 1
|
||||||
|
k= k - 1
|
||||||
|
|
||||||
|
while (k !=0 ) do
|
||||||
|
j = SecureRandom.random_number(2)
|
||||||
|
randomNumberArray << j
|
||||||
|
k = k - 1
|
||||||
|
end
|
||||||
|
return randomNumberArray.join.to_i(2)
|
||||||
|
end
|
||||||
|
|
||||||
|
def specyficPrimaryNumber
|
||||||
|
while true do
|
||||||
|
q = SecureRandom.random_number(2 ** 256)
|
||||||
|
p = 2 * q + 1
|
||||||
|
if primalityTest(q) && primalityTest(p)
|
||||||
|
return p,q
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def generator(p,q)
|
||||||
|
while true
|
||||||
|
g = SecureRandom.random_number(2..p-2)
|
||||||
|
if betterExponentiation(g,q,p) == 1
|
||||||
|
next
|
||||||
|
else
|
||||||
|
return g
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def generatePrime(n)
|
||||||
|
return `openssl prime -generate -bits '#{n}'`.gsub(/\n$/, '').to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
###################################################################################
|
||||||
|
|
||||||
|
# Zadanie.1 Losowy element z zbioru Z_n
|
||||||
|
#
|
||||||
|
# Uzycie funkcji:
|
||||||
|
# random_gen_Zn(n,k)
|
||||||
|
#
|
||||||
|
# Gdzie n - grupa mod
|
||||||
|
# k - ilosc bitow
|
||||||
|
|
||||||
|
#puts "Zadanie 1: " + random_gen_Zn(2817960879631397637428637785383222308241674912977296371078328827783823173932945686560271709717223685413005001487628305095704070793958971415087255523073935114518202433817141315589046697665767753020703330561718642810157214393228915238808369985314803605127547366769525321238034444131545038866079220135011303234308583293055538438855440051375449844277218217712896675378963068720345515473434111699867542985885331741594726201346190830240692715577811297545617592986841862266976883958728343753502720654957572591625318922387371348775299617016576604164586635463962864505536324347369114225605202600072081219902252264258236028218613372215876496394341867072862629022156295534655310094481486891656868251869749451857161662890356600101860594118885136794555465318782343870373413707643678756555809662335374658562880045543430140169913540153961566294704894163494226738977413711366121112146042996377423578488354012607979075727403258666348434920910981957617905109649522084109820427893425361930683246183514216962034556016051184044970184676662827978420213281480510069379347007543973139554178175690773315927469361215139519543395792659825942864347234234285907203635728345512501417615714291999748745375110198782386769755218587158953598215023147871490893052124457996091330393194346889440433647348345868290410045628941650640315817691074244257070087501412656465257388504574920756736653658826614297081956072241319983753399595743495837950519045870810249068878008938965513649172515852388457433200750100902751828486806189811961720524504976040165319005498382580099718522287255514024665802127411452771343820848551192190973237603226514162458911052832006635099614443220118029822801995173084006448848714642581145597450095993844936566471047767974285413266855493150984091512278787539020882065620089396507786047199321023827465489494495513119543424285384803025749486259267450477917340669954258887755716092814959537213397128776047910200001947756249766479670986137076981224642743069725401099384031028670810757683522821674118926373517718330706064178699035286815124318059529948663730575377063300582239742709428517028624628630255640464009691179606104444672232047869709734836046571505598681429286372976726889778746623725623485244272247797117303046464791441413956650048631706362550020890628552166104678993837549091779514727408814458173452235187339390489070470959043964826221641981413639067723526075949807992319555177364587831440901887979371647118775640122112691496917328672989185787786333743943571201682455849788768447941052589112093217146238196393058840427167875379895033862665036580869700901671149307822867600084775797450043528335040523181300939047560092173656437964805986878846909755616898664300908149121103065456346895895553346803762232995496756924185713587688989499627282573083392504068537865838813511676470822568313458216527495376958238195629276140142912559084213140117194512209752564237149304569698487234268004667685663277598956964116311211220970915272172182795014191615102024324085452311963348372375839824417067170842114940446271152850552864783993722225100122068507428533164772669151799555291529447719732682260868021763685052338407930046892500473287361324180925326385131283404240275852188687583358717018916949107259963059221294732376067807306008309012964676038322426438467806570485299377626514669181680963962016304313021681669696580753784295259640169007074506637844332833804045109981978476343942218065708022217566222881348496310403404164467438352649885637702780969376650799749541038736604262724814821757258372470336571211357167468680200965241152364670892328226187787108777323221988154289834826302745446444518424440700033948649807665278475079826664974213162552336459719975478725157313669922561440432360473265250572606571269284353534676478375363478936179040075297456773814344907213125333476476655366455733030460908712160208004620689954788191597043073132610377893018543064926955781952650214504633097216561549411315119718460509357878230720821699669794678584993658379092808863297233433557739623968183417003317301945898487820593437670555891465841469699644209269444311251317668652989906375182749637967733151694853436985266479700749763565024494627271013468757521359567838089899510388135209838269214753262972848939363295323913595985611694315406247934779335196977854974497594863447427077079600386381593134665326520126544264832044936966738225526176229232138173609626231926255343974851041449136902209592667970872917688005523210617825501923851244940494708102661449185546240415803793231334092660117374732910436571816210806786100000371385219128407888245069151381708001509375,15000).inspect
|
||||||
|
|
||||||
|
###################################################################################
|
||||||
|
|
||||||
|
# Zadanie.2 Odwrotnosc w grupie Phi(n)
|
||||||
|
#
|
||||||
|
# Uzycie funkcji:
|
||||||
|
# reciprocal_Phi_p(n,p)
|
||||||
|
#
|
||||||
|
# Gdzie p - element w grupie phi
|
||||||
|
# n - liczba nalezaca do N
|
||||||
|
|
||||||
|
#puts "Zadanie 2: " + reciprocal_Phi_p(10,13).inspect
|
||||||
|
#puts "Zadanie 2: " + reciprocal_Phi_p(76638723687263876287368268368726378623873687326872634868374687236487623874687648634863847623846834687643,100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000961 ).inspect
|
||||||
|
|
||||||
|
###################################################################################
|
||||||
|
|
||||||
|
# Zadanie.3 Efektywne potegowanie.
|
||||||
|
#
|
||||||
|
# Uzycie funkcji:
|
||||||
|
# betterExponentiation(x,k,n)
|
||||||
|
#
|
||||||
|
# Gdzie obliczna jest wartosc x^k mod n
|
||||||
|
|
||||||
|
#puts "Zadanie 3: " + betterExponentiation(823789137891789217389173981378913789137289,565490994747691690475378499398697660773449981085993539792067,1399661509700116309409184866497198118594638278433610469383879).inspect
|
||||||
|
#puts "Zadanie 3: " + betterExponentiation(8,2,30).inspect
|
||||||
|
#puts "Zadanie 3: " + betterExponentiation(76638723687263876287368268368726378623873687326872634868374687236487623874687648634863847623846834687643, 76382637812836812638612836812638612376182263812623861283618723681263861238612386, 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000961).inspect
|
||||||
|
|
||||||
|
|
||||||
|
###################################################################################
|
||||||
|
|
||||||
|
# Zadanie.4 Sprawdzenie czy element a jest reszta kwadratowa w Z_p
|
||||||
|
#
|
||||||
|
# Uzycie funkcji:
|
||||||
|
# remSqEuler(a,p)
|
||||||
|
#
|
||||||
|
# Gdzie a - element
|
||||||
|
# p - liczba pierwsza
|
||||||
|
|
||||||
|
#puts "Zadanie 4: " + remSqEuler(4,15485863).inspect
|
||||||
|
#puts "Zadanie 4: " + remSqEuler(3,13).inspect
|
||||||
|
#puts "Zadanie 4: " + remSqEuler(5,13).inspect
|
||||||
|
|
||||||
|
###################################################################################
|
||||||
|
|
||||||
|
# Zadanie.5 Obliczanie pierwiastka kwadratowego w ciele F_p*.
|
||||||
|
#
|
||||||
|
# Uzycie funkcji
|
||||||
|
# squareRootFp(p,b)
|
||||||
|
#
|
||||||
|
# Gdzie p - liczba pierwsza (modulo)
|
||||||
|
# b - reszta kwadratowa
|
||||||
|
|
||||||
|
#puts "Zadanie 5: " + squareRootFp(15485863,2).inspect
|
||||||
|
|
||||||
|
###################################################################################
|
||||||
|
|
||||||
|
# Zadanie 6. Test pierwszości.
|
||||||
|
#
|
||||||
|
# Uzycie funkcji:
|
||||||
|
# primalityTest(n)
|
||||||
|
#
|
||||||
|
# Gdzie n - liczba wejsciowa
|
||||||
|
|
||||||
|
#puts "Zadanie 6: " + primalityTest(13).inspect
|
||||||
|
#puts "Zadanie 6: " + primalityTest(100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000961).inspect
|
||||||
|
|
||||||
|
###################################################################################
|
Loading…
Reference in New Issue
Block a user