当前位置:网站首页>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 :
边栏推荐
- 【TA-霜狼_may-《百人计划》】美术2.2 模型基础
- NAT如何配置地址转换
- 1-7 solve the problem of this pointing of methods in classes
- SQL实现将多行记录合并成一行
- 实时数仓:滴滴的实时数仓落地实践
- 1-7 解决类中方法的this指向问题
- Pycharm configuring the running environment
- 1-8 basic use of props
- Install MySQL using Yum for Linux
- 【C】 Replace spaces and realize binary parity bit exchange of integers by macros
猜你喜欢

【MySQL系列】MySQL数据库基础

After SAP Oracle replicates a new instance, the remote connection of the database reports an error ora-01031

Leetcode64. 最小路径和

Have passed hcip and joined the company of your choice, and share the learning experience and experience of Huawei certification

PowerCLi 批量添加esxi到vCenter

Build SSM project with JSP as view parser

AutoCAD -- import excel tables into CAD and merge CAD

Control fillet stroke materialshapedrawable

Real time data warehouse: meituan's implementation of real-time data warehouse construction based on Flink

Android studio connects to MySQL and completes simple login and registration functions
随机推荐
mysql索引失效的常见9种原因详解
SQL实现将多行记录合并成一行
熊市下PLATO如何通过Elephant Swap,获得溢价收益?
考过HCIP入职心仪公司,分享华为认证学习经历及心得
【TA-霜狼_may-《百人计划》】图形3.6 纹理压缩——包体瘦身术
Js判断数据类型的4种⽅式
SQL implementation merges multiple rows of records into one row
JS高级 之 ES6~ES13 新特性
[TA frost wolf \u may - "hundred people plan"] Figure 3.6 texture compression - inclusion slimming
Build SSM project with JSP as view parser
【微服务】Nacos集群搭建以及加载文件配置
Leetcode62. 不同路径
Worthington -- Specification of Worthington trypsin inhibitor
Detailed principle explanation and verification results of digital clock based on FPGA
Data warehouse: Doris' application practice in meituan
GhostNets on Heterogeneous Devices via Cheap Operations
curl (7) Failed connect to localhost8080; Connection refused
ISO 13400(DoIP)标准解读
IDEA报错Error running ‘Application‘ Command line is too long解决方案
laptop外接显示器