日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

关于Crypto.PublicKey.RSA,在generate后无法被赋值问题

發布時間:2024/9/30 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于Crypto.PublicKey.RSA,在generate后无法被赋值问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

最近做題的時候有遇到一個問題,就是去利用求解出來的rsa的私鑰去生成pem格式的私鑰文件。找到網上用的都是先令RSA去generate,然后分別給RSA的n,e,d,p,q參數賦值,再導出私鑰。所以在想這種情況下怎么解決。

解決方法

現在使用Crypto.PublicKey.RSA模塊時,這么做將報錯

Exception has occurred: AttributeError can't set attribute

意思就是我們不能去給他賦值了。
然后我們去找到Crypto.PublicKey.RSA模塊的代碼

發現可以通過構造函數傳入

這邊是Crypto.PublicKey.RSA模塊的構造函數實現。

def construct(rsa_components, consistency_check=True):r"""Construct an RSA key from a tuple of valid RSA components.The modulus **n** must be the product of two primes.The public exponent **e** must be odd and larger than 1.In case of a private key, the following equations must apply:.. math::\begin{align}p*q &= n \\e*d &\equiv 1 ( \text{mod lcm} [(p-1)(q-1)]) \\p*u &\equiv 1 ( \text{mod } q)\end{align}Args:rsa_components (tuple):A tuple of integers, with at least 2 and nomore than 6 items. The items come in the following order:1. RSA modulus *n*.2. Public exponent *e*.3. Private exponent *d*.Only required if the key is private.4. First factor of *n* (*p*).Optional, but the other factor *q* must also be present.5. Second factor of *n* (*q*). Optional.6. CRT coefficient *q*, that is :math:`p^{-1} \text{mod }q`. Optional.consistency_check (boolean):If ``True``, the library will verify that the provided componentsfulfil the main RSA properties.Raises:ValueError: when the key being imported fails the most basic RSA validity checks.Returns: An RSA key object (:class:`RsaKey`)."""class InputComps(object):passinput_comps = InputComps()for (comp, value) in zip(('n', 'e', 'd', 'p', 'q', 'u'), rsa_components):setattr(input_comps, comp, Integer(value))n = input_comps.ne = input_comps.eif not hasattr(input_comps, 'd'):key = RsaKey(n=n, e=e)else:d = input_comps.dif hasattr(input_comps, 'q'):p = input_comps.pq = input_comps.qelse:# Compute factors p and q from the private exponent d.# We assume that n has no more than two factors.# See 8.2.2(i) in Handbook of Applied Cryptography.ktot = d * e - 1# The quantity d*e-1 is a multiple of phi(n), even,# and can be represented as t*2^s.t = ktotwhile t % 2 == 0:t //= 2# Cycle through all multiplicative inverses in Zn.# The algorithm is non-deterministic, but there is a 50% chance# any candidate a leads to successful factoring.# See "Digitalized Signatures and Public Key Functions as Intractable# as Factorization", M. Rabin, 1979spotted = Falsea = Integer(2)while not spotted and a < 100:k = Integer(t)# Cycle through all values a^{t*2^i}=a^kwhile k < ktot:cand = pow(a, k, n)# Check if a^k is a non-trivial root of unity (mod n)if cand != 1 and cand != (n - 1) and pow(cand, 2, n) == 1:# We have found a number such that (cand-1)(cand+1)=0 (mod n).# Either of the terms divides n.p = Integer(n).gcd(cand + 1)spotted = Truebreakk *= 2# This value was not any good... let's try another!a += 2if not spotted:raise ValueError("Unable to compute factors p and q from exponent d.")# Found !assert ((n % p) == 0)q = n // pif hasattr(input_comps, 'u'):u = input_comps.uelse:u = p.inverse(q)# Build key objectkey = RsaKey(n=n, e=e, d=d, p=p, q=q, u=u)# Verify consistency of the keyif consistency_check:# Modulus and public exponent must be coprimeif e <= 1 or e >= n:raise ValueError("Invalid RSA public exponent")if Integer(n).gcd(e) != 1:raise ValueError("RSA public exponent is not coprime to modulus")# For RSA, modulus must be oddif not n & 1:raise ValueError("RSA modulus is not odd")if key.has_private():# Modulus and private exponent must be coprimeif d <= 1 or d >= n:raise ValueError("Invalid RSA private exponent")if Integer(n).gcd(d) != 1:raise ValueError("RSA private exponent is not coprime to modulus")# Modulus must be product of 2 primesif p * q != n:raise ValueError("RSA factors do not match modulus")if test_probable_prime(p) == COMPOSITE:raise ValueError("RSA factor p is composite")if test_probable_prime(q) == COMPOSITE:raise ValueError("RSA factor q is composite")# See Carmichael theoremphi = (p - 1) * (q - 1)lcm = phi // (p - 1).gcd(q - 1)if (e * d % int(lcm)) != 1:raise ValueError("Invalid RSA condition")if hasattr(key, 'u'):# CRT coefficientif u <= 1 or u >= q:raise ValueError("Invalid RSA component u")if (p * u % q) != 1:raise ValueError("Invalid RSA component u with p")return key

也就是說我們傳入一個元組也就是tuple類型的值就行了。那我們就傳入(n,e,d,p,q)

我這邊給一個我的一個exp

from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP f=open("public.key","r") key=RSA.import_key(f.read()) f.close() e=key.e n=key.nimport base64 from Crypto.Util.number import * import gmpy2 p= 3133337 q=25478326064937419292200172136399497719081842914528228316455906211693118321971399936004729134841162974144246271486439695786036588117424611881955950996219646807378822278285638261582099108339438949573034101215141156156408742843820048066830863814362379885720395082318462850002901605689761876319151147352730090957556940842144299887394678743607766937828094478336401159449035878306853716216548374273462386508307367713112073004011383418967894930554067582453248981022011922883374442736848045920676341361871231787163441467533076890081721882179369168787287724769642665399992556052144845878600126283968890273067575342061776244939 print(p*q==n) f=open("flag.enc","r") c_base64=f.read().strip("\n") c_bytes=base64.b64decode(c_base64) c=bytes_to_long(c_bytes) phi=(p-1)*(q-1) d=gmpy2.invert(e,phi)rsa_components=(n,e,int(d),p,q)arsa=RSA.construct(rsa_components)rsakey = RSA.importKey(arsa.exportKey()) rsakey = PKCS1_OAEP.new(rsakey)decrypted = rsakey.decrypt(c_bytes) print(decrypted)

主要就是從公鑰里面把n和e取出來
然后把n分解出p、q

接著計算私鑰d

然后傳入RSA模塊的構造函數,導出私鑰。利用私鑰對我們的密文進行求解即可

總結

以上是生活随笔為你收集整理的关于Crypto.PublicKey.RSA,在generate后无法被赋值问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产亚洲精品久久久久久777 | 天天干天天色天天 | 久久精品精品 | 成人在线观看亚洲 | 91 在线观看 | 欧美bdsm调教视频 | 成年人免费视频观看 | 亚洲成人看片 | 欧美日韩成人网 | 极品色影视| 国产日韩欧美一区二区东京热 | 国产成人片 | 天堂素人约啪 | 亚洲av综合色区 | 日韩在线观看视频一区二区 | 欧美另类视频在线 | 日韩a级一片| 大陆明星乱淫(高h)小说 | 国产精品av久久久久久无 | 成人黄色片视频 | 精品午夜久久久 | 一级片播放 | 欧美一区不卡 | 亚洲综人 | 波多野结衣视频一区二区 | 国产人妻人伦精品1国产盗摄 | ts人妖在线 | 精品亚洲一区二区三区 | 欧美在线日韩 | 亚洲玖玖玖 | 精国产人伦一区二区三区 | 日韩一级黄 | 加勒比精品在线 | 欧美日韩国产一区二区在线观看 | 国产成人精品女人久久久 | 特黄色大片 | 日本wwwwww| 国产视频一区二区三区在线播放 | 黄色成人在线播放 | 日p免费视频 | 丝袜老师扒开让我了一夜漫画 | 999久久久免费精品国产 | 国内精品卡一卡二卡三 | 中文字幕乱码人妻二区三区 | 中文字幕国产综合 | 一区二区三区在线免费视频 | 午夜精品视频一区二区三区在线看 | 性涩av | 欧美放荡性医生videos | 亚洲精品人人 | 97精品人妻一区二区三区蜜桃 | 国产精品zjzjzj在线观看 | 日日摸天天爽天天爽视频 | 色婷婷综合成人 | 国产91视频播放 | 午夜免费影院 | 精品视频一二区 | 久久综合日本 | 日韩欧美国产网站 | 欧美高清性xxxxhdvideosex | 天堂√ | 97视频免费在线观看 | 欧美视频一区二区三区四区 | 青青操网 | 国产精品一区二区三区四区五区 | 老司机在线观看视频 | 麻豆传媒网站入口 | 国产成人精品一区二区三区在线观看 | 午夜黄色剧场 | 亚洲国产aⅴ成人精品无吗 日韩乱论 | 香蕉免费在线视频 | 丁香八月婷婷 | 欧美有码视频 | 欧美人与物videos另类 | 国产精品91在线观看 | 深夜在线 | 色综合中文字幕 | 国产又粗又猛又爽又黄的网站 | 亚洲啊啊啊啊啊 | 欧美性猛烈 | 丝袜美腿av | 日本天堂在线观看 | 亚洲国产综合在线 | 2019日韩中文字幕 | 久久久国产精品x99av | 激情一区| 爱爱二区| 乱熟女高潮一区二区在线 | 玖玖综合网 | 亚洲综合第一页 | 国产精品无码永久免费不卡 | 人人干97 | 一区二区三区四区在线视频 | 大地资源二中文在线影视免费观看 | 国产丝袜一区二区三区 | 久久精品无码一区二区三区免费 | 一级片a级片 | 免费在线黄色网址 | 超碰啪啪|