Module4 - has been ended 1,2,3,4?
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
parent
7fd51c072d
commit
cbd508726c
BIN
lab5_kryptoalgo 2.pdf
Normal file
BIN
lab5_kryptoalgo 2.pdf
Normal file
Binary file not shown.
69
module.rb
69
module.rb
@ -565,7 +565,16 @@ def algorytmDeSzyfrowania(x,y,u)
|
|||||||
return m
|
return m
|
||||||
end
|
end
|
||||||
|
|
||||||
# Module 4
|
########################### Module 4 ############################################
|
||||||
|
# Podstawowe operacje na Galois Field GF(2^8)
|
||||||
|
# Źródła:
|
||||||
|
# * https://people.scs.carleton.ca/~maheshwa/courses/4109/Seminar11/The_Advanced_Encryption_Standard_AES_.pdf
|
||||||
|
# * https://cs465.internet.byu.edu/static/lectures/w19/AES.pdf
|
||||||
|
# * https://en.wikipedia.org/wiki/Finite_field_arithmetic
|
||||||
|
# * https://swarm.cs.pub.ro/~mbarbulescu/cripto/Understanding%20Cryptography%20by%20Christof%20Paar%20.pdf
|
||||||
|
# * http://www.cs.man.ac.uk/~banach/COMP61411.Info/CourseSlides/Wk2.2.FinField.pdf
|
||||||
|
#################################################################################
|
||||||
|
|
||||||
#################################################################################
|
#################################################################################
|
||||||
# Zadanie 1
|
# Zadanie 1
|
||||||
# Funkcja suma(a,b) wykorzystujac liczby hex
|
# Funkcja suma(a,b) wykorzystujac liczby hex
|
||||||
@ -573,12 +582,7 @@ end
|
|||||||
def suma(a,b)
|
def suma(a,b)
|
||||||
binA = a.to_i(16).to_s(2)
|
binA = a.to_i(16).to_s(2)
|
||||||
binB = b.to_i(16).to_s(2)
|
binB = b.to_i(16).to_s(2)
|
||||||
|
|
||||||
if (a =~ /[^01]/).nil? && (b =~ /[^01]/).nil?
|
|
||||||
return (a.to_i(2) ^ b.to_i(2)).to_s(2)
|
|
||||||
else
|
|
||||||
return (binA.to_i(2) ^ binB.to_i(2)).to_s(16)
|
return (binA.to_i(2) ^ binB.to_i(2)).to_s(16)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
#################################################################################
|
#################################################################################
|
||||||
@ -588,11 +592,13 @@ end
|
|||||||
def xtime(a)
|
def xtime(a)
|
||||||
binA = a.to_i(16).to_s(2)
|
binA = a.to_i(16).to_s(2)
|
||||||
const = "1B".to_i(16).to_s(2)
|
const = "1B".to_i(16).to_s(2)
|
||||||
|
|
||||||
dl = binA.length
|
dl = binA.length
|
||||||
while dl != 8
|
while dl != 8
|
||||||
binA = "0" + binA
|
binA = "0" + binA
|
||||||
dl = dl + 1
|
dl = dl + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
if binA[0].to_i == 1
|
if binA[0].to_i == 1
|
||||||
binA[0] = ''
|
binA[0] = ''
|
||||||
return ((binA.to_i(2) << 1) ^ const.to_i(2)).to_s(16)
|
return ((binA.to_i(2) << 1) ^ const.to_i(2)).to_s(16)
|
||||||
@ -604,16 +610,12 @@ end
|
|||||||
#################################################################################
|
#################################################################################
|
||||||
# Zadanie 3
|
# Zadanie 3
|
||||||
# Funkcja iloczyn(a,b) wykorzystujac liczby hex
|
# Funkcja iloczyn(a,b) wykorzystujac liczby hex
|
||||||
|
# {53} • {CA} = {01}
|
||||||
|
# {53} • {13} = {fe}
|
||||||
#################################################################################
|
#################################################################################
|
||||||
def iloczyn(a,b)
|
def iloczyn(a,b)
|
||||||
solve = "0"
|
solve = "0"
|
||||||
|
|
||||||
binA = a.to_i(16).to_s(2)
|
binA = a.to_i(16).to_s(2)
|
||||||
dl = binA.length
|
|
||||||
while dl != 8
|
|
||||||
binA = "0" + binA
|
|
||||||
dl = dl + 1
|
|
||||||
end
|
|
||||||
|
|
||||||
len = binA.length - 1
|
len = binA.length - 1
|
||||||
binA.split('').each { |a|
|
binA.split('').each { |a|
|
||||||
@ -628,6 +630,47 @@ def iloczyn(a,b)
|
|||||||
end
|
end
|
||||||
len = len - 1
|
len = len - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return solve
|
return solve
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#################################################################################
|
||||||
|
# Zadanie 4
|
||||||
|
# Funkcja odwrotnosc(a,b) wykorzystujac liczby hex
|
||||||
|
#################################################################################
|
||||||
|
def extended_euklidesHex(a, b)
|
||||||
|
aDec = a.to_i(16)
|
||||||
|
bDec = b.to_i(16)
|
||||||
|
|
||||||
|
if bDec == 0
|
||||||
|
return a
|
||||||
|
else
|
||||||
|
return extended_euklidesHex(bDec.to_s(16), (aDec % bDec).to_s(16))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def bpd(ax,fx)
|
||||||
|
a = ax.to_i(16).to_s(2)
|
||||||
|
f = fx.to_i(16).to_s(2)
|
||||||
|
r = 1
|
||||||
|
q = 1
|
||||||
|
|
||||||
|
lenA = a.length
|
||||||
|
lenF = f.length
|
||||||
|
|
||||||
|
puts "#{a} #{f}"
|
||||||
|
puts "#{lenF} #{lenA}"
|
||||||
|
while (lenF >= lenA)
|
||||||
|
puts "#{lenF} #{lenA}"
|
||||||
|
a = (a.to_i(2) << (lenF - lenA)).to_s(2)
|
||||||
|
r = suma(a,fx)
|
||||||
|
lenR = r.to_i(16).to_s(2).length
|
||||||
|
if (lenR >= lenA)
|
||||||
|
q = (q << (lenF - lenR)) + 1
|
||||||
|
else
|
||||||
|
q = (q << (lenF - lenA))
|
||||||
|
end
|
||||||
|
f = r
|
||||||
|
return r,q
|
||||||
|
end
|
||||||
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user