mlymath 0.1.0__py3-none-any.whl → 0.1.1__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.
math/__init__.py CHANGED
@@ -27,6 +27,9 @@ 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,
30
33
  math_help
31
34
  )
32
35
 
@@ -59,5 +62,8 @@ __all__ = [
59
62
  'max_index',
60
63
  'list_min_index',
61
64
  'min_index',
65
+ 'get_rsa_key',
66
+ 'rsa_encrypt',
67
+ 'rsa_decrypt',
62
68
  'math_help'
63
69
  ]
math/math.py CHANGED
@@ -1,4 +1,67 @@
1
- import hashlib # 新增:用于 SHA-256 加密
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)
2
65
 
3
66
  def sha3_256(input_string: str) -> str:
4
67
  """
@@ -370,7 +433,7 @@ def math_help():
370
433
  print("- euclidean_d(point1, point2, precision=None): 计算两个点之间的欧几里得距离,可指定小数位数")
371
434
  print("- matrix_add(matrix1, matrix2): 计算两个矩阵的加法")
372
435
  print("- matrix_sub(matrix1, matrix2): 计算两个矩阵的减法")
373
- print("- matrix_mu(matrix1, matrix2): 计算两个矩阵的乘法")
436
+ print("- matrix_mul(matrix1, matrix2): 计算两个矩阵的乘法")
374
437
  print("- matrix_transpose(matrix): 计算矩阵的转置")
375
438
  print("- matrix_scalar_mul(matrix, scalar): 计算矩阵与标量的乘法")
376
439
  print("- list_max(nums, n): 找到数组中第 N 大的数")
@@ -381,3 +444,6 @@ def math_help():
381
444
  print("- min(*nums, n): 找到多个数中第 N 小的数, 输入n时请注意使用n = [num]")
382
445
  print("- list_min_index(nums, n): 找到数组中第 N 小的数的索引")
383
446
  print("- min_index(*nums, n): 找到多个数中第 N 小的数的索引, 输入n时请注意使用n = [num]")
447
+ print("- get_rsa_keys(p: int, q: int) -> Tuple[Tuple[int, int], Tuple[int, int]]: 生成 RSA 公钥和私钥")
448
+ print("- rsa_encrypt(public_key: Tuple[int, int], plaintext: int) -> int: 使用 RSA 公钥加密数据")
449
+ print("- rsa_decrypt(private_key: Tuple[int, int], ciphertext: int) -> int: 使用 RSA 私钥解密数据")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: mlymath
3
- Version: 0.1.0
3
+ Version: 0.1.1
4
4
  Author: mly
5
5
  Classifier: Programming Language :: Python :: 3
6
6
  Classifier: License :: OSI Approved :: MIT License
@@ -0,0 +1,10 @@
1
+ math/__init__.py,sha256=2Q6NjcDyT4wb5_UU11bXeVcjZ4hsCN7VnUbei8DcHB4,1086
2
+ math/math.py,sha256=xuRLXB2nqEEpnFLLGLgw-GuBV-GDfze3TlR2TgX9L2A,15650
3
+ morse/__init__.py,sha256=PGiYLAGjK5B0uVJpHSYojCQ_5H-bH4ISWvdJ7RFRujI,131
4
+ morse/morse.py,sha256=SP6AdGNC5LDK-FW6kZ69X5PiHkCOpKVp8ULddanIqDI,2484
5
+ sort/__init__.py,sha256=Y1JTa7Be89khaACYQi7vvYq8suTrgOl60DUc-2LHMww,456
6
+ sort/sort.py,sha256=9iSiS8pxOP7K11t1Kd0iVYEN6k4NaJBN0wEdVF0pR-E,6555
7
+ mlymath-0.1.1.dist-info/METADATA,sha256=g8LZdrjmkpRwybhP5H12hjAiJ6p8obEIkMYzuGcmxPA,304
8
+ mlymath-0.1.1.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
9
+ mlymath-0.1.1.dist-info/top_level.txt,sha256=D1tgdXw9RQ7CHuKLWcH6JfmEr28PVv2I3JN2vrNXivE,16
10
+ mlymath-0.1.1.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- math/__init__.py,sha256=gIUmq5fEIyJJzq0bve9YqYs53EJImUJpiF2EBunI_Bg,972
2
- math/math.py,sha256=PGgeKNkdEFGOTN7YOx7DLVfx6rvGy8vIiEiaQQJksM0,13685
3
- morse/__init__.py,sha256=PGiYLAGjK5B0uVJpHSYojCQ_5H-bH4ISWvdJ7RFRujI,131
4
- morse/morse.py,sha256=SP6AdGNC5LDK-FW6kZ69X5PiHkCOpKVp8ULddanIqDI,2484
5
- sort/__init__.py,sha256=Y1JTa7Be89khaACYQi7vvYq8suTrgOl60DUc-2LHMww,456
6
- sort/sort.py,sha256=9iSiS8pxOP7K11t1Kd0iVYEN6k4NaJBN0wEdVF0pR-E,6555
7
- mlymath-0.1.0.dist-info/METADATA,sha256=wPf289ak3YNJvOQDi_avTUArna6ukd-Bs7ClZQdo11Y,304
8
- mlymath-0.1.0.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
9
- mlymath-0.1.0.dist-info/top_level.txt,sha256=D1tgdXw9RQ7CHuKLWcH6JfmEr28PVv2I3JN2vrNXivE,16
10
- mlymath-0.1.0.dist-info/RECORD,,