Skip to content

Commit dfc6ebe

Browse files
committed
Initial commit
0 parents  commit dfc6ebe

File tree

9 files changed

+383
-0
lines changed

9 files changed

+383
-0
lines changed

.gitignore

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*.cover
47+
.hypothesis/
48+
49+
# Translations
50+
*.mo
51+
*.pot
52+
53+
# Django stuff:
54+
*.log
55+
local_settings.py
56+
57+
# Flask stuff:
58+
instance/
59+
.webassets-cache
60+
61+
# Scrapy stuff:
62+
.scrapy
63+
64+
# Sphinx documentation
65+
docs/_build/
66+
67+
# PyBuilder
68+
target/
69+
70+
# Jupyter Notebook
71+
.ipynb_checkpoints
72+
73+
# pyenv
74+
.python-version
75+
76+
# celery beat schedule file
77+
celerybeat-schedule
78+
79+
# SageMath parsed files
80+
*.sage.py
81+
82+
# dotenv
83+
.env
84+
85+
# virtualenv
86+
.venv
87+
venv/
88+
ENV/
89+
90+
# Spyder project settings
91+
.spyderproject
92+
.spyproject
93+
94+
# Rope project settings
95+
.ropeproject
96+
97+
# mkdocs documentation
98+
/site
99+
100+
# mypy
101+
.mypy_cache/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Christian Pfarr
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# simplersa
2+
Simple RSA Library completely written in Python

euclid.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Euclid:
4+
def gcd(self, a, b):
5+
while (b != 0):
6+
(a, b) = (b, a % b)
7+
return abs(a)
8+
9+
def extendedGcd(self, a, b):
10+
u, v, s, t = 1, 0, 0, 1
11+
oa, ob = a, b
12+
while b > 0:
13+
q = a // b
14+
a, b = b, a - q * b
15+
u, s = s, u - q * s
16+
v, t = t, v - q * t
17+
if (u < 0): u += ob
18+
if (v < 0): v += oa
19+
return a, u, v

jacobi.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Jacobi:
4+
5+
def symbol(self, a, b):
6+
result = 1
7+
while (a != 0):
8+
while ((a % 2) == 0):
9+
a = a // 2
10+
if ((b == (3 % 8)) or (b == (5 % 8))):
11+
result = -result
12+
(a, b) = (b, a)
13+
if ((a == (3 % 4)) or (b == (3 % 4))):
14+
result = -result
15+
a = a % b
16+
if (b == 1):
17+
return result
18+
return 0

key.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Key:
4+
def __init__(self, modulo, exponent):
5+
self.__modulo = modulo
6+
self.__exponent = exponent
7+
8+
def getBits(self):
9+
return self.__modulo.bit_length()
10+
11+
def getModulo(self):
12+
return self.__modulo
13+
14+
def getExponent(self):
15+
return self.__exponent
16+
17+
def isPublic(self):
18+
return False
19+
20+
def isPrivate(self):
21+
return False
22+
23+
24+
class PrivateKey(Key):
25+
def __init__(self, modulo, decipherExponent):
26+
Key.__init__(self, modulo, decipherExponent)
27+
28+
def getDecipherExponent(self):
29+
return self.getExponent()
30+
31+
def isPrivate(self):
32+
return True
33+
34+
35+
class PublicKey(Key):
36+
def __init__(self, modulo, encipherExponent):
37+
Key.__init__(self, modulo, encipherExponent)
38+
self.__isPublic = True
39+
40+
def getEncipherExponent(self):
41+
return self.getExponent()
42+
43+
def isPublic(self):
44+
return True

prime.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import random
4+
import solovaystrassen
5+
6+
7+
class Prime:
8+
__SOLOVAYSTRASSENROUNDS = 100
9+
10+
def __init__(self, bits):
11+
self.__solovayStrassen = solovaystrassen.SolovayStrassen()
12+
while True:
13+
prime = random.SystemRandom().getrandbits(bits)
14+
prime |= 1
15+
if self.__isPrimality(prime): break
16+
self.__value = prime
17+
18+
def __del__(self):
19+
del self.__value
20+
21+
def __eq__(self, b):
22+
if isinstance(b, Prime):
23+
if self.__value == b.__value:
24+
return True
25+
else:
26+
return False
27+
else:
28+
if self.__value == b:
29+
return True
30+
else:
31+
return False
32+
33+
def __add_(self, b):
34+
if isinstance(b, Prime):
35+
return (self.__value + b.__value)
36+
else:
37+
return (self.__value + b)
38+
39+
def __sub__(self, b):
40+
if isinstance(b, Prime):
41+
return (self.__value - b.__value)
42+
else:
43+
return (self.__value - b)
44+
45+
def __mul__(self, b):
46+
if isinstance(b, Prime):
47+
return (self.__value * b.__value)
48+
else:
49+
return (self.__value * b)
50+
51+
def __div__(self, b):
52+
if isinstance(b, Prime):
53+
return (self.__value / b.__value)
54+
else:
55+
return (self.__value / b)
56+
57+
def getValue(self):
58+
return self.__value
59+
60+
def __isPrimality(self, maybe):
61+
for _ in range(self.__SOLOVAYSTRASSENROUNDS):
62+
x = random.SystemRandom().randint(1, maybe)
63+
if self.__solovayStrassen.isComposite(x, maybe):
64+
return False
65+
return True

