Skip to content

Commit 3460e90

Browse files
committedJul 8, 2023
Support Python 3
1 parent bdde5b2 commit 3460e90

30 files changed

+2061
-1670
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.pyc
22
**/__pycache__/*
33
**egg-info*
4+
**/build/

‎scryptos/crypto/Ciphertext.py

+116-86
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,129 @@
11
class Ciphertext(object):
2-
def __init__(s, parent, v):
3-
s.parent = parent
4-
s.v = v
5-
s.is_homomorphic = hasattr(s.parent, '_homomorphic_type')
6-
if s.is_homomorphic:
7-
s.homomorphic_type = s.parent._homomorphic_type()
2+
def __init__(s, parent, v):
3+
s.parent = parent
4+
s.v = v
5+
s.is_homomorphic = hasattr(s.parent, "_homomorphic_type")
6+
if s.is_homomorphic:
7+
s.homomorphic_type = s.parent._homomorphic_type()
88

9-
def __add__(s, rhs):
10-
assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption'
11-
assert '+' in s.homomorphic_type, 'Parents does not support addition'
12-
if isinstance(rhs, Ciphertext):
13-
assert rhs.parent == s.parent, 'Invalid Parents: %s != %s' % (rhs.parent, s.parent)
14-
return Ciphertext(s.parent, s.parent.hom('+', s.v, rhs.v))
15-
else:
16-
return s + s.parent.encrypt(rhs)
9+
def __add__(s, rhs):
10+
assert (
11+
s.is_homomorphic
12+
), "The cryptosystem/cipher does not support homomorphic encryption"
13+
assert "+" in s.homomorphic_type, "Parents does not support addition"
14+
if isinstance(rhs, Ciphertext):
15+
assert rhs.parent == s.parent, "Invalid Parents: %s != %s" % (
16+
rhs.parent,
17+
s.parent,
18+
)
19+
return Ciphertext(s.parent, s.parent.hom("+", s.v, rhs.v))
20+
else:
21+
return s + s.parent.encrypt(rhs)
1722

18-
def __mul__(s, rhs):
19-
assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption'
20-
if isinstance(rhs, Ciphertext):
21-
assert '*' in s.homomorphic_type, 'Parents does not support multiplication'
22-
assert rhs.parent == s.parent, 'Invalid Parents: %s != %s' % (rhs.parent, s.parent)
23-
return Ciphertext(s.parent, s.parent.hom('*', s.v, rhs.v))
24-
else:
25-
if '*' in s.homomorphic_type:
26-
return s * s.parent.encrypt(rhs)
27-
elif '+' in s.homomorphic_type:
28-
bin_repr = map(int, format(rhs, 'b'))
29-
t = s.parent.encrypt(0)
30-
for x in bin_repr:
31-
if x:
32-
t += s
33-
t += t
34-
return t
35-
else:
36-
raise TypeError('Parents does not support multiplication')
23+
def __mul__(s, rhs):
24+
assert (
25+
s.is_homomorphic
26+
), "The cryptosystem/cipher does not support homomorphic encryption"
27+
if isinstance(rhs, Ciphertext):
28+
assert "*" in s.homomorphic_type, "Parents does not support multiplication"
29+
assert rhs.parent == s.parent, "Invalid Parents: %s != %s" % (
30+
rhs.parent,
31+
s.parent,
32+
)
33+
return Ciphertext(s.parent, s.parent.hom("*", s.v, rhs.v))
34+
else:
35+
if "*" in s.homomorphic_type:
36+
return s * s.parent.encrypt(rhs)
37+
elif "+" in s.homomorphic_type:
38+
bin_repr = map(int, format(rhs, "b"))
39+
t = s.parent.encrypt(0)
40+
for x in bin_repr:
41+
if x:
42+
t += s
43+
t += t
44+
return t
45+
else:
46+
raise TypeError("Parents does not support multiplication")
3747

38-
def __sub__(s, rhs):
39-
assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption'
40-
if isinstance(rhs, Ciphertext):
41-
assert rhs.parent == s.parent, 'Invalid Parents: %s != %s' % (rhs.parent, s.parent)
42-
if '-' in s.homomorphic_type:
43-
return Ciphertext(s.parent, s.parent.hom('-', s.v, rhs.v))
44-
return s + -rhs
45-
else:
46-
return s - s.parent.encrypt(rhs)
48+
def __sub__(s, rhs):
49+
assert (
50+
s.is_homomorphic
51+
), "The cryptosystem/cipher does not support homomorphic encryption"
52+
if isinstance(rhs, Ciphertext):
53+
assert rhs.parent == s.parent, "Invalid Parents: %s != %s" % (
54+
rhs.parent,
55+
s.parent,
56+
)
57+
if "-" in s.homomorphic_type:
58+
return Ciphertext(s.parent, s.parent.hom("-", s.v, rhs.v))
59+
return s + -rhs
60+
else:
61+
return s - s.parent.encrypt(rhs)
4762

