RSA基本知识
这里我只列出几个核心点:
两把钥匙
- 公钥:(n, e) //用于加密
- 密钥:(n, d) //用于解密
构造过程
- 随机生成了一些非常非常大的整数,并用Miller-Rabin算法检测它们是不是质数,直到找到两个大质数——
和
。
- 计算两个质数的乘积
- 计算
//
- 构造一个比1大、比φ(n)小、不等于
或
的整数e。
- 求出e对于φ(n)的乘法逆元d,也就是说ed ≡ 1(mod φ(n)),也就是说ed=kφ(n)+1
具体可以参考:
如何深入浅出地讲解RSA密码?www.zhihu.com![v2-a2237c671d07d26fd970b11a310e0057_180x120.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=9dcbf8a5-6e2f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-a2237c671d07d26fd970b11a310e0057_180x120.jpg)
RSA私钥和公钥文件格式
![v2-0a61e2dce2066ccb05d60d14d8d65291_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=9dcbf8a5-6e2f-eb11-8da9-e4434bdf6706&url=https://pic2.zhimg.com/v2-0a61e2dce2066ccb05d60d14d8d65291_b.jpg)
这里只给出PKCS#1的例子
![v2-0339ef8ab8f890b510eb13165eabd6b8_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=9dcbf8a5-6e2f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-0339ef8ab8f890b510eb13165eabd6b8_b.jpg)
![v2-9a5d27a82c8a0438f36a732b1d102f1c_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=9dcbf8a5-6e2f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-9a5d27a82c8a0438f36a732b1d102f1c_b.jpg)
相关工具
在线工具
公钥分解:
RSA公私钥分解 Exponent、Modulus,Rsa公私钥指数、系数(模数)分解--查错网tool.chacuo.net进制转换:
进制转换tool.lu![v2-df26dbfaadd41211098d8961dede8ad8_180x120.jpg](http://img-02.proxy.5ce.com/view/image?&type=2&guid=9dcbf8a5-6e2f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-df26dbfaadd41211098d8961dede8ad8_180x120.jpg)
n分解:
factordb.comfactordb.com本地工具
RsaCtfTool:(kali下下载即可使用)
git clone https://github.com/Ganapati/RsaCtfTool.git
cd RsaCtfTool
自己改造的工具
前置条件:
安装libnum
git clone hellman/libnum
cd libnum
python setup.py install
通过c、d、n求m:
#!/usr/bin/python
完整版
#!/usr/bin/python
# -*- coding:utf8 -
from libnum import n2s,s2n
def gcd(a, b): #求最大公约数
if a < b:
a, b = b, a
while b != 0:
temp = a % b
a = b
b = temp
return a
def egcd(a,b): #扩展欧几里得算法
if a==0:
return (b,0,1)
else:
g,y,x=egcd(b%a,a)
return (g,x-(b//a)*y,y)
def modinv(a,m):
g,x,y=egcd(a,m)
if g!=1:
raise Exception('modular inverse does not exist')
else:
return x%m
if __name__ == '__main__':
p =15991846970993213322072626901560749932686325766403404864023341810735319249066370916090640926219079368845510444031400322229147771682961132420481897362843199
q =28805791771260259486856902729020438686670354441296247148207862836064657849735343618207098163901787287368569768472521344635567334299356760080507454640207003
e = 354611102441307572056572181827925899198345350228753730931089393275463916544456626894245415096107834465778409532373187125318554614722599301791528916212839368121066035541008808261534500586023652767712271625785204280964688004680328300124849680477105302519377370092578107827116821391826210972320377614967547827619
d =modinv(e,(p-1)*(q-1))
c =38230991316229399651823567590692301060044620412191737764632384680546256228451518238842965221394711848337832459443844446889468362154188214840736744657885858943810177675871991111466653158257191139605699916347308294995664530280816850482740530602254559123759121106338359220242637775919026933563326069449424391192
n =p*q
m = pow(c,d,n)
print n2s(m)
https://blog.csdn.net/qq_40657585/article/details/84874073
例子1(i春秋 第二届春秋欢乐赛,类型:Crypto题目名称:rsa256)
下载下来有三个文件:
![v2-d1a94c5c520e9372c0c78fb146c4776b_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=9dcbf8a5-6e2f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-d1a94c5c520e9372c0c78fb146c4776b_b.jpg)
public.key内容如下
-----BEGIN PUBLIC KEY-----
MDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhANmelSKWptlg38JQSrpUW5RC1gp7npMK
/0UceOxV1VXrAgMBAAE=
-----END PUBLIC KEY-----
公钥分解
使用工具:http://tool.chacuo.net/cryptrsakeyparse
公钥:(n, e)
模数: D99E952296A6D960DFC2504ABA545B9442D60A7B9E930AFF451C78EC55D555EB
指数: 65537 (0x10001)
![v2-a7516ee86b12ca9b9465aab96d340b00_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=9dcbf8a5-6e2f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-a7516ee86b12ca9b9465aab96d340b00_b.jpg)
N分解
先换成十进制
98432079271513130981267919056149161631892822707167177858831841699521774310891
![v2-25be4228e1f3c3672776c28b5b51a34f_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=9dcbf8a5-6e2f-eb11-8da9-e4434bdf6706&url=https://pic4.zhimg.com/v2-25be4228e1f3c3672776c28b5b51a34f_b.jpg)
试着分解
p:302825536744096741518546212761194311477
q:325045504186436346209877301320131277983
![v2-1cfa04b1effdc3d659e2c392dd4a607c_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=9dcbf8a5-6e2f-eb11-8da9-e4434bdf6706&url=https://pic1.zhimg.com/v2-1cfa04b1effdc3d659e2c392dd4a607c_b.jpg)
恢复消息
p、q换成十六进制
p:e3d213b0a3c9551f9fb1eb8d7c3daf35
q:f4897caaba80236bdc1b59385c4bf49f
./RsaCtfTool.py -n 0xD99E952296A6D960DFC2504ABA545B9442D60A7B9E930AFF451C78EC55D555EB -p 0xe3d213b0a3c9551f9fb1eb8d7c3daf35 -q 0xf4897caaba80236bdc1b59385c4bf49f -e 65537 --uncipherfile encrypted.message1
./RsaCtfTool.py -n 0xD99E952296A6D960DFC2504ABA545B9442D60A7B9E930AFF451C78EC55D555EB -p 0xe3d213b0a3c9551f9fb1eb8d7c3daf35 -q 0xf4897caaba80236bdc1b59385c4bf49f -e 65537 --uncipherfile encrypted.message2
./RsaCtfTool.py -n 0xD99E952296A6D960DFC2504ABA545B9442D60A7B9E930AFF451C78EC55D555EB -p 0xe3d213b0a3c9551f9fb1eb8d7c3daf35 -q 0xf4897caaba80236bdc1b59385c4bf49f -e 65537 --uncipherfile encrypted.message3
![v2-1224096225f3dc7eeadf536c7506ceea_b.jpg](http://img-03.proxy.5ce.com/view/image?&type=2&guid=9dcbf8a5-6e2f-eb11-8da9-e4434bdf6706&url=https://pic3.zhimg.com/v2-1224096225f3dc7eeadf536c7506ceea_b.jpg)
拼接得到结果
flag{3b6d3806-4b2b-11e7-95a0-000c29d7e93d}
Unciphered data :
[b'x00x02Zx80x07xe9xad(txabxbfZx00 flag{3b6d3806-4b2b n']
Unciphered data :
[b'x00x02xd2xb9{nxa0xd9xe5x16x9ex1bxc1LHxa0xdbxecTx00 -11e7-95a0- n']
Unciphered data :
[b'x00x02eNxe6$Cqxf5D+Oxc8xb9mx7f/x00 000c29d7e93d} n']
例子2
门罗矿工:2017第二届广东省强网杯线上赛( 类型:Crypto 题目名称:RSA)zhuanlan.zhihu.com用户登录 - 博客园
http://factordb.com/downloads.php
https://www.freebuf.com/column/148898.html