通过公钥解密密文思路(256bits RSA)

p { margin-bottom: 0.25cm; line-height: 120% }
a:link { }

1.分解公钥,分解出n与e:

  1.1使用openssl(红色标记是e与n)

 1 [email protected]:~/download/iscc-ctf/RSA$ openssl rsa -pubin -text -modulus -in public.pem
 2
 3 Public-Key: (256 bit)
 4
 5 Modulus:
 6
 7 00:a4:10:06:de:fd:37:8b:73:95:b4:e2:eb:1e:c9:
 8
 9 bf:56:a6:1c:d9:c3:b5:a0:a7:35:28:52:1e:eb:2f:
10
11 b8:17:a7
12
13 Exponent: 65537 (0x10001)                #e
14
15 Modulus=A41006DEFD378B7395B4E2EB1EC9BF56A61CD9C3B5A0A73528521EEB2FB817A7 #n
16
17 writing RSA key
18
19 -----BEGIN PUBLIC KEY-----
20
21 MDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAKQQBt79N4tzlbTi6x7Jv1amHNnDtaCn
22
23 NShSHusvuBenAgMBAAE=
24
25 -----END PUBLIC KEY-----
26
27 [email protected]:~/download/iscc-ctf/RSA$

1.2使用脚本

 1 from Crypto.PublicKey import RSA
 2
 3 pub = RSA.importKey(open(‘xxx\public.pem‘).read())
 4
 5 n = long(pub.n)
 6
 7 e = long(pub.e)
 8
 9 print n
10
11 print e

2.使用msieve来对n来分解因式p、q:(红色标记部分)

 1 [email protected]:~/download/iscc-ctf/RSA$ msieve 0XA41006DEFD378B7395B4E2EB1EC9BF56A61CD9C3B5A0A73528521EEB2FB817A7 -v
 2
 3
 4 Msieve v. 1.54 (SVN 1009)
 5
 6 Wed May 31 17:02:38 2017
 7
 8 random seeds: 31130210 1225946d
 9
10 factoring 74207624142945242263057035287110983967646020057307828709587969646701361764263 (77 digits)
11
12 no P-1/P+1/ECM available, skipping
13
14 commencing quadratic sieve (77-digit input)
15
16 using multiplier of 7
17
18 using generic 32kb sieve core
19
20 sieve interval: 12 blocks of size 32768
21
22 processing polynomials in batches of 17
23
24 using a sieve bound of 921409 (36471 primes)
25
26 using large prime bound of 92140900 (26 bits)
27
28 using trial factoring cutoff of 26 bits
29
30 polynomial ‘A‘ values have 10 factors
31
32 restarting with 19759 full and 186503 partial relations
33
34
35 36750 relations (19759 full + 16991 combined from 186503 partial), need 36567
36
37 sieving complete, commencing postprocessing
38
39 begin with 206262 relations
40
41 reduce to 51619 relations in 2 passes
42
43 attempting to read 51619 relations
44
45 recovered 51619 relations
46
47 recovered 38442 polynomials
48
49 attempting to build 36750 cycles
50
51 found 36750 cycles in 1 passes
52
53 distribution of cycle lengths:
54
55 length 1 : 19759
56
57 length 2 : 16991
58
59 largest cycle: 2 relations
60
61 matrix is 36471 x 36750 (5.3 MB) with weight 1099597 (29.92/col)
62
63 sparse part has weight 1099597 (29.92/col)
64
65 filtering completed in 4 passes
66
67 matrix is 24901 x 24965 (4.0 MB) with weight 837672 (33.55/col)
68
69 sparse part has weight 837672 (33.55/col)
70
71 saving the first 48 matrix rows for later
72
73 matrix includes 64 packed rows
74
75 matrix is 24853 x 24965 (2.6 MB) with weight 610638 (24.46/col)
76
77 sparse part has weight 441218 (17.67/col)
78
79 commencing Lanczos iteration
80
81 memory use: 2.7 MB
82
83 lanczos halted after 394 iterations (dim = 24853)
84
85 recovered 18 nontrivial dependencies
86
87 p39 factor: 258631601377848992211685134376492365269------------------->p
88
89 p39 factor: 286924040788547268861394901519826758027------------------->q
90
91 elapsed time 00:00:10
92
93 [email protected]:~/download/iscc-ctf/RSA$

