当前位置:网站首页>With this, your messages can't be monitored
With this, your messages can't be monitored
2022-07-29 00:12:00 【somenzz】
I think everyone should learn to use RSA, Because only in the encrypted world , Our privacy can be truly protected . Today, let's share how to use Python To apply RSA.
Let's start with a scene , You are a A, Send an important message to B, But through any chat APP It's not safe , May be monitored , It may also be recorded , So you need to encrypt the message .
How to encrypt security , That's using RSA Public key encryption , Private key decryption , The public key can be made public , Private key is not public .
First step : Key generation and public key exchange
First pip install rsa
A Make a pair of RSA secret key , It's called public key A, Private key A
from rsa import newkeys
public_key_a, private_key_a = newkeys(2048, poolsize=8)
with open("public_key_a.pem", "wb") as f:
f.write(public_key_a.save_pkcs1(format="PEM"))
with open("private_key_a.pem", "wb") as f:
f.write(private_key_a.save_pkcs1(format="PEM"))public_key_a.pem Is as follows :
-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEArAdHKqjnVzeMt+4DpmQvoQsaKokL05+DS6Kykj1KiOmVWLreIUfP
fvedIRykBFNODwHQHGtWGPeaMYs86+9zdIRMtqC0TlXQyj74aP9CTAV43SSA65kY
EwQVJh3QBmrQok8lEljhN/QbiWrw4xG+rYP6BgsHFM3tuHb6oIkuhL6r+A3hj2ep
DY3jxn0ZPNSN10i9tTC/KXqKGnJrOAEQ6nNL/mAnFLwiuDQrEBmXfnK5bpw7hjDy
pJ0Cs3JM74+OTxo76Tbay1nWGGipLSXzRCXQW2+AD4Q6bFq6nuGic9gQLvQa11B7
XZVhVvVTxrezAOMYL3FOFXfIFqOB8HvTEQIDAQAB
-----END RSA PUBLIC KEY-----B Make a pair of RSA secret key , It's called public key B, Private key B.
from rsa import newkeys
public_key_b, private_key_b = newkeys(2048, poolsize=8)
with open("public_key_b.pem", "wb") as f:
f.write(public_key_b.save_pkcs1(format="PEM"))
with open("private_key_b.pem", "wb") as f:
f.write(private_key_b.save_pkcs1(format="PEM"))A The file public_key_a.pem Send to B,B Also put public_key_b.pem Send to A, This completes the exchange of keys . Back A Want to be with B signal communication , Just use B Public key encrypted message for ,B Decrypt with your own private key , You can get A Message sent , vice versa .
The second step : encryption
for instance A There is now a B The public key , When you want to encrypt messages , Load... First B The public key :
import base64
from rsa import PublicKey, PrivateKey, encrypt, decrypt,
public_key_b_file = Path("./public_key_b.pem")
public_key_b = PublicKey.load_pkcs1(public_key_b_file.read_bytes(), format="PEM")
print(public_key_b)You can get this output
PublicKey(21831899084185660921840174683452830587321319689015722246782364711292417600371532112177905506057539367671580668438126704427894857192387484162751992715040787885971624030995019473104987454304337735143558728644817397903950824366991556950207676816787133824709081376405184001095218083813620277536858163575686850410455092011765877504499366336792653732470469604531683754075419135479867324338689671063858801578735120084016574895760616498188773853425143006311923355945139917528996017456427975883103933944819388497917519791324247040364723098266886847457872058619189743684510784904551008906591879274280765194272666652349889346853, 65537)Then put the plaintext first according to utf-8 Encoded in bytes , In encryption , And then go base64, You can send it to B 了 .
import base64
from rsa import PublicKey, PrivateKey, encrypt, decrypt
if __name__ == "__main__":
public_key_b_file = Path("./public_key_b.pem")
public_key_b = PublicKey.load_pkcs1(public_key_b_file.read_bytes(), format="PEM")
# print(public_key_b)
orgin_message = " The withdrawal password is 123321"
encrypt_message = base64.b64encode(
encrypt(orgin_message.encode("utf-8"), public_key_b)
)
print(encrypt_message.decode())The ciphertext is :
mlVecdANZxsvXPivECRvNpoJr+2NxElM84moDr5N3FoKdxvubcHcteA4AW7UyHMj2PD96jaiQiD6hWX6M4dFePi05IE6k6JdAVXWSq5fTbZsiqBggidrcDL3azAaW5e4jQ6md6Bem1XcVAo2u1meIl+rXPKLUECu9R4tkgjtUTgUZpZ7rObl18Sz51A86ZxxRIEwkmG+TYJUPa27CyMPgD0zqlrKqh4SjIRZ1e3SmWxyet+3XFLE2b1XJwMhPUOFsJEO6qs204WtvpOYH0nGQorYsJe+ReIx/W0zZyK4zE+En9xW7fBByYNrseonh3sm6ALG6cRpZV/Odd2vmRUtOQ==And every time the same plaintext is encrypted into ciphertext, the result is different , Brute force cracking is almost impossible :

The second step : Decrypt
Decryption is to decrypt with your own key , Also load your private key first , And public_key_b It's a pair. .
import base64
from rsa import PublicKey, PrivateKey, encrypt, decrypt
if __name__ == "__main__":
encrypted_msg = "mlVecdANZxsvXPivECRvNpoJr+2NxElM84moDr5N3FoKdxvubcHcteA4AW7UyHMj2PD96jaiQiD6hWX6M4dFePi05IE6k6JdAVXWSq5fTbZsiqBggidrcDL3azAaW5e4jQ6md6Bem1XcVAo2u1meIl+rXPKLUECu9R4tkgjtUTgUZpZ7rObl18Sz51A86ZxxRIEwkmG+TYJUPa27CyMPgD0zqlrKqh4SjIRZ1e3SmWxyet+3XFLE2b1XJwMhPUOFsJEO6qs204WtvpOYH0nGQorYsJe+ReIx/W0zZyK4zE+En9xW7fBByYNrseonh3sm6ALG6cRpZV/Odd2vmRUtOQ=="
private_key_b_file = Path("./private_key_b.pem")
private_key_b = PrivateKey.load_pkcs1(private_key_b_file.read_bytes(), format="PEM")
origin_message = decrypt(base64.b64decode(encrypted_msg.encode()), private_key_b)
print(origin_message.decode("utf-8"))
# The withdrawal password is 123321as long as B The private key of has not been leaked , That's all B To unlock this ciphertext , So it's very safe .
Last words
In this article, we share our experience in Python How to use RSA encryption , You can make a communication program with encryption based on this , I hope it helped you .
recommend :
边栏推荐
- VMware VCSA 7.0 Install
- Interpretation of ISO 13400 (doip) standard
- feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
- Leetcode64. Minimum path sum
- ZABBIX 5.0 uses its own redis template for monitoring
- Connection pool - return connection details (Part 2)
- [TA frost wolf \u may - "hundred people plan"] Figure 3.6 texture compression - inclusion slimming
- Tyrosine decarboxylase -- characteristics of tyrosine decarboxylase of Streptococcus faecalis in Worthington
- EN 12101-8:2011 smoke dampers for smoke and heat control systems - CE certification
- Develop effective Tao spell
猜你喜欢

EN 1935 building hardware. Single axis hinge - CE certification

[TA frost wolf \u may - "hundred people plan"] Figure 3.6 texture compression - inclusion slimming

Powercli VMware vCenter deploys conventional new VMS in batch through self built PXE server with one click

The failure rate is as high as 80%. How to correctly complete the strategic planning of digital transformation?

【MySQL系列】MySQL数据库基础

Leetcode61. rotating linked list

研发效能的道法术器

【C】 Replace spaces and realize binary parity bit exchange of integers by macros

Es6操作教程

【C】喝汽水,找单身狗问题
随机推荐
Doip test development practice
Worthington RNA determination detailed introduction
Android studio连接MySQL并完成简单的登录注册功能
【MySQL 8】Generated Invisible Primary Keys(GIPK)
Web系统常见安全漏洞介绍及解决方案-CSRF攻击
Is the declarative code of compose so concise?
研发效能的道法术器
Cmake basic learning
Android studio connects to MySQL and completes simple login and registration functions
Websocket heartbeat mechanism (keep alive mechanism)
Multi sensor fusion positioning (I) -- 3D laser odometer
Eight performance analysis indicators of effective supply chain management (Part 1)
Linux之yum安装MySQL
Control fillet stroke materialshapedrawable
Okaleido ecological core equity Oka, all in fusion mining mode
Leetcode60. 排列序列
熊市下PLATO如何通过Elephant Swap,获得溢价收益?
Where is the DP interface of desktop computer (what if the host has no DP interface)
SQL implementation merges multiple rows of records into one row
Leetcode61. 旋转链表