Works module4
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
parent
cbd508726c
commit
8db99192fb
97
10-lab/modul4.rb
Normal file
97
10-lab/modul4.rb
Normal file
@ -0,0 +1,97 @@
|
||||
#!/usr/bin/ruby
|
||||
# coding: utf-8
|
||||
|
||||
##################################################################################
|
||||
#
|
||||
# Marcin Woźniak
|
||||
# s434812
|
||||
#
|
||||
##################################################################################
|
||||
|
||||
########################### 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
|
||||
#################################################################################
|
||||
def suma(a,b)
|
||||
binA = a.to_i(16).to_s(2)
|
||||
binB = b.to_i(16).to_s(2)
|
||||
return (binA.to_i(2) ^ binB.to_i(2)).to_s(16)
|
||||
end
|
||||
|
||||
#################################################################################
|
||||
# Zadanie 2
|
||||
# Funkcja xtime(a) wykorzystujac liczby hex
|
||||
#################################################################################
|
||||
def xtime(a)
|
||||
binA = a.to_i(16).to_s(2)
|
||||
const = "1B"
|
||||
|
||||
dl = binA.length
|
||||
while dl != 8
|
||||
binA = "0" + binA
|
||||
dl = dl + 1
|
||||
end
|
||||
|
||||
if binA[0].to_i == 1
|
||||
binA[0] = ''
|
||||
return suma((binA.to_i(2) << 1 ).to_s(16), const)
|
||||
elsif binA[0].to_i == 0
|
||||
return (binA.to_i(2) << 1 ).to_s(16)
|
||||
end
|
||||
end
|
||||
|
||||
#################################################################################
|
||||
# Zadanie 3
|
||||
# Funkcja iloczyn(a,b) wykorzystujac liczby hex
|
||||
# {53} • {CA} = {01}
|
||||
# {57} • {13} = {fe}
|
||||
#################################################################################
|
||||
def iloczyn(a,b)
|
||||
solve = "0"
|
||||
binA = a.to_i(16).to_s(2)
|
||||
|
||||
len = binA.length - 1
|
||||
binA.split('').each { |a|
|
||||
if a == "1"
|
||||
tmp = b
|
||||
counter = len
|
||||
while counter != 0
|
||||
tmp = xtime(tmp)
|
||||
counter = counter - 1
|
||||
end
|
||||
solve = suma(tmp.to_i(16).to_s(16), solve)
|
||||
end
|
||||
len = len - 1
|
||||
}
|
||||
return solve
|
||||
end
|
||||
|
||||
#################################################################################
|
||||
# Zadanie 4
|
||||
# Funkcja odwrotnosc(a) wykorzystujac liczby hex
|
||||
#################################################################################
|
||||
def odwrotnosc(a)
|
||||
b = a
|
||||
i = 13
|
||||
const = 1
|
||||
while i > 0
|
||||
if i.to_s(2).to_i & const.to_s(2).to_i == 1
|
||||
tmp = b
|
||||
else
|
||||
tmp = a
|
||||
end
|
||||
b = iloczyn(b,tmp)
|
||||
i = i - 1
|
||||
end
|
||||
return b
|
||||
end
|
54
module.rb
54
module.rb
@ -591,7 +591,7 @@ end
|
||||
#################################################################################
|
||||
def xtime(a)
|
||||
binA = a.to_i(16).to_s(2)
|
||||
const = "1B".to_i(16).to_s(2)
|
||||
const = "1B"
|
||||
|
||||
dl = binA.length
|
||||
while dl != 8
|
||||
@ -601,7 +601,7 @@ def xtime(a)
|
||||
|
||||
if binA[0].to_i == 1
|
||||
binA[0] = ''
|
||||
return ((binA.to_i(2) << 1) ^ const.to_i(2)).to_s(16)
|
||||
return suma((binA.to_i(2) << 1 ).to_s(16), const)
|
||||
elsif binA[0].to_i == 0
|
||||
return (binA.to_i(2) << 1 ).to_s(16)
|
||||
end
|
||||
@ -611,7 +611,7 @@ end
|
||||
# Zadanie 3
|
||||
# Funkcja iloczyn(a,b) wykorzystujac liczby hex
|
||||
# {53} • {CA} = {01}
|
||||
# {53} • {13} = {fe}
|
||||
# {57} • {13} = {fe}
|
||||
#################################################################################
|
||||
def iloczyn(a,b)
|
||||
solve = "0"
|
||||
@ -635,42 +635,20 @@ end
|
||||
|
||||
#################################################################################
|
||||
# Zadanie 4
|
||||
# Funkcja odwrotnosc(a,b) wykorzystujac liczby hex
|
||||
# Funkcja odwrotnosc(a) wykorzystujac liczby hex
|
||||
#################################################################################
|
||||
def extended_euklidesHex(a, b)
|
||||
aDec = a.to_i(16)
|
||||
bDec = b.to_i(16)
|
||||
|
||||
if bDec == 0
|
||||
return a
|
||||
def odwrotnosc(a)
|
||||
b = a
|
||||
i = 13
|
||||
const = 1
|
||||
while i > 0
|
||||
if i.to_s(2).to_i & const.to_s(2).to_i == 1
|
||||
tmp = b
|
||||
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
|
||||
tmp = a
|
||||
end
|
||||
b = iloczyn(b,tmp)
|
||||
i = i - 1
|
||||
end
|
||||
return b
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user