3.使用脚本来生成私钥文件(修改红色部分)

 1 import math
 2
 3 import sys
 4
 5 from Crypto.PublicKey import RSA
 6
 7
 8 keypair = RSA.generate(1024)
 9
10
11 keypair.p = 258631601377848992211685134376492365269           #msieve求解的p
12
13 keypair.q = 286924040788547268861394901519826758027         #msieve求解的q     
14
15 keypair.e = 65537                                             #分解出的e
16
17
18 keypair.n = keypair.p * keypair.q
19
20 Qn = long((keypair.p-1) * (keypair.q-1))
21
22
23 i = 1
24
25 while (True):
26
27 x = (Qn * i ) + 1
28
29 if (x % keypair.e == 0):
30
31 keypair.d = x / keypair.e
32
33 break
34
35 i += 1
36
37
38 private = open(‘private.pem‘,‘w‘)
39
40 private.write(keypair.exportKey())
41
42 private.close()

4.使用生成的privete.pem私钥文件对密文解密

 

1  openssl rsautl -decrypt -in flag.enc -inkey private.pem -out flag

附录:

1.linux下安装msieve

sourceforgot上下载软件源代码包:

https://sourceforge.net/projects/msieve/

解压后

 1 $ cd msieve-code/
 2
 3 $make
 4
 5 to build:
 6
 7 make all
 8
 9 add ‘WIN=1 if building on windows
10
11 add ‘WIN64=1 if building on 64-bit windows
12
13 add ‘ECM=1‘ if GMP-ECM is available (enables ECM)
14
15 add ‘CUDA=1‘ for Nvidia graphics card support
16
17 add ‘MPI=1‘ for parallel processing using MPI
18
19 add ‘BOINC=1‘ to add BOINC wrapper
20
21 add ‘NO_ZLIB=1‘ if you don‘t have zlib
22
23 $ make all ECM=1 #根据自己的配置进行选择

应该会报错gmp.h不存在,安装高精度数学库就可以啦。

2.linux安装gmp(高精度数学库) 

环境:ubuntu
17.04

源代码:https://gmplib.org/

下载gmp-5.0.1的源代码,解压至gmp-5.0.1目录。

su切换至超级用户权限。

./configure
--prefix=/usr  --enable-cxx

提示:

checking
for suitable m4… configure: error:

No
usable m4 in $PATH or /usr/5bin (see config.log for
reasons).

根据提示查看config.log日志文件,发现文件太大,何处找原因呢?

没有办法,直接google搜索上面的英文提示。

居然马上就找到了资料解决这个问题,原来是缺少m4软件包。

查了一下m4是一个通用的宏处理器,由Brian
Kernighan 和Dennis
Ritchie设计。

apt-get
install build-essential
m4

安装完毕,其中的build-essential是ubuntu下用来解决安装g++/gcc编译环境依赖关系的软件包。

开始编译,安装gmp数学库。

1 ./configure --prefix=/usr  --enable-cxx
2 make
3 make check
4 make install 

参考资料:

  1.256-bitRSA破解-实验吧

  2.[翻译]初学者向导―GGNFS和MSIEVE分解因数-『外文翻译』-看雪安全论坛:http://bbs.pediy.com/thread-156206.htm

  3.ubuntu10.4下安装和使用GMP高精度数学库:http://blog.csdn.net/bingqingsuimeng/article/details/12748341

时间: 2024-08-28 10:34:05

通过公钥解密密文思路(256bits RSA)的相关文章

RSA,JAVA私钥加密,C#公钥解密

做这个东西在坑里爬了3天才爬出来,记录下供园友参考.C#程序员一枚,项目需要和Java做数据交互,对方甩了段密文和一个CER证书给我,然后我要对其密文进行解密. RSA 非对称加密,对方用私钥加密,我用公钥解密.关于证书的一点说明:证书类型有两种 .pfx 和 .cer ,其中 .pfx 证书既包含公钥也包含私钥, 而 .cer 证书只包含公钥. C#默认RSA只支持公钥加密,私钥解密.而现在的需求正好相反,因此想要直接用C#内置加密类肯定是行不通的.而且C#和Java的RSA加密并不互通.经过