simplersa.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import euclid
4+
import prime
5+
import key
6+
import random
7+
8+
9+
class SimpleRsa:
10+
def __init__(self):
11+
self.__euclid = euclid.Euclid()
12+
13+
def __calculateModulo(self, p, q):
14+
modulo = p * q
15+
return modulo
16+
17+
def __calculatePhi(self, p, q):
18+
phi = (p - 1) * (q - 1)
19+
return phi
20+
21+
def __calculateEncipherExponent(self, phi):
22+
while True:
23+
encipherExponent = random.SystemRandom().randint(1, phi)
24+
if (self.__euclid.gcd(encipherExponent, phi) == 1):
25+
break
26+
return encipherExponent
27+
28+
def __calculateDecipherExponent(self, phi, encipherExponent):
29+
gcd, k, decipherExponent = self.__euclid.extendedGcd(phi, encipherExponent)
30+
return decipherExponent
31+
32+
def __checkModulo(self, modulo):
33+
if (modulo.bit_length() == bits):
34+
return True
35+
else:
36+
return False
37+
38+
def generateKeyPair(self, bits=2048):
39+
p = 0
40+
q = 0
41+
pBits = bits // 2
42+
qBits = bits // 2
43+
change = 0
44+
while True:
45+
if (p == 0) or (change == 1):
46+
p = prime.Prime(pBits)
47+
if (q == 0) or (change == 0):
48+
q = prime.Prime(qBits)
49+
modulo = self.__calculateModulo(p, q)
50+
if self.__checkModulo(modulo):
51+
break
52+
else:
53+
if (change == 0):
54+
change = 1
55+
else:
56+
change = 0
57+
continue
58+
phi = self.__calculatePhi(p, q)
59+
encipherExponent = self.__calculateEncipherExponent(phi)
60+
decipherExponent = self.__calculateDecipherExponent(phi, encipherExponent)
61+
privateKey = key.PrivateKey(modulo, decipherExponent)
62+
publicKey = key.PublicKey(modulo, encipherExponent)
63+
return (privateKey, publicKey)
64+
65+
def encipher(self, data, encipherExponent, modulo):
66+
return pow(data, encipherExponent, modulo)
67+
68+
def decipher(self, data, decipherExponent, modulo):
69+
return pow(data, decipherExponent, modulo)
70+
71+
def sign(self, data, decipherExponent, modulo):
72+
return pow(data, decipherExponent, modulo)
73+
74+
def verify(self, data, encipherExponent, modulo):
75+
return pow(data, encipherExponent, modulo)
76+
77+
78+
# TESTS
79+
rsa = SimpleRsa()
80+
data = int(input("Data : "))
81+
while True:
82+
bits = int(input("Bits : "))
83+
privateKey, publicKey = rsa.generateKeyPair(bits)
84+
print("encipherExponent : ", publicKey.getEncipherExponent())
85+
print("decipherExponent : ", privateKey.getDecipherExponent())
86+
encipheredData = rsa.encipher(data, publicKey.getEncipherExponent(), publicKey.getModulo())
87+
print("encipheredData : ", encipheredData)
88+
decipheredData = rsa.decipher(encipheredData, privateKey.getDecipherExponent(), privateKey.getModulo())
89+
print("decipheredData : ", decipheredData)
90+
if input("nochmal? ") == "nein": break

solovaystrassen.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import euclid
4+
import jacobi
5+
6+
7+
class SolovayStrassen:
8+
def __init__(self):
9+
self.__euclid = euclid.Euclid()
10+
self.__jacobi = jacobi.Jacobi()
11+
12+
def isComposite(self, a, maybe):
13+
g = self.__euclid.gcd(a, maybe)
14+
b = pow(a, maybe >> 1, maybe)
15+
if g > 1:
16+
return True
17+
elif (1 < b) and (b < maybe - 1):
18+
return True
19+
elif (b == 1) or (b == maybe - 1):
20+
j = self.__jacobi.symbol(a, maybe)
21+
c = b % maybe
22+
if (j == c): return False
23+
return False

0 commit comments

Comments
 (0)