-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_rsa.py
executable file
·162 lines (117 loc) · 3.65 KB
/
test_rsa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
import unittest
import rsa
import examples
import random
import sympy
class TestGetRandomPrime(unittest.TestCase):
def test_random_prime(self):
"""
Test ob zurückgegebene Primzahl eine Primzahl ist.
"""
want = True
get = sympy.isprime(rsa.get_random_prime(128))
self.assertEqual(want, get)
class TestEuclid(unittest.TestCase):
def test_coprime(self):
"""
Testen ob Zahl a teilerfremd zu Zahl b ist.
"""
want = 1
get = rsa.euclid(89, 3)
self.assertEqual(want, get)
def test_not_coprime(self):
"""
Testen ob Zahl a nicht teilerfremd zu Zahl b ist.
"""
want = 5
get = rsa.euclid(895, 85)
self.assertEqual(want, get)
class TestExtendedEuclid(unittest.TestCase):
def test_coprime(self):
""" """
want = (1, -29, 5)
get = rsa.extended_euclid(16, 93)
self.assertEqual(want, get)
class TestModulareInverse(unittest.TestCase):
def test_prime(self):
""" """
want = 64
get = rsa.modular_inverse(16, 93)
self.assertEqual(want, get)
class TestFermatNumbers(unittest.TestCase):
def test_fermat_number(self):
""""""
# (1) Fermat Zahl
want = 3
get = rsa.fermat_numbers(4)
self.assertEqual(want, get)
# (2) Fermat Zahl
want = 5
get = rsa.fermat_numbers(12)
self.assertEqual(want, get)
# (3) Fermat Zahl
want = 17
get = rsa.fermat_numbers(45)
self.assertEqual(want, get)
def test_no_fermat_number(self):
""""""
want = -1
get = rsa.fermat_numbers(3 * 5 * 17 * 257 * 65537)
self.assertEqual(want, get)
class TestEncrypt(unittest.TestCase):
def __init__(self, methodName: str = ...) -> None:
super().__init__(methodName=methodName)
self.n = examples.n
self.e = examples.e
self.d = examples.d
self.rsa = rsa.RSA()
def test_correct_encrypt(self):
"""
Test ob Zahl m richig verschlüsselt wird.
"""
m = 63
want = 105
get = self.rsa.encrypt(m, self.e, self.n)
self.assertEqual(want, get)
def test_message_too_long(self):
"""
Test mit zu langer Naricht.
"""
want = -1
get = self.rsa.encrypt(
int.from_bytes(random.randbytes(4097), "big"), self.e, self.n
)
self.assertEqual(want, get)
class TestDecrypt(unittest.TestCase):
def __init__(self, methodName: str = ...) -> None:
super().__init__(methodName=methodName)
self.n = examples.n
self.e = examples.e
self.d = examples.d
self.rsa = rsa.RSA()
def test_correct_decrypt(self):
"""
Test ob Zahl c richtig entschlüsselt wird.
"""
c = 105
want = 63
get = self.rsa.decrypt(c, self.d, self.n)
self.assertEqual(want, get)
class TestTextDecryption(unittest.TestCase):
def __init__(self, methodName: str = ...) -> None:
super().__init__(methodName=methodName)
self.n = examples.n_4096
self.e = examples.e_4096
self.d = examples.d_4096
self.text = "Works for me!"
self.rsa = rsa.RSA()
def test_correct_encrypt(self):
"""
Test ob Text richtig ver- und entschlüsselt wird.
"""
c_blocks = self.rsa.encrypt_text(self.text, self.e, self.n)
message = self.rsa.decrypt_text(c_blocks, self.d, self.n)
self.assertEqual(self.text, message.replace("\0", ""))
if __name__ == "__main__":
unittest.main()