RSA不对称加密,公钥加密私钥解密,私钥加密公钥解密

RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作. RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一.RSA的安全性依赖于大数的因子分解,但并没有从理论上证明破译RSA的难度与大数分解难度等价. .NET提供常用的加密算法类,支持RSA的类是RSACryptoServiceProvider(命名空间:System.Security.Cryptography),但只支持公钥加密,私钥解密.RSACr

基于私钥加密公钥解密的RSA算法C#实现

RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作. RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一.RSA的安全性依赖于大数的因子分解,但并没有从理论上证明破译RSA的难度与大数分解难度等价.    RSA的安全性依赖于大数分解.公钥和私钥都是两个大素数( 大于 100个十进制位)的函数.据猜测,从一个密钥和密文推断出明文的难度等同于分解两个大素数的积.     密钥对的产生.选择两个大素数,p

基于私钥加密公钥解密的RSA算法C#实现方法

本文实例讲述了基于私钥加密公钥解密的RSA算法C#实现方法,是一种应用十分广泛的算法.分享给大家供大家参考之用.具体方法如下: 一.概述 RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作. RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一.RSA的安全性依赖于大数的因子分解,但并没有从理论上证明破译RSA的难度与大数分解难度等价. RSA的安全性依赖于大数分解.公钥和私钥都是两个大素数( 大于 1

RSA加解密 私钥加密公钥解密 私加公解 && C++ 调用openssl库 的代码实例

前提:秘钥长度=1024 ============================================== 对一片(117字节)明文加密  私加 ============================================== // 私钥加密 std::string rsa_pri_encrypt(const std::string &clearText, std::string &pubKey) { std::string strRet; BIO *keybio

C# 与JAVA 的RSA 加密解密交互,互通,C#使用BouncyCastle来实现私钥加密,公钥解密的方法

因为C#的RSA加密解密只有公钥加密,私钥解密,没有私钥加密,公钥解密.在网上查了很久也没有很好的实现.BouncyCastle的文档少之又少.很多人可能会说,C#也是可以的,通过Biginteger开源类来实现,不过那个是有一个文章,不过他加密出来的是16进制结果的.根本不能和JAVA互通.连加密出来的都不和C#原生的加密出来的结果格式一样.所以还是没有好的解决方法. 接下来还是不断的找资料,找方法.找朋友找同事.个个都找.问题是有的,方法也是有的,所以总结各路大神之后写了这个类.实现了私钥加

RSA公钥加密—私钥解密&私钥加密—公钥解密&私钥签名—公钥验证签名

关于RSA算法,前面有介绍,点击打开链接. 这里直接有实现. 代码太多就不直接贴了,免积分下载. http://download.csdn.net/detail/acmjk/7310847 RSA公钥加密-私钥解密&私钥加密-公钥解密&私钥签名-公钥验证签名,布布扣,bubuko.com

C# RSA加密、解密、加签、验签、支持JAVA格式公钥私钥、PEM格式公钥私钥、.NET格式公钥私钥 -变态模式【支持私钥加密,公钥解密】(二)

RSA变态模式:[私钥加密,公钥解密] 一般这种写法都是JAVA弄的..NET原生不支持.为啥,我也不清楚,大概是因为安全性问题吧,毕竟公钥是人人都可是持有的.私钥只有自己拥有. 对接注意事项:https://www.cnblogs.com/kevin860/p/9557845.html 一般方法请看:https://www.cnblogs.com/kevin860/p/9557845.html 签名一直都是[私钥加签.公钥验签]只为证明该消息是你发出来的. 这里使用了BouncyCastle1

Delphi RSA加解密【 (RSA公钥加密,私钥解密)、(RSA私钥加密,公钥解密)、MD5加密、SHA加密】

作者QQ:(648437169) delphi RSA加解密 [Delphi RSA加解密]支持 (RSA公钥加密,私钥解密).(RSA私钥加密,公钥解密).MD5加密.SHA1加密.SHA224加密.SHA256加密.SHA384加密.SHA512加密 原文地址:https://www.cnblogs.com/zhimamaigua/p/11003504.html