• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Feb 27th, 2020 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import crybas
2. from random import randint
3.
4. # shamir
5.
6. def shamir(m):
7.     p = crybas.prime_generate()
8.
9.     if m > p:
10.         return "error: message too long."
11.
12.     # Limits
13.     down_limit = 10 ** 7
14.     up_limit = 10 ** 9
15.
16.     # Alice
17.
18.     # Ca generation
19.     while True:
20.         Ca = randint(down_limit, up_limit)
21.         if crybas.extended_gcd(Ca, p - 1)[0] == 1:
22.             break
23.     # Da generation
24.     Da = crybas.extended_gcd(Ca, p - 1)[1]
25.     if Da < 0:
26.         Da += p - 1
27.
28.     # Bob
29.
30.     # Cb generation
31.     while True:
32.         Cb = randint(down_limit, up_limit)
33.         if crybas.extended_gcd(Cb, p - 1)[0] == 1:
34.             break
35.     # Db generation
36.     Db = crybas.extended_gcd(Cb, p - 1)[1]
37.     if Db < 0:
38.         Db += p - 1
39.
40.     # Alice
41.     x1 = crybas.fast_pow(m, Ca, p)
42.     # Bob
43.     x2 = crybas.fast_pow(x1, Cb, p)
44.     # Alice
45.     x3 = crybas.fast_pow(x2, Da, p)
46.     # Bob
47.     x4 = crybas.fast_pow(x3, Db, p)
48.
49.     return x4
50.
51. # shamir
52.
53.
54.
55. # elgamal
56.
57. def elgamal(m):
58.     p = crybas.prime_generate()
59.     g = crybas.g_generate(p)
60.
61.     if m > p:
62.         return "error: message too long."
63.
64.     # Alice
65.     k = randint(1, p - 1)
66.     a = crybas.fast_pow(g, k, p)
67.     x = randint(1, p)
68.     y = crybas.fast_pow(g, x, p)
69.     b = crybas.fast_pow_multiplicate(y, k, p, m)
70.
71.     # Bob
72.     m_from_alice = crybas.fast_pow_multiplicate(a, p - 1 - x, p, b)
73.
74.     return m_from_alice
75.
76.
77. # elgamal
78.
79.
80.
81. # vernam
82.
83. def vernam(m):
84.     k = randint(1, 1000000000)
85.     e = m ^ k
86.     e_decrypt = e ^ k
87.
88.     return e_decrypt
89.
90. # vernam
91.
92.
93.
94. # rsa
95.
96. def rsa(m):
97.     p = crybas.prime_generate()
98.     q = crybas.prime_generate()
99.     n = p * q
100.
101.     if m > n:
102.         return "Error: message too long."
103.
104.     f = (p - 1) * (q - 1)
105.     d = randint(1, f)
106.     while True:
107.         if crybas.extended_gcd(d, f)[0] == 1:
108.             break
109.         d = randint(1, f)
110.
111.     c = crybas.extended_gcd(f, d)[2]
112.     if c < 0:
113.         c += f
114.
115.     e = crybas.fast_pow(m, d, n)
116.     m_from_alice = crybas.fast_pow(e, c, n)
117.
118.     return m_from_alice
119.
120. # rsa
121.
122.
123. def use_ciphers():
124.     m = randint(1, 1000000)
125.     print("message:", m)
126.     print("***********************")
127.     print("shamir:", shamir(m))
128.     print("elgamal:", elgamal(m))
129.     print("vernam:", vernam(m))
130.     print("rsa:", rsa(m))
131.
132.
133. use_ciphers()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top