48-
def __div__(s, rhs):
49-
assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption'
50-
assert '/' in s.homomorphic_type, 'Parents does not support division'
51-
if isinstance(rhs, Ciphertext):
52-
assert rhs.parent == s.parent, 'Invalid Parents: %s != %s' % (rhs.parent, s.parent)
53-
return Ciphertext(s.parent, s.parent.hom('/', s.v, rhs.v))
54-
else:
55-
return s / s.parent.encrypt(rhs)
63+
def __div__(s, rhs):
64+
assert (
65+
s.is_homomorphic
66+
), "The cryptosystem/cipher does not support homomorphic encryption"
67+
assert "/" in s.homomorphic_type, "Parents does not support division"
68+
if isinstance(rhs, Ciphertext):
69+
assert rhs.parent == s.parent, "Invalid Parents: %s != %s" % (
70+
rhs.parent,
71+
s.parent,
72+
)
73+
return Ciphertext(s.parent, s.parent.hom("/", s.v, rhs.v))
74+
else:
75+
return s / s.parent.encrypt(rhs)
5676

57-
def __neg__(s):
58-
assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption'
59-
if '*' in s.homomorphic_type:
60-
return s * s.parent.encrypt(-1)
61-
elif '-' in s.homomorphic_type:
62-
return s.parent.encrypt(0) - s
63-
else:
64-
raise TypeError('Parents does not support negation')
77+
def __neg__(s):
78+
assert (
79+
s.is_homomorphic
80+
), "The cryptosystem/cipher does not support homomorphic encryption"
81+
if "*" in s.homomorphic_type:
82+
return s * s.parent.encrypt(-1)
83+
elif "-" in s.homomorphic_type:
84+
return s.parent.encrypt(0) - s
85+
else:
86+
raise TypeError("Parents does not support negation")
6587

66-
def __radd__(s, lhs):
67-
assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption'
68-
assert '+' in s.homomorphic_type, 'Parents does not support addition'
69-
return s.parent.encrypt(lhs) + s
88+
def __radd__(s, lhs):
89+
assert (
90+
s.is_homomorphic
91+
), "The cryptosystem/cipher does not support homomorphic encryption"
92+
assert "+" in s.homomorphic_type, "Parents does not support addition"
93+
return s.parent.encrypt(lhs) + s
7094

71-
def __rmul__(s, lhs):
72-
assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption'
73-
if '*' in s.homomorphic_type:
74-
return s.parent.encrypt(lhs) * s
75-
elif '+' in s.homomorphic_type:
76-
return s * lhs
77-
else:
78-
raise TypeError('Parents does not support multiplication')
95+
def __rmul__(s, lhs):
96+
assert (
97+
s.is_homomorphic
98+
), "The cryptosystem/cipher does not support homomorphic encryption"
99+
if "*" in s.homomorphic_type:
100+
return s.parent.encrypt(lhs) * s
101+
elif "+" in s.homomorphic_type:
102+
return s * lhs
103+
else:
104+
raise TypeError("Parents does not support multiplication")
79105

80-
def __rsub__(s, lhs):
81-
assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption'
82-
assert '-' in s.homomorphic_type, 'Parents does not support subtract'
83-
return s.parent.encrypt(lhs) - s
106+
def __rsub__(s, lhs):
107+
assert (
108+
s.is_homomorphic
109+
), "The cryptosystem/cipher does not support homomorphic encryption"
110+
assert "-" in s.homomorphic_type, "Parents does not support subtract"
111+
return s.parent.encrypt(lhs) - s
84112

85-
def __rdiv__(s, lhs):
86-
assert s.is_homomorphic, 'The cryptosystem/cipher does not support homomorphic encryption'
87-
assert '/' in s.homomorphic_type, 'Parents does not support division'
88-
return s.parent.encrypt(lhs) / s
113+
def __rdiv__(s, lhs):
114+
assert (
115+
s.is_homomorphic
116+
), "The cryptosystem/cipher does not support homomorphic encryption"
117+
assert "/" in s.homomorphic_type, "Parents does not support division"
118+
return s.parent.encrypt(lhs) / s
89119

90-
def __eq__(s, rhs):
91-
if isinstance(rhs, Ciphertext):
92-
return s.v == rhs.v
93-
return s.v == rhs
120+
def __eq__(s, rhs):
121+
if isinstance(rhs, Ciphertext):
122+
return s.v == rhs.v
123+
return s.v == rhs
94124

