mlymath 0.3.0b1__py3-none-any.whl → 0.3.0b2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- RSA/RSA.py +73 -0
- RSA/__init__.py +13 -0
- math/__init__.py +2 -8
- math/math.py +0 -61
- {mlymath-0.3.0b1.dist-info → mlymath-0.3.0b2.dist-info}/METADATA +5 -2
- {mlymath-0.3.0b1.dist-info → mlymath-0.3.0b2.dist-info}/RECORD +8 -6
- {mlymath-0.3.0b1.dist-info → mlymath-0.3.0b2.dist-info}/top_level.txt +1 -0
- {mlymath-0.3.0b1.dist-info → mlymath-0.3.0b2.dist-info}/WHEEL +0 -0
RSA/RSA.py
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
import hashlib
|
2
|
+
import random
|
3
|
+
from typing import Tuple
|
4
|
+
|
5
|
+
def get_rsa_key(p: int, q: int) -> Tuple[Tuple[int, int], Tuple[int, int]]:
|
6
|
+
"""
|
7
|
+
生成 RSA 公钥和私钥。
|
8
|
+
:param p: 第一个大素数
|
9
|
+
:param q: 第二个大素数
|
10
|
+
:return: 公钥 (e, n) 和私钥 (d, n)
|
11
|
+
"""
|
12
|
+
n = p * q
|
13
|
+
phi = (p - 1) * (q - 1)
|
14
|
+
|
15
|
+
def multiplicative_inverse(e: int, phi: int) -> int:
|
16
|
+
d = 0
|
17
|
+
x1 = 0
|
18
|
+
x2 = 1
|
19
|
+
y1 = 1
|
20
|
+
temp_phi = phi
|
21
|
+
|
22
|
+
while e > 0:
|
23
|
+
temp1 = temp_phi // e
|
24
|
+
temp2 = temp_phi - temp1 * e
|
25
|
+
temp_phi = e
|
26
|
+
e = temp2
|
27
|
+
|
28
|
+
x = x2 - temp1 * x1
|
29
|
+
y = d - temp1 * y1
|
30
|
+
|
31
|
+
x2 = x1
|
32
|
+
x1 = x
|
33
|
+
d = y1
|
34
|
+
y1 = y
|
35
|
+
|
36
|
+
if temp_phi == 1:
|
37
|
+
return d + phi
|
38
|
+
|
39
|
+
e = random.randint(1, phi)
|
40
|
+
while gcd(e, phi) != 1: # 直接调用模块级 gcd 函数
|
41
|
+
e = random.randint(1, phi)
|
42
|
+
|
43
|
+
d = multiplicative_inverse(e, phi)
|
44
|
+
return (e, n), (d, n)
|
45
|
+
|
46
|
+
def rsa_encrypt(public_key: Tuple[int, int], plaintext: int) -> int:
|
47
|
+
"""
|
48
|
+
使用 RSA 公钥加密数据。
|
49
|
+
:param public_key: 公钥 (e, n)
|
50
|
+
:param plaintext: 明文数据
|
51
|
+
:return: 密文数据
|
52
|
+
"""
|
53
|
+
e, n = public_key
|
54
|
+
return pow(plaintext, e, n)
|
55
|
+
|
56
|
+
def rsa_decrypt(private_key: Tuple[int, int], ciphertext: int) -> int:
|
57
|
+
"""
|
58
|
+
使用 RSA 私钥解密数据。
|
59
|
+
:param private_key: 私钥 (d, n)
|
60
|
+
:param ciphertext: 密文数据
|
61
|
+
:return: 明文数据
|
62
|
+
"""
|
63
|
+
d, n = private_key
|
64
|
+
return pow(ciphertext, d, n)
|
65
|
+
|
66
|
+
def RSA_help():
|
67
|
+
"""提供模块帮助信息"""
|
68
|
+
print("以下是可用的函数:")
|
69
|
+
print("- get_rsa_keys(p: int, q: int) -> Tuple[Tuple[int, int], Tuple[int, int]]: 生成 RSA 公钥和私钥")
|
70
|
+
print("- rsa_encrypt(public_key: Tuple[int, int], plaintext: int) -> int: 使用 RSA 公钥加密数据")
|
71
|
+
print("- rsa_decrypt(private_key: Tuple[int, int], ciphertext: int) -> int: 使用 RSA 私钥解密数据")
|
72
|
+
print("- RSA_help() -> None: 显示帮助信息")
|
73
|
+
|
RSA/__init__.py
ADDED
math/__init__.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
from .math import (
|
2
|
+
sha256,
|
2
3
|
sha3_256,
|
3
4
|
sha3_512,
|
4
|
-
sha256,
|
5
5
|
gcd,
|
6
6
|
lcm,
|
7
7
|
is_prime,
|
@@ -27,16 +27,13 @@ from .math import (
|
|
27
27
|
max_index,
|
28
28
|
list_min_index,
|
29
29
|
min_index,
|
30
|
-
get_rsa_key,
|
31
|
-
rsa_encrypt,
|
32
|
-
rsa_decrypt,
|
33
30
|
math_help
|
34
31
|
)
|
35
32
|
|
36
33
|
__all__ = [
|
34
|
+
'sha256',
|
37
35
|
'sha3_256',
|
38
36
|
'sha3_512',
|
39
|
-
'sha256',
|
40
37
|
'gcd',
|
41
38
|
'lcm',
|
42
39
|
'is_prime',
|
@@ -62,8 +59,5 @@ __all__ = [
|
|
62
59
|
'max_index',
|
63
60
|
'list_min_index',
|
64
61
|
'min_index',
|
65
|
-
'get_rsa_key',
|
66
|
-
'rsa_encrypt',
|
67
|
-
'rsa_decrypt',
|
68
62
|
'math_help'
|
69
63
|
]
|
math/math.py
CHANGED
@@ -2,67 +2,6 @@ import hashlib
|
|
2
2
|
import random
|
3
3
|
from typing import Tuple
|
4
4
|
|
5
|
-
def get_rsa_key(p: int, q: int) -> Tuple[Tuple[int, int], Tuple[int, int]]:
|
6
|
-
"""
|
7
|
-
生成 RSA 公钥和私钥。
|
8
|
-
:param p: 第一个大素数
|
9
|
-
:param q: 第二个大素数
|
10
|
-
:return: 公钥 (e, n) 和私钥 (d, n)
|
11
|
-
"""
|
12
|
-
n = p * q
|
13
|
-
phi = (p - 1) * (q - 1)
|
14
|
-
|
15
|
-
def multiplicative_inverse(e: int, phi: int) -> int:
|
16
|
-
d = 0
|
17
|
-
x1 = 0
|
18
|
-
x2 = 1
|
19
|
-
y1 = 1
|
20
|
-
temp_phi = phi
|
21
|
-
|
22
|
-
while e > 0:
|
23
|
-
temp1 = temp_phi // e
|
24
|
-
temp2 = temp_phi - temp1 * e
|
25
|
-
temp_phi = e
|
26
|
-
e = temp2
|
27
|
-
|
28
|
-
x = x2 - temp1 * x1
|
29
|
-
y = d - temp1 * y1
|
30
|
-
|
31
|
-
x2 = x1
|
32
|
-
x1 = x
|
33
|
-
d = y1
|
34
|
-
y1 = y
|
35
|
-
|
36
|
-
if temp_phi == 1:
|
37
|
-
return d + phi
|
38
|
-
|
39
|
-
e = random.randint(1, phi)
|
40
|
-
while gcd(e, phi) != 1: # 直接调用模块级 gcd 函数
|
41
|
-
e = random.randint(1, phi)
|
42
|
-
|
43
|
-
d = multiplicative_inverse(e, phi)
|
44
|
-
return (e, n), (d, n)
|
45
|
-
|
46
|
-
def rsa_encrypt(public_key: Tuple[int, int], plaintext: int) -> int:
|
47
|
-
"""
|
48
|
-
使用 RSA 公钥加密数据。
|
49
|
-
:param public_key: 公钥 (e, n)
|
50
|
-
:param plaintext: 明文数据
|
51
|
-
:return: 密文数据
|
52
|
-
"""
|
53
|
-
e, n = public_key
|
54
|
-
return pow(plaintext, e, n)
|
55
|
-
|
56
|
-
def rsa_decrypt(private_key: Tuple[int, int], ciphertext: int) -> int:
|
57
|
-
"""
|
58
|
-
使用 RSA 私钥解密数据。
|
59
|
-
:param private_key: 私钥 (d, n)
|
60
|
-
:param ciphertext: 密文数据
|
61
|
-
:return: 明文数据
|
62
|
-
"""
|
63
|
-
d, n = private_key
|
64
|
-
return pow(ciphertext, d, n)
|
65
|
-
|
66
5
|
def sha3_256(input_string: str) -> str:
|
67
6
|
"""
|
68
7
|
使用 SHA3-256 算法加密字符串。
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.2
|
2
2
|
Name: mlymath
|
3
|
-
Version: 0.3.
|
3
|
+
Version: 0.3.0b2
|
4
4
|
Summary: 一个用于数学计算的Python库
|
5
5
|
Author: mly
|
6
6
|
Classifier: Programming Language :: Python :: 3
|
@@ -46,10 +46,13 @@ Dynamic: summary
|
|
46
46
|
- min(\*nums, n): 找到多个数中第 N 小的数, 输入n时请注意使用n = [num]
|
47
47
|
- list_min_index(nums, n): 找到数组中第 N 小的数的索引
|
48
48
|
- min_index(\*nums, n): 找到多个数中第 N 小的数的索引, 输入n时请注意使用n = [num]
|
49
|
+
- math_help() -> None: 显示math帮助信息
|
50
|
+
- mlymath.RSA RSA
|
49
51
|
- get_rsa_keys(p: int, q: int) -> Tuple[Tuple[int, int], Tuple[int, int]]: 生成 RSA 公钥和私钥
|
50
52
|
- rsa_encrypt(public_key: Tuple[int, int], plaintext: int) -> int: 使用 RSA 公钥加密数据
|
51
53
|
- rsa_decrypt(private_key: Tuple[int, int], ciphertext: int) -> int: 使用 RSA 私钥解密数据
|
52
|
-
-
|
54
|
+
- RSA_help(): 显示帮助信息
|
55
|
+
|
53
56
|
- mlymath.sort 排序模块
|
54
57
|
- quick_sort: 使用快速排序算法对数组进行排序。可通过 reverse 参数控制排序顺序(默认正序)
|
55
58
|
- shell_sort: 使用希尔排序算法对数组进行排序。可通过 reverse 参数控制排序顺序(默认正序)
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
RSA/RSA.py,sha256=5nFVY7nf96JvrMBV_dosCocMPlgDA78gqBrRwAsIgVQ,2166
|
2
|
+
RSA/__init__.py,sha256=gXU7sVoEqBEa3wexZBCvZKGW4z0oz_x3xaFu85FypBY,183
|
3
|
+
math/__init__.py,sha256=op_Ajek3ZYUe8pkDiffT0Oj1Gr-8V6lq2OWYn1Q114k,972
|
4
|
+
math/math.py,sha256=n-9O2ofZf_-O0WLh-5fBkYTC0GZzn55l06bd6QeQc2A,14091
|
3
5
|
mod/__init__.py,sha256=Xeg0qesxAdTNaXqP0gdNenF4AJx199soFh3_nIfevBY,231
|
4
6
|
mod/mod.py,sha256=zTy3a6Ac1hzQM3An4m0PosjzxLC2J83hR4OBZfF0Y6k,977
|
5
7
|
morse/__init__.py,sha256=PGiYLAGjK5B0uVJpHSYojCQ_5H-bH4ISWvdJ7RFRujI,131
|
@@ -8,7 +10,7 @@ puzzle/__init__.py,sha256=BbamD6wQAR-G8dQZ34gu5wbeC8QRamps_R1XwwTaAFs,132
|
|
8
10
|
puzzle/puzzle.py,sha256=28i4g86a_LBtmDU26dNE8bHyru5yar2eTCuD2hCohYg,3640
|
9
11
|
sort/__init__.py,sha256=Y1JTa7Be89khaACYQi7vvYq8suTrgOl60DUc-2LHMww,456
|
10
12
|
sort/sort.py,sha256=J3fqraNWMWIx7TaV9j9VMNEEpVyu2IeNz5lMuEoRnbU,6616
|
11
|
-
mlymath-0.3.
|
12
|
-
mlymath-0.3.
|
13
|
-
mlymath-0.3.
|
14
|
-
mlymath-0.3.
|
13
|
+
mlymath-0.3.0b2.dist-info/METADATA,sha256=32A9fnuI49sxx1VauX_TfX44q84xwE49JBCw0frhgAU,5578
|
14
|
+
mlymath-0.3.0b2.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
15
|
+
mlymath-0.3.0b2.dist-info/top_level.txt,sha256=pNNNbvxhomGELmcK4fAMvphKmtqQivc-G5DDJ6V-b58,31
|
16
|
+
mlymath-0.3.0b2.dist-info/RECORD,,
|
File without changes
|