diff --git a/lab5_kryptoalgo 2.pdf b/lab5_kryptoalgo 2.pdf new file mode 100644 index 0000000..f3541b6 Binary files /dev/null and b/lab5_kryptoalgo 2.pdf differ diff --git a/module.rb b/module.rb index ffa2895..d608d7d 100755 --- a/module.rb +++ b/module.rb @@ -565,7 +565,16 @@ def algorytmDeSzyfrowania(x,y,u) return m 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 # Funkcja suma(a,b) wykorzystujac liczby hex @@ -573,12 +582,7 @@ end def suma(a,b) binA = a.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) - end + return (binA.to_i(2) ^ binB.to_i(2)).to_s(16) end ################################################################################# @@ -588,11 +592,13 @@ end def xtime(a) binA = a.to_i(16).to_s(2) const = "1B".to_i(16).to_s(2) + dl = binA.length while dl != 8 binA = "0" + binA dl = dl + 1 end + if binA[0].to_i == 1 binA[0] = '' return ((binA.to_i(2) << 1) ^ const.to_i(2)).to_s(16) @@ -604,16 +610,12 @@ end ################################################################################# # Zadanie 3 # Funkcja iloczyn(a,b) wykorzystujac liczby hex +# {53} • {CA} = {01} +# {53} • {13} = {fe} ################################################################################# def iloczyn(a,b) solve = "0" - 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 binA.split('').each { |a| @@ -628,6 +630,47 @@ def iloczyn(a,b) end len = len - 1 } - return solve 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