95-
def __str__(s):
96-
return 'Ciphertext of %s: %s' % (s.parent, s.v)
125+
def __str__(s):
126+
return "Ciphertext of %s: %s" % (s.parent, s.v)
97127

98-
def __repr__(s):
99-
return '%s(%r, %r)' % (s.__class__.__name__, s.parent, s.v)
128+
def __repr__(s):
129+
return "%s(%r, %r)" % (s.__class__.__name__, s.parent, s.v)

‎scryptos/crypto/RC4.py

+54-52
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,67 @@
11
from scryptos.math.num import *
22
from .Ciphertext import Ciphertext
33

4+
45
class RC4(object):
5-
"""
6-
RSA Public/Private Key Class
7-
"""
8-
def __init__(s, key):
96
"""
10-
Constructor of RC4
11-
Args:
12-
key : RC4 Key
7+
RSA Public/Private Key Class
138
"""
14-
s.key = key
159

16-
def init_ksa(s):
17-
import math
18-
s.table = list(range(256))
19-
j = 0
20-
key = s.key * int(math.ceil(256./len(s.key)))
21-
for i in range(256):
22-
j = (j + s.table[i] + ord(key[i])) % 256
23-
s.table[i], s.table[j] = s.table[j], s.table[i]
10+
def __init__(s, key):
11+
"""
12+
Constructor of RC4
13+
Args:
14+
key : RC4 Key
15+
"""
16+
s.key = key
2417

25-
def prga(s, length):
26-
s.init_ksa()
27-
i, j = 0, 0
28-
rand = ""
29-
for _ in range(length):
30-
i = (i + 1) % 256
31-
j = (j + s.table[i]) % 256
32-
s.table[i], s.table[j] = s.table[j], s.table[i]
33-
rand += chr(s.table[(s.table[i] + s.table[j]) % 256])
34-
return rand
18+
def init_ksa(s):
19+
import math
3520

21+
s.table = list(range(256))
22+
j = 0
23+
key = s.key * int(math.ceil(256.0 / len(s.key)))
24+
for i in range(256):
25+
j = (j + s.table[i] + ord(key[i])) % 256
26+
s.table[i], s.table[j] = s.table[j], s.table[i]
3627

37-
def encrypt(s, m, raw=False):
38-
"""
39-
RC4 Encryption
40-
Args:
41-
m : Plaintext Message (must be string object)
42-
raw : Is result wrapped by Ciphertext object?
43-
Return: RC4 Ciphertext object or Encrypted String (c_i = m_i XOR PRGA())
44-
"""
45-
c = "".join([chr(ord(x) ^ ord(y)) for x, y in zip(m, s.prga(len(m)))])
46-
if raw:
47-
return c
48-
return Ciphertext(s, c)
28+
def prga(s, length):
29+
s.init_ksa()
30+
i, j = 0, 0
31+
rand = ""
32+
for _ in range(length):
33+
i = (i + 1) % 256
34+
j = (j + s.table[i]) % 256
35+
s.table[i], s.table[j] = s.table[j], s.table[i]
36+
rand += chr(s.table[(s.table[i] + s.table[j]) % 256])
37+
return rand
4938

50-
def decrypt(s, c):
51-
"""
52-
RC4 Decryption
53-
Args:
54-
c : Ciphertext (must be string object or Ciphertext object)
55-
Return: RC4 Decrypted String (same as `encrypt`)
56-
"""
57-
if isinstance(c, Ciphertext):
58-
c = c.v
59-
return s.encrypt(c, True)
39+
def encrypt(s, m, raw=False):
40+
"""
41+
RC4 Encryption
42+
Args:
43+
m : Plaintext Message (must be string object)
44+
raw : Is result wrapped by Ciphertext object?
45+
Return: RC4 Ciphertext object or Encrypted String (c_i = m_i XOR PRGA())
46+
"""
47+
c = "".join([chr(ord(x) ^ ord(y)) for x, y in zip(m, s.prga(len(m)))])
48+
if raw:
49+
return c
50+
return Ciphertext(s, c)
51+
52+
def decrypt(s, c):
53+
"""
54+
RC4 Decryption
55+
Args:
56+
c : Ciphertext (must be string object or Ciphertext object)
57+
Return: RC4 Decrypted String (same as `encrypt`)
58+
"""
59+
if isinstance(c, Ciphertext):
60+
c = c.v
61+
return s.encrypt(c, True)
6062

61-
def __str__(s):
62-
return "RC4 Instance: key = %r" % s.key
63+
def __str__(s):
64+
return "RC4 Instance: key = %r" % s.key
6365

64-
def __repr__(s):
65-
return "RC4(%r)" % s.key
66+
def __repr__(s):
67+
return "RC4(%r)" % s.key

0 commit comments

Comments
 (0)