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
| def judgePrimeNumber(num): sqrtResult = int(num **0.5) for i in range(2, sqrtResult + 1): if num % i == 0: return False return True
def judgeCoPrime(a, b): def maxCommonFactor(m, n): result = 0 while m % n > 0: result = m % n m = n n = result return result if maxCommonFactor(a, b) == 1: return True return False
def getInverse(a, b): def extGcd(a_, b_, arr): if b_ == 0: arr[0] = 1 arr[1] = 0 return a_ g = extGcd(b_, a_ % b_, arr) t = arr[0] arr[0] = arr[1] arr[1] = t - int(a_ / b_) * arr[1] return g arr = [0,1,] gcd = extGcd(a, b, arr) if gcd == 1: return (arr[0] % b + b) % b else: return -1
def cerateKey(p, q): n = p * q n_Euler = (p - 1) * (q - 1) while True: e = int(input("选择公钥e(1 < e < %d 且e与%d互质):" %(n_Euler, n_Euler))) if 1 < e < n_Euler and judgeCoPrime(e, n_Euler): break d = getInverse(e, n_Euler) return n, e, d
def encrypt(n, e, plaintext): plaintextList = [] ciphertextList = [] i = 0 while i < len(plaintext): j = len(str(n)) while True: if int(plaintext[i:(i + j)]) < n: plaintextList.append(int(plaintext[i:(i + j)])) i += j break j -= 1 for item in plaintextList: cipherText = item ** e % n ciphertextList.append(cipherText) return ciphertextList
def decrypt(d, n, ciphertextList): plaintext = "" plaintextList = [] for item in ciphertextList: plaintext += str((item ** d % n)) return plaintext
def inputData(): while True: p = int(input("输入p(素数):")) if judgePrimeNumber(p): break while True: q = int(input("输入q(素数):")) if judgePrimeNumber(q): break return p, q
if __name__ == "__main__": while True: print("—————RSA算法—————") choice = input("1、加密 2、解密\n请选择:") if choice == "1": p, q = inputData() n, e, d = cerateKey(p, q) print("————————————————————") print("| 公钥n:%d | 公钥e:%d | 私钥d:%d |" % (n, e, d)) print("————————————————————") massage = input("输入明文:") ciphertextList = encrypt(n, e, massage) print("加密结果:", ciphertextList) elif choice == "2": ciphertextList = list(map(int, list(input("输入密文序列:").split(",")))) n = int(input("输入公钥n:")) d = int(input("输入私钥d:")) plaintext = decrypt(d, n, ciphertextList) print("解密结果:", plaintext)
|