任意選取兩個不一樣的大素數p和q計算乘積php
任意選取一個大整數e,知足html
,整數e用作加密鑰(注意:e的選取是很容易的,例如,全部大於p和q的素數均可用)python
肯定的解密鑰d,知足算法
,即api
是一個任意的整數;因此,若知道e和ui
,則很容易計算出d ;加密
公開整數n和e,祕密保存d.net
將明文m(m<n是一個整數)加密成密文c,加密算法爲code
將密文c解密爲明文m,解密算法爲htm
然而只根據n和e(注意:不是p和q)要計算出d是不可能的。所以,任何人均可對明文進行加密,但只有受權用戶(知道d)纔可對密文解密。
n=833810193564967701912362955539789451139872863794534923259743419423089229206473091408403560311191545764221310666338878019 e=65537
p = 863653476616376575308866344984576466644942572246900013156919 q = 965445304326998194798282228842484732438457170595999523426901
from Crypto.PublicKey import RSA import gmpy2 import rsa import base64 f = open("key.pub","rb") pu = RSA.importKey(f.read()) #https://www.dlitz.net/software/pycrypto/api/current/Crypto.PublicKey.RSA-module.html n = pu.n e = pu.e #print(n,e) #quit() p = 863653476616376575308866344984576466644942572246900013156919 q = 965445304326998194798282228842484732438457170595999523426901 #http://factordb.com/index.php?query= fn = (p - 1) * (q - 1) d = int(gmpy2.invert(e,fn)) #https://gmpy2.readthedocs.io/en/latest/intro.html pri = rsa.PrivateKey(n,e,d,p,q) #https://stuvel.eu/files/python-rsa-doc/index.html with open("flag.b64","r") as fl: data = fl.read() datas = base64.b64decode(data) m = rsa.decrypt(datas,pri) print(m.decode("ascii"))