-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
453 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,302 @@ | ||
# Aleksander Kluczka | ||
|
||
|
||
def multiplicative_inverse(a, n): | ||
def nwd(j, k): | ||
if j == 0: | ||
return k | ||
r = k % j | ||
return nwd(r, j) | ||
|
||
def nwd_expanded(j, k): | ||
assert 0 <= j < k | ||
if j == 0: | ||
return k, 0, 1 | ||
r = k % j | ||
d, xp, yp = nwd_expanded(r, j) | ||
x = yp - (k // j) * xp | ||
y = xp | ||
return d, x, y | ||
|
||
d, x, y = nwd_expanded(a, n) | ||
reverse_a = x % n | ||
assert (a * reverse_a) % n == 1 | ||
return reverse_a | ||
|
||
|
||
def zad1(): | ||
n = 18923 | ||
b = 1261 | ||
n_1 = 127 | ||
n_2 = 149 | ||
phi_n = 18648 | ||
|
||
a = multiplicative_inverse(b, phi_n) | ||
encrypted = [ | ||
12423, | ||
11524, | ||
7243, | ||
7459, | ||
14303, | ||
6127, | ||
10964, | ||
16399, | ||
9792, | ||
13629, | ||
14407, | ||
18817, | ||
18830, | ||
13556, | ||
3159, | ||
16647, | ||
5300, | ||
13951, | ||
81, | ||
8986, | ||
8007, | ||
13167, | ||
10022, | ||
17213, | ||
2264, | ||
961, | ||
17459, | ||
4101, | ||
2999, | ||
14569, | ||
17183, | ||
15827, | ||
12693, | ||
9553, | ||
18194, | ||
3830, | ||
2664, | ||
13998, | ||
12501, | ||
18873, | ||
12161, | ||
13071, | ||
16900, | ||
7233, | ||
8270, | ||
17086, | ||
9792, | ||
14266, | ||
13236, | ||
5300, | ||
13951, | ||
8850, | ||
12129, | ||
6091, | ||
18110, | ||
3332, | ||
15061, | ||
12347, | ||
7817, | ||
7946, | ||
11675, | ||
13924, | ||
13892, | ||
18031, | ||
2620, | ||
6276, | ||
8500, | ||
201, | ||
8850, | ||
11178, | ||
16477, | ||
10161, | ||
3533, | ||
13842, | ||
7537, | ||
12259, | ||
18110, | ||
44, | ||
2364, | ||
15570, | ||
3460, | ||
9886, | ||
8687, | ||
4481, | ||
11231, | ||
7547, | ||
11383, | ||
17910, | ||
12867, | ||
13203, | ||
5102, | ||
4742, | ||
5053, | ||
15407, | ||
2976, | ||
9330, | ||
12192, | ||
56, | ||
2471, | ||
15334, | ||
841, | ||
13995, | ||
17592, | ||
13297, | ||
2430, | ||
9741, | ||
11675, | ||
424, | ||
6686, | ||
738, | ||
13874, | ||
8168, | ||
7913, | ||
6246, | ||
14301, | ||
1144, | ||
9056, | ||
15967, | ||
7328, | ||
13203, | ||
796, | ||
195, | ||
9872, | ||
16979, | ||
15404, | ||
14130, | ||
9105, | ||
2001, | ||
9792, | ||
14251, | ||
1498, | ||
11296, | ||
1105, | ||
4502, | ||
16979, | ||
1105, | ||
56, | ||
4118, | ||
11302, | ||
5988, | ||
3363, | ||
15827, | ||
6928, | ||
4191, | ||
4277, | ||
10617, | ||
874, | ||
13211, | ||
11821, | ||
3090, | ||
18110, | ||
44, | ||
2364, | ||
15570, | ||
3460, | ||
9886, | ||
9988, | ||
3798, | ||
1158, | ||
9872, | ||
16979, | ||
15404, | ||
6127, | ||
9872, | ||
3652, | ||
14838, | ||
7437, | ||
2540, | ||
1367, | ||
2512, | ||
14407, | ||
5053, | ||
1521, | ||
297, | ||
10935, | ||
17137, | ||
2186, | ||
9433, | ||
13293, | ||
7555, | ||
13618, | ||
13000, | ||
6490, | ||
5310, | ||
18676, | ||
4782, | ||
11374, | ||
446, | ||
4165, | ||
11634, | ||
3846, | ||
14611, | ||
2364, | ||
6789, | ||
11634, | ||
4493, | ||
4063, | ||
4576, | ||
17955, | ||
7965, | ||
11748, | ||
14616, | ||
11453, | ||
17666, | ||
925, | ||
56, | ||
4118, | ||
18031, | ||
9522, | ||
14838, | ||
7437, | ||
3880, | ||
11476, | ||
8305, | ||
5102, | ||
2999, | ||
18628, | ||
14326, | ||
9175, | ||
9061, | ||
650, | ||
18110, | ||
8720, | ||
15404, | ||
2951, | ||
722, | ||
15334, | ||
841, | ||
15610, | ||
2443, | ||
11056, | ||
2186, | ||
] | ||
|
||
def find_power(code): | ||
power = 0 | ||
while code >= 26**power: | ||
power += 1 | ||
return power - 1 | ||
|
||
def decode_to_letters(code: int) -> str: | ||
temp: int = code + 0 | ||
numbers = [] | ||
power: int = find_power(temp) | ||
while power >= 0: | ||
min_val = 26**power | ||
number = temp // min_val | ||
numbers.append(number) | ||
temp = temp - min_val * number | ||
power -= 1 | ||
|
||
letters: str = "".join([chr(ord("a") + number) for number in numbers]) | ||
|
||
return letters | ||
|
||
for code in encrypted: | ||
decrypted = pow(code, a, n) | ||
letters = decode_to_letters(decrypted) | ||
print(f"{letters}", end="") | ||
|
||
|
||
def main(): | ||
zad1() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Aleksander Kluczka | ||
|
||
|
||
def multiplicative_inverse(a, n): | ||
def nwd_expanded(j, k): | ||
if j == 0: | ||
return k, 0, 1 | ||
r = k % j | ||
d, xp, yp = nwd_expanded(r, j) | ||
x = yp - (k // j) * xp | ||
y = xp | ||
return d, x, y | ||
|
||
d, x, y = nwd_expanded(a, n) | ||
reverse_a = x % n | ||
assert (a * reverse_a) % n == 1 | ||
return reverse_a | ||
|
||
|
||
def zad2(): | ||
p = 1511 | ||
q = 2003 | ||
d = 1234577 | ||
dp = d % (p - 1) | ||
dq = d % (q - 1) | ||
mp = multiplicative_inverse(q, p) | ||
mq = multiplicative_inverse(p, q) | ||
n = (p - 1) * (q - 1) | ||
|
||
print(f"{dp=}, {dq=}, {mp=}, {mq=}") | ||
|
||
def ctr(n, dp, dq, mp, mq, y): | ||
xp = pow(y, dp, p) | ||
xq = pow(y, dq, q) | ||
print(f"{xp=}, {xq=}") | ||
x = ((mp * q * xp) + (mq * p * xq)) % n | ||
return x | ||
|
||
x = ctr(n, dp, dq, mp, mq, 152702) | ||
print(f"{x=}") | ||
|
||
|
||
def main(): | ||
zad2() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Aleksander Kluczka | ||
|
||
|
||
def square_remainders(p): | ||
return set([pow(i, 2, p) for i in range(p)]) | ||
|
||
|
||
def euler_criteria(p): | ||
return [0] + [i for i in range(p) if pow(i, (p - 1) // 2, p) == 1] | ||
|
||
|
||
def zad3(): | ||
square = square_remainders(13) | ||
print(square) | ||
euler = euler_criteria(13) | ||
print(euler) | ||
|
||
|
||
def main(): | ||
zad3() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.