Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/crypto/cipher.py
4045 views
1
"""
2
Ciphers
3
"""
4
5
#*****************************************************************************
6
# Copyright (C) 2007 David Kohel <[email protected]>
7
#
8
# Distributed under the terms of the GNU General Public License (GPL)
9
#
10
# http://www.gnu.org/licenses/
11
#*****************************************************************************
12
13
# Ciphers should inherit from morphisms (of sets).
14
# Specific cipher types will implement their functions in terms of the key
15
16
from sage.structure.element import Element
17
18
class Cipher(Element):
19
"""
20
Cipher class
21
"""
22
def __init__(self, parent, key):
23
"""
24
Create a cipher.
25
26
INPUT: Parent and key
27
28
EXAMPLES: None yet
29
"""
30
Element.__init__(self, parent)
31
self._key = key
32
33
def __eq__(self, right):
34
return type(self) == type(right) and self.parent() == right.parent() and self._key == right._key
35
36
def _repr_(self):
37
r"""
38
Return the string representation of this cipher.
39
40
EXAMPLES::
41
42
sage: S = ShiftCryptosystem(AlphabeticStrings())
43
sage: S(13)
44
Shift cipher on Free alphabetic string monoid on A-Z
45
"""
46
# return str(self._key)
47
return "Cipher on %s" % self.parent().cipher_domain()
48
49
def key(self):
50
return self._key # was str(self._key)
51
52
def domain(self):
53
return self.parent().cipher_domain()
54
55
def codomain(self):
56
return self.parent().cipher_codomain()
57
58
class SymmetricKeyCipher(Cipher):
59
"""
60
Symmetric key cipher class
61
"""
62
def __init__(self, parent, key):
63
"""
64
Create a symmetric cipher
65
66
INPUT: Parent and key
67
68
EXAMPLES: None yet
69
"""
70
Cipher.__init__(self, parent, key)
71
72
class PublicKeyCipher(Cipher):
73
"""
74
Public key cipher class
75
"""
76
def __init__(self, parent, key, public = True):
77
"""
78
Create a public key cipher
79
80
INPUT: Parent and key
81
82
EXAMPLES: None yet
83
"""
84
Cipher.__init__(self, parent, key)
85
self._public = public
86
87