密码学---对称加密和非对称加密
✨安装工具openssl,并且生成Alice和bob的各自私钥文件,查看其文件的内容和详细内容!!!
──(nanli㉿kali)-[~]
└─$ sudo su
[sudo] password for nanli:
┌──(root㉿kali)-[/home/nanli]
└─# apt-get install openssl
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
libnsl-dev libtirpc-dev
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
libc-bin libc-dev-bin libc-devtools libc-l10n libc6 libc6-dev libc6-i386 libssl3t64 locales
openssh-client openssh-server openssh-sftp-server
Suggested packages:
glibc-doc libnss-nis libnss-nisplus keychain libpam-ssh monkeysphere ssh-askpass molly-guard ufw
The following packages will be REMOVED:
libssl3
The following NEW packages will be installed:
libssl3t64
The following packages will be upgraded:
libc-bin libc-dev-bin libc-devtools libc-l10n libc6 libc6-dev libc6-i386 locales openssh-client
openssh-server openssh-sftp-server openssl
12 upgraded, 1 newly installed, 1 to remove and 2022 not upgraded.
Need to get 17.6 MB of archives.
After this operation, 432 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Ign:1 http://http.kali.org/kali kali-rolling/main amd64 libc-l10n all 2.38-13
Ign:2 http://http.kali.org/kali kali-rolling/main amd64 libc-devtools amd64 2.38-13
Ign:3 http://http.kali.org/kali kali-rolling/main amd64 libc-dev-bin amd64 2.38-13
Ign:4 http://http.kali.org/kali kali-rolling/main amd64 libc6-dev amd64 2.38-13
Ign:5 http://http.kali.org/kali kali-rolling/main amd64 libc6-i386 amd64 2.38-13
Ign:6 http://http.kali.org/kali kali-rolling/main amd64 locales all 2.38-13
Ign:7 http://http.kali.org/kali kali-rolling/main amd64 libc6 amd64 2.38-13
Ign:8 http://http.kali.org/kali kali-rolling/main amd64 libc-bin amd64 2.38-13
Ign:9 http://http.kali.org/kali kali-rolling/main amd64 openssh-sftp-server amd64 1:9.7p1-7
Ign:10 http://http.kali.org/kali kali-rolling/main amd64 openssh-server amd64 1:9.7p1-7
Ign:11 http://http.kali.org/kali kali-rolling/main amd64 openssh-client amd64 1:9.7p1-7
Ign:12 http://http.kali.org/kali kali-rolling/main amd64 openssl amd64 3.2.2-1
Ign:13 http://http.kali.org/kali kali-rolling/main amd64 libssl3t64 amd64 3.2.2-1
Ign:1 http://http.kali.org/kali kali-rolling/main amd64 libc-l10n all 2.38-13
Ign:2 http://http.kali.org/kali kali-rolling/main amd64 libc-devtools amd64 2.38-13
Ign:3 http://http.kali.org/kali kali-rolling/main amd64 libc-dev-bin amd64 2.38-13
Ign:4 http://http.kali.org/kali kali-rolling/main amd64 libc6-dev amd64 2.38-13
Ign:5 http://http.kali.org/kali kali-rolling/main amd64 libc6-i386 amd64 2.38-13
Ign:6 http://http.kali.org/kali kali-rolling/main amd64 locales all 2.38-13
Ign:7 http://http.kali.org/kali kali-rolling/main amd64 libc6 amd64 2.38-13
Ign:8 http://http.kali.org/kali kali-rolling/main amd64 libc-bin amd64 2.38-13
Ign:9 http://http.kali.org/kali kali-rolling/main amd64 openssh-sftp-server amd64 1:9.7p1-7
Ign:10 http://http.kali.org/kali kali-rolling/main amd64 openssh-server amd64 1:9.7p1-7
Ign:11 http://http.kali.org/kali kali-rolling/main amd64 openssh-client amd64 1:9.7p1-7
Ign:12 http://http.kali.org/kali kali-rolling/main amd64 openssl amd64 3.2.2-1
Ign:13 http://http.kali.org/kali kali-rolling/main amd64 libssl3t64 amd64 3.2.2-1
Ign:1 http://http.kali.org/kali kali-rolling/main amd64 libc-l10n all 2.38-13
Ign:2 http://http.kali.org/kali kali-rolling/main amd64 libc-devtools amd64 2.38-13
Ign:3 http://http.kali.org/kali kali-rolling/main amd64 libc-dev-bin amd64 2.38-13
Ign:4 http://http.kali.org/kali kali-rolling/main amd64 libc6-dev amd64 2.38-13
Ign:5 http://http.kali.org/kali kali-rolling/main amd64 libc6-i386 amd64 2.38-13
Ign:6 http://http.kali.org/kali kali-rolling/main amd64 locales all 2.38-13
Ign:7 http://http.kali.org/kali kali-rolling/main amd64 libc6 amd64 2.38-13
Ign:8 http://http.kali.org/kali kali-rolling/main amd64 libc-bin amd64 2.38-13
Ign:9 http://http.kali.org/kali kali-rolling/main amd64 openssh-sftp-server amd64 1:9.7p1-7
Ign:10 http://http.kali.org/kali kali-rolling/main amd64 openssh-server amd64 1:9.7p1-7
Ign:11 http://http.kali.org/kali kali-rolling/main amd64 openssh-client amd64 1:9.7p1-7
Ign:12 http://http.kali.org/kali kali-rolling/main amd64 openssl amd64 3.2.2-1
Ign:13 http://http.kali.org/kali kali-rolling/main amd64 libssl3t64 amd64 3.2.2-1
Err:1 http://http.kali.org/kali kali-rolling/main amd64 libc-l10n all 2.38-13
Could not connect to http.kali.org:80 (18.211.24.19). - connect (111: Connection refused)
Err:2 http://http.kali.org/kali kali-rolling/main amd64 libc-devtools amd64 2.38-13
Unable to connect to http.kali.org:http:
Err:3 http://http.kali.org/kali kali-rolling/main amd64 libc-dev-bin amd64 2.38-13
Unable to connect to http.kali.org:http:
Err:4 http://http.kali.org/kali kali-rolling/main amd64 libc6-dev amd64 2.38-13
Unable to connect to http.kali.org:http:
Err:5 http://http.kali.org/kali kali-rolling/main amd64 libc6-i386 amd64 2.38-13
Unable to connect to http.kali.org:http:
Err:6 http://http.kali.org/kali kali-rolling/main amd64 locales all 2.38-13
Unable to connect to http.kali.org:http:
Err:7 http://http.kali.org/kali kali-rolling/main amd64 libc6 amd64 2.38-13
Unable to connect to http.kali.org:http:
Err:8 http://http.kali.org/kali kali-rolling/main amd64 libc-bin amd64 2.38-13
Unable to connect to http.kali.org:http:
Err:9 http://http.kali.org/kali kali-rolling/main amd64 openssh-sftp-server amd64 1:9.7p1-7
Unable to connect to http.kali.org:http:
Err:10 http://http.kali.org/kali kali-rolling/main amd64 openssh-server amd64 1:9.7p1-7
Unable to connect to http.kali.org:http:
Err:11 http://http.kali.org/kali kali-rolling/main amd64 openssh-client amd64 1:9.7p1-7
Unable to connect to http.kali.org:http:
Err:12 http://http.kali.org/kali kali-rolling/main amd64 openssl amd64 3.2.2-1
Unable to connect to http.kali.org:http:
Err:13 http://http.kali.org/kali kali-rolling/main amd64 libssl3t64 amd64 3.2.2-1
Unable to connect to http.kali.org:http:
E: Failed to fetch http://http.kali.org/kali/pool/main/g/glibc/libc-l10n_2.38-13_all.deb Could not connect to http.kali.org:80 (18.211.24.19). - connect (111: Connection refused)
E: Failed to fetch http://http.kali.org/kali/pool/main/g/glibc/libc-devtools_2.38-13_amd64.deb Unable to connect to http.kali.org:http:
E: Failed to fetch http://http.kali.org/kali/pool/main/g/glibc/libc-dev-bin_2.38-13_amd64.deb Unable to connect to http.kali.org:http:
E: Failed to fetch http://http.kali.org/kali/pool/main/g/glibc/libc6-dev_2.38-13_amd64.deb Unable to connect to http.kali.org:http:
E: Failed to fetch http://http.kali.org/kali/pool/main/g/glibc/libc6-i386_2.38-13_amd64.deb Unable to connect to http.kali.org:http:
E: Failed to fetch http://http.kali.org/kali/pool/main/g/glibc/locales_2.38-13_all.deb Unable to connect to http.kali.org:http:
E: Failed to fetch http://http.kali.org/kali/pool/main/g/glibc/libc6_2.38-13_amd64.deb Unable to connect to http.kali.org:http:
E: Failed to fetch http://http.kali.org/kali/pool/main/g/glibc/libc-bin_2.38-13_amd64.deb Unable to connect to http.kali.org:http:
E: Failed to fetch http://http.kali.org/kali/pool/main/o/openssh/openssh-sftp-server_9.7p1-7_amd64.deb Unable to connect to http.kali.org:http:
E: Failed to fetch http://http.kali.org/kali/pool/main/o/openssh/openssh-server_9.7p1-7_amd64.deb Unable to connect to http.kali.org:http:
E: Failed to fetch http://http.kali.org/kali/pool/main/o/openssh/openssh-client_9.7p1-7_amd64.deb Unable to connect to http.kali.org:http:
E: Failed to fetch http://http.kali.org/kali/pool/main/o/openssl/openssl_3.2.2-1_amd64.deb Unable to connect to http.kali.org:http:
E: Failed to fetch http://http.kali.org/kali/pool/main/o/openssl/libssl3t64_3.2.2-1_amd64.deb Unable to connect to http.kali.org:http:
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
┌──(root㉿kali)-[/home/nanli]
└─# openssl version
OpenSSL 3.0.10 1 Aug 2023 (Library: OpenSSL 3.0.10 1 Aug 2023)
┌──(root㉿kali)-[/home/nanli]
└─# openssl genrsa -out alice_private.pem 1024
┌──(root㉿kali)-[/home/nanli]
└─# openssl genrsa -out bob_private.pem 1024
┌──(root㉿kali)-[/home/nanli]
└─# head alice_private.pem
-----BEGIN PRIVATE KEY-----
MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKRj+0msYaAy0b0J
QwjctnKQ9VlKRtePVxF1ssbPFJkSrEY/QwQfh/PjJR3p315dCp8gjqmalCiFUFIR
/E4JT+gimn2yrj/nn5TmJ+ndqg1jklUFvpkuoc210rJJ5smes7LzCExFQ2PJ+DTp
PyRortt6/EkSIOcRNUeJgZqLSS0TAgMBAAECgYA07dAhx3PjbZlkQRApUU8ZEh08
85Jdo1RIVdoK8u/R3VZEg+fi8vgGCZOQzxVRP6nE1Gm6p/+eEFZtJkdTJOJp+kLz
CPzdMLT0/WsS3HXBQ52Cw2let/0j6rvNegxzryHqsoZx41iPVLY8zIMqRlbFbku7
xqovNAHyujDaAUGN2QJBANb4FUF087hycvEQaqkP9zF5bpIjdIwqNUibKW9QDlkz
QagfqJtFLiwsUWxEhkGjemru+XWmGjXcS+8WBVACDHUCQQDDxIH+SQfit8Unecm6
BgeSbdhCCFSmNKCRneDvVemukh8B1dE/OZBQGGyMQPZiI7T0nogbERg4IduPgaYe
┌──(root㉿kali)-[/home/nanli]
└─# openssl rsa -in alice_private.pem -noout -text
Private-Key: (1024 bit, 2 primes)
modulus:
00:a4:63:fb:49:ac:61:a0:32:d1:bd:09:43:08:dc:
b6:72:90:f5:59:4a:46:d7:8f:57:11:75:b2:c6:cf:
14:99:12:ac:46:3f:43:04:1f:87:f3:e3:25:1d:e9:
df:5e:5d:0a:9f:20:8e:a9:9a:94:28:85:50:52:11:
fc:4e:09:4f:e8:22:9a:7d:b2:ae:3f:e7:9f:94:e6:
27:e9:dd:aa:0d:63:92:55:05:be:99:2e:a1:cd:b5:
d2:b2:49:e6:c9:9e:b3:b2:f3:08:4c:45:43:63:c9:
f8:34:e9:3f:24:68:ae:db:7a:fc:49:12:20:e7:11:
35:47:89:81:9a:8b:49:2d:13
publicExponent: 65537 (0x10001)
privateExponent:
34:ed:d0:21:c7:73:e3:6d:99:64:41:10:29:51:4f:
19:12:1d:3c:f3:92:5d:a3:54:48:55:da:0a:f2:ef:
d1:dd:56:44:83:e7:e2:f2:f8:06:09:93:90:cf:15:
51:3f:a9:c4:d4:69:ba:a7:ff:9e:10:56:6d:26:47:
53:24:e2:69:fa:42:f3:08:fc:dd:30:b4:f4:fd:6b:
12:dc:75:c1:43:9d:82:c3:69:5e:b7:fd:23:ea:bb:
cd:7a:0c:73:af:21:ea:b2:86:71:e3:58:8f:54:b6:
3c:cc:83:2a:46:56:c5:6e:4b:bb:c6:aa:2f:34:01:
f2:ba:30:da:01:41:8d:d9
prime1:
00:d6:f8:15:41:74:f3:b8:72:72:f1:10:6a:a9:0f:
f7:31:79:6e:92:23:74:8c:2a:35:48:9b:29:6f:50:
0e:59:33:41:a8:1f:a8:9b:45:2e:2c:2c:51:6c:44:
86:41:a3:7a:6a:ee:f9:75:a6:1a:35:dc:4b:ef:16:
05:50:02:0c:75
prime2:
00:c3:c4:81:fe:49:07:e2:b7:c5:27:79:c9:ba:06:
07:92:6d:d8:42:08:54:a6:34:a0:91:9d:e0:ef:55:
e9:ae:92:1f:01:d5:d1:3f:39:90:50:18:6c:8c:40:
f6:62:23:b4:f4:9e:88:1b:11:18:38:21:db:8f:81:
a6:1e:40:42:67
exponent1:
40:44:70:ce:5d:f1:93:3e:1c:20:f7:c0:7b:84:bb:
69:3e:1d:91:71:2a:21:d1:68:96:a9:54:ac:e3:a9:
36:7f:bd:6c:4e:f2:b0:60:f1:59:e1:72:02:45:46:
42:0f:66:c4:db:67:17:06:2e:a5:d5:93:9a:09:e2:
52:b6:1a:15
exponent2:
08:66:5a:1d:8b:0c:a7:91:b3:07:ea:95:d9:41:80:
f9:ab:74:e8:00:6b:81:66:28:ad:96:d4:9b:f0:57:
c2:81:ca:8a:cd:9d:d8:d4:a3:78:8c:53:bc:00:dd:
91:80:6e:b7:40:46:1e:c3:31:02:d7:91:10:2b:43:
f1:70:58:69
coefficient:
04:ba:cf:10:50:75:c3:4f:7a:c1:fd:b7:d0:1e:15:
f2:1c:13:4d:e4:fe:5d:27:64:c1:7b:c3:31:93:7f:
9b:e7:4d:fb:46:58:86:c8:df:10:a7:ab:b3:60:32:
bb:ee:a7:e1:c7:d9:9a:b8:ad:37:44:c0:d1:2f:26:
c7:56:98:a1
✨利用私钥提取两个人的公钥!!!
┌──(root㉿kali)-[/home/nanli]
└─# openssl rsa -in alice_private.pem -pubout > alice_public.pem
writing RSA key
┌──(root㉿kali)-[/home/nanli]
└─# openssl rsa -in bob_private.pem -pubout >bob_public.pem
writing RSA key
┌──(root㉿kali)-[/home/nanli]
└─# openssl rsa -in alice_public.pem -pubin -text -noout
Public-Key: (1024 bit)
Modulus:
00:a4:63:fb:49:ac:61:a0:32:d1:bd:09:43:08:dc:
b6:72:90:f5:59:4a:46:d7:8f:57:11:75:b2:c6:cf:
14:99:12:ac:46:3f:43:04:1f:87:f3:e3:25:1d:e9:
df:5e:5d:0a:9f:20:8e:a9:9a:94:28:85:50:52:11:
fc:4e:09:4f:e8:22:9a:7d:b2:ae:3f:e7:9f:94:e6:
27:e9:dd:aa:0d:63:92:55:05:be:99:2e:a1:cd:b5:
d2:b2:49:e6:c9:9e:b3:b2:f3:08:4c:45:43:63:c9:
f8:34:e9:3f:24:68:ae:db:7a:fc:49:12:20:e7:11:
35:47:89:81:9a:8b:49:2d:13
Exponent: 65537 (0x10001)
✨二人通过信息传输交换公钥!!!
✨使用公钥加密需要交换的消息!!!假设Alice需要与bob密码交流,她将秘密信息写入文件中,并将其保存到 1.txt 中。
🔖Alice利用openssl 的加密命令,将 明文1.txt 利 用bob 的公钥进行加密,输出密文在1.enc文件
openssl pkeyutl -encrypt -inkey bob_public.pem -pubin -in 1.txt -out 1.enc
🔖查看密文
head 1.enc
🔖bob 收到后利用自己的私钥将 1.enc 进行解密,解密后保存在 answer.txt 文件中
openssl pkeyutl -decrypt -inkey bob_private.pem -in 1.enc > answer.txt
🔖查看answer.txt文件,bob可查看Alice发送给他的加密信息
head answer.txt
✨在实战中RSA的常见套路!!!
(1)求私钥D
(2)分解p,q,利用如下网站实现分解factordb.com
from Crypto.Util.number import *
c = 87634567846785346787654567887654
n = 87654356789098765432456798456434
e = 65537
# 求解顺序 N L(通常被写为phi,也就是欧拉函数) E D
# N=p*q , L=(p-1)*(q-1), E*D mol L = 1, 求逆元可得 D, M(明文)=C^D(密文的D次幂) mol N
p = 87654356789098765432456798456434
q = 5140727833944742323
phi = (p-1)*(q-1)
d = inverse(e, phi)
# 计算明文
m = pow(c, d, n)
print(m)
即可得到明文!!!