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
|
52
module.rb
52
module.rb
@ -591,7 +591,7 @@ 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"
|
||||||
|
|
||||||
dl = binA.length
|
dl = binA.length
|
||||||
while dl != 8
|
while dl != 8
|
||||||
@ -601,7 +601,7 @@ def xtime(a)
|
|||||||
|
|
||||||
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 suma((binA.to_i(2) << 1 ).to_s(16), const)
|
||||||
elsif binA[0].to_i == 0
|
elsif binA[0].to_i == 0
|
||||||
return (binA.to_i(2) << 1 ).to_s(16)
|
return (binA.to_i(2) << 1 ).to_s(16)
|
||||||
end
|
end
|
||||||
@ -611,7 +611,7 @@ end
|
|||||||
# Zadanie 3
|
# Zadanie 3
|
||||||
# Funkcja iloczyn(a,b) wykorzystujac liczby hex
|
# Funkcja iloczyn(a,b) wykorzystujac liczby hex
|
||||||
# {53} • {CA} = {01}
|
# {53} • {CA} = {01}
|
||||||
# {53} • {13} = {fe}
|
# {57} • {13} = {fe}
|
||||||
#################################################################################
|
#################################################################################
|
||||||
def iloczyn(a,b)
|
def iloczyn(a,b)
|
||||||
solve = "0"
|
solve = "0"
|
||||||
@ -635,42 +635,20 @@ end
|
|||||||
|
|
||||||
#################################################################################
|
#################################################################################
|
||||||
# Zadanie 4
|
# Zadanie 4
|
||||||
# Funkcja odwrotnosc(a,b) wykorzystujac liczby hex
|
# Funkcja odwrotnosc(a) wykorzystujac liczby hex
|
||||||
#################################################################################
|
#################################################################################
|
||||||
def extended_euklidesHex(a, b)
|
def odwrotnosc(a)
|
||||||
aDec = a.to_i(16)
|
b = a
|
||||||
bDec = b.to_i(16)
|
i = 13
|
||||||
|
const = 1
|
||||||
if bDec == 0
|
while i > 0
|
||||||
return a
|
if i.to_s(2).to_i & const.to_s(2).to_i == 1
|
||||||
else
|
tmp = b
|
||||||
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
|
else
|
||||||
q = (q << (lenF - lenA))
|
tmp = a
|
||||||
end
|
end
|
||||||
f = r
|
b = iloczyn(b,tmp)
|
||||||
return r,q
|
i = i - 1
|
||||||
end
|
end
|
||||||
|
return b
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user