1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
| from typing import Tuple
class RSAKey: class RSAParams: def __init__(self, p, q, N, phiN, e, d) -> None: self.p = p self.q = q self.N = N self.phiN = phiN self.e = e self.d = d
def __str__(self) -> str: return f"""{{ p: {self.p}\n q: {self.q}\n N: {self.N}\n phiN: {self.phiN}\n e: {self.e}\n d: {self.d} }}"""
def __init__(self) -> None: self.params = self.__generateRSAParams()
def getPublicKey(self) -> Tuple[int, int]: """get public key
Returns: Tuple[int, int]: (N, e) """ return (self.params.N, self.params.e)
def getPrivateKey(self) -> Tuple[int, int]: """get private key
Returns: Tuple[int, int]: (N, d) """ return (self.params.N, self.params.d)
def __generateRSAParams(self) -> RSAParams: """Generate parameters for RSA
Returns: RSAParams: instance of RSAParams """ from Crypto.Util import number p: int = number.getPrime(476) q: int = number.getPrime(476)
while q == p: q: int = number.getPrime(10)
N: int = p * q
phiN: int = (p - 1) * (q - 1)
e: int = number.getPrime(16) while phiN % e == 0: e: int = number.getPrime(16)
_, d, _ = self.__exgcd(e, phiN)
if d < 0: d = d + phiN
return self.RSAParams(p, q, N, phiN, e, d)
def __exgcd(self, a, b): """扩展欧几里得算法
Args: a (int): a b (int): b
Returns: int: (gcd, x, y) """ ri: int = a rj: int = b si: int = 1 sj: int = 0 ti: int = 0 tj: int = 1
while rj != 0: qi = ri // rj
rtemp = rj rj = ri - qi * rj ri = rtemp
stemp = sj sj = si - qi * sj si = stemp
ttemp = tj tj = ti - qi * tj ti = ttemp
return ri, si, ti
def pow_mod(a: int, b: int, m: int) -> int: """fast power module
Args: a : a ^ b % m b : a ^ b % m m : a ^ b % m
Returns: int: a ^ b % m """ if b == 0: return 1 res = pow_mod(a, b // 2, m) if b % 2 == 0: return res * res % m else: return res * res * (a % m) % m
def encrypt(m, publicKey) -> int: """encrypt message using public key
Args: m (int): origin message m publicKey (Tuple[int, int]): (N, e)
Returns: int: encrypted message c """ N, e = publicKey c = pow_mod(m, e, N) return c
def decrypt(c, privateKey) -> int: """decrypt message using private key
Args: c (int): encrypted message c privateKey (Tuple[int, int]): (N, d)
Returns: int: origin message m """ N, d = privateKey m = pow_mod(c, d, N) return m
def main(): rsakey = RSAKey() publicKey = rsakey.getPublicKey() privateKey = rsakey.getPrivateKey()
print(f"publicKey: {publicKey}") print(f"privateKey: {privateKey}")
message = 123456789 encryptedMessage = encrypt(message, publicKey) decryptedMessage = decrypt(encryptedMessage, privateKey)
print(f"origin message: {message}") print(f"encrypted message: {encryptedMessage}") print(f"decrypted message: {decryptedMessage}")
if __name__ == '__main__': main()
|