98 lines
2.7 KiB
Ruby
98 lines
2.7 KiB
Ruby
#!/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
|