Пример #1
0
 def action(self):
     # Генератор
     # m
     m = self.field_m.get()
     if not m:
         m = rsa.randint(10, 10000)
         self.field_m.put(m)
         return
     else:
         m = int(m)
     # p
     p = self.field_p.get()
     if not p:
         p = rsa.genprime(32)
         self.field_p.put(p)
     else:
         p = int(p)
     if not rsa.solovay_strassen(p):
         messagebox.showerror("Ошибка", "Число p не простое")
     # q
     q = self.field_q.get()
     if not q:
         q = rsa.genprime(32)
         self.field_q.put(q)
     else:
         q = int(q)
     if not rsa.solovay_strassen(q) or q == p:
         messagebox.showerror("Ошибка", "Число q не простое")
     # k
     phi = (p - 1) * (q - 1)
     k = self.field_k.get()
     if not k:
         k = rsa.randint(2, phi - 1)
         while rsa.gcd(k, phi) != 1:
             k = rsa.randint(2, phi - 1)
         self.field_k.put(k)
     else:
         k = int(k)
     if not (1 < k < phi and rsa.gcd(k, phi) == 1):
         messagebox.showerror(
             "Ошибка",
             "Некорректное значение k (1 < k < %d и НОД(k, %d))" %
             (phi, phi))
     # u0
     N = p * q
     u0 = self.field_u0.get()
     if not u0:
         u0 = rsa.randint(2, N - 2)
         self.field_u0.put(u0)
     else:
         u0 = int(u0)
     if not (1 < u0 < N - 1):
         messagebox.showerror(
             "Ошибка",
             "Некорректное значение u0 (1 < u0 < %d)" % (N - 1))
     seq = rsa.generate(m, p, q, k, u0)
     self.put(seq)
     self.calcStat()
Пример #2
0
 def test_gcd(self):
     self.assertEqual(0, rsa.gcd(0, 0))
     self.assertEqual(1, rsa.gcd(3, 7))
     self.assertEqual(3, rsa.gcd(12, 15))
     self.assertEqual(9, rsa.gcd(0, 9))
     self.assertEqual(12, rsa.gcd(12, 0))
     self.assertEqual(14, rsa.gcd(42, 56))
     self.assertEqual(18, rsa.gcd(461952, 116298))
     self.assertEqual(32, rsa.gcd(7966496, 314080416))
     self.assertEqual(526, rsa.gcd(24826148, 45296490))
Пример #3
0
print(kryptert_melding)  # -> [58822763, 79142533]

# d)
primtall = rsa.generate_primes(2, int(np.sqrt(n)))

p_q = [tall for tall in primtall if n % tall == 0][0]
p = p_q
q = int(n / p_q)

print(p, q)  # -> 11897 13463

# e)
# for at e er gyldig må gcd((p-1)*(q-1), e) == 1

phi = (p - 1) * (q - 1)
(g, y, x) = rsa.gcd(phi, e)

print(g == 1)  # -> True

# f)
d = x % phi
print("".join(
    [fraTallTilTekst(rsa.powermod(c, d, n), 4) for c in kryptert_melding]))
# -> HEI SJEF

# g)
U = [112718817, 85128008, 148479246, 91503316, 26066602, 95584344, 142943071]
print("".join([fraTallTilTekst(rsa.powermod(c, d, n), 4) for c in U]))
# -> "  JEG GLEDER MEG TIL EKSAMEN"

# Oppgave 2
Пример #4
0
 def test_trivial(self):
   self.assertEquals(rsa.gcd(3, 4), 1)
   self.assertEquals(rsa.gcd(4, 3), 1)
   self.assertEquals(rsa.gcd(10, 25), 5)
   self.assertEquals(rsa.gcd(11, 17), 1)
   self.assertEquals(rsa.gcd(561, 253), 11)
Пример #5
0
class rsaTestCase(unittest.TestCase):
    def test_gcd(self):
        cases = [(12, 15, 3), (3, 7, 1)]
        for i, (a, b, c) in enumerate(cases):
            with self.subTest(case=i, a=a, c=c):
                self.assertEqual(c, rsa.gcd(a, b=b))
Пример #6
0
 def test_gsd(self):
     self.assertEqual(rsa.gcd(17, 23), 1)