1. 进入环境
题目给出了一长串字符,我们将其提出来,发现是p,q,e,c,因此猜测可能是RSA相关的解密问题。
查一查RSA的具体操作流程:
2. 代码解题
参考网上的wp,先安装两个包:
pip install libnum
pip install pycryptodome
接着上代码:
import libnum
from Crypto.Util.number import long_to_bytes
p = int(
"0xa6055ec186de51800ddd6fcbf0192384ff42d707a55f57af4fcfb0d1dc7bd97055e8275cd4b78ec63c5d592f567c66393a061324aa2e6a8d8fc2a910cbee1ed9",
16)
q = int(
"0xfa0f9463ea0a93b929c099320d31c277e0b0dbc65b189ed76124f5a1218f5d91fd0102a4c8de11f28be5e4d0ae91ab319f4537e97ed74bc663e972a4a9119307",
16)
e = int(
"0x6d1fdab4ce3217b3fc32c9ed480a31d067fd57d93a9ab52b472dc393ab7852fbcb11abbebfd6aaae8032db1316dc22d3f7c3d631e24df13ef23d3b381a1c3e04abcc745d402ee3a031ac2718fae63b240837b4f657f29ca4702da9af22a3a019d68904a969ddb01bcf941df70af042f4fae5cbeb9c2151b324f387e525094c41",
16)
c = 0x7fe1a4f743675d1987d25d38111fae0f78bbea6852cba5beda47db76d119a3efe24cb04b9449f53becd43b0b46e269826a983f832abb53b7a7e24a43ad15378344ed5c20f51e268186d24c76050c1e73647523bd5f91d9b6ad3e86bbf9126588b1dee21e6997372e36c3e74284734748891829665086e0dc523ed23c386bb520
n = p * q
# invmod就是求mod的逆,invmod(a, n)则返回 1 / a (mod n).
d = libnum.invmod(e, (p - 1) * (q - 1))
m = pow(c, d, n)
string = long_to_bytes(m)
print(string.decode())
python编程之int(h, 16)的用法
只能转换a-f之间
数字不能转,因为本来就是数字!!
如果字符串是a8,那么输出的是什么呢?
是168,为什么呢?
y=int('a8',16)
print(y)
print(int('a1',16))
print(int('1a',16))
168
161
26
libnum.invmod是一个用于计算模反元素的函数。它的作用是计算给定模数下的乘法逆元
模运算
定义模运算符
a mod n
等于a除以n时的余数。
使用“同余”这个术语
a ≡ b (mod n)
举个例子
100 ≡ 34 (mod 11)
也可以写成
100 ≡ 1 (mod 11)
100除以11余1
b被称为a mod n的残差,因为它可以表示为:a = qn + b。
举个例子:
-12 ≡ -5 ≡ 2 ≡ 9 (mod 7)
定义Zn ={0,1,…,n-1}
If a+b ≡ a+c (mod n) then b ≡ c (mod n)
but if ab ≡ ac (mod n) then b ≡ c (mod n) only if a is relatively prime to n。
举个例子:
7 x 11 ≡ 7 x 5 (mod 6) 11 ≡ 5 (mod 6)
– 9 x 3 ≡ 9 x 5 (mod 6) but 3 ! ≡ 5 (mod 6)
描述
Python decode() 方法以 encoding 指定的编码格式解码字符串。默认编码为字符串编码。
语法
decode()方法语法:
str.decode(encoding='UTF-8',errors='strict')
参数
encoding -- 要使用的编码,如"UTF-8"。
errors -- 设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通过 codecs.register_error() 注册的任何值。
返回值
该方法返回解码后的字符串。
实例
以下实例展示了decode()方法的实例:
#!/usr/bin/python
str = "this is string example....wow!!!";
str = str.encode('base64','strict');
print "Encoded String: " + str;
print "Decoded String: " + str.decode('base64','strict')
Output:
-----------------------------------------------------------------
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
Decoded String: this is string example....wow!!!
-----------------------------------------------------------------
幂运算是高更数学的应用学科,是一种关于幂的数学运算。同底数幂相乘,底数不变,指数相加。同底数幂相除,底数不变,指数相减。幂的乘方,底数不变,指数相乘。适用于精确计算领域。
计算机作为精确计算的一种方式,含有大量的幂运算。在python中就有内置函数pow函数表示幂的运算。
1、pow()函数
Python的内置函数,它计算并返回x的y次方的值。
2、语法
pow(x, y, z)
3、参数
x -- 数值表达式。
y -- 数值表达式。
z -- 数值表达式。
4、返回值
返回 x y (x的y次方) 的值。
5、pow()函数解释
pow(x,y):表示x的y次幂。
>>> pow(2,4)
16
>>>
pow(x,y,z):表示x的y次幂后除以z的余数。
>>> pow(2,4,5)
1
>>>
6、示例:pow() 返回两个数值的幂运算值
>>> pow(2,3)
>>> 2**3
>>> pow(2,3,5)
>>> pow(2,3)%5
内置函数pow()实例
>>> help(pow)
Help on built-in function pow in module __builtin__:
pow(...)
pow(x, y[, z]) -> number
With two arguments, equivalent to x**y. With three arguments,
equivalent to (x**y) % z, but may be more efficient (e.g. for longs).
>>>
>>> pow(3,2) # 3**2
9
>>> pow(3,2,4)# (3**2)%4
1