当前位置:网站首页>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 :
边栏推荐
- #{}和${}的区别
- Wildcard ssl/tls certificate
- 【C】替换空格,宏实现整数的二进制奇偶位交换
- @Transactional 注解使用详解
- SQL left connection, internal connection writing method "recommended collection"
- 1-7 解决类中方法的this指向问题
- Es6操作教程
- EN 1873 assembly accessories for roofing - plastic single roof lamps - CE certification
- @Detailed explanation of the use of transactional annotation
- Dual for loop optimization
猜你喜欢

Principle of meter skipping

Real time data warehouse: Netease strictly selects the practice of real-time data warehouse based on Flink

DoIP测试开发实践

#{}和${}的区别

Centos7 install mysql8

Worthington RNA determination detailed introduction

DevOps在物联网解决方案中的应用

【微服务】Nacos集群搭建以及加载文件配置

VMware VCSA 7.0 Install

【TA-霜狼_may-《百人计划》】美术2.2 模型基础
随机推荐
SQL实现将多行记录合并成一行
Oracle超全SQL,细节狂魔
AutoCAD -- import excel tables into CAD and merge CAD
Leetcode62. 不同路径
Oracle create tablespaces and users
Word中的\n是什么?:^p
mysql索引失效的常见9种原因详解
双重for循环优化
[TA frost wolf \u may - "hundred people plan"] Figure 3.6 texture compression - inclusion slimming
Everything you have learned will come in handy at some point in your life (turn)
Detailed principle explanation and verification results of digital clock based on FPGA
After SAP Oracle replicates a new instance, the remote connection of the database reports an error ora-01031
Laptop external display
[detailed and super simple] how to use websocket links
ZABBIX 5.0 uses its own redis template for monitoring
Websocket heartbeat mechanism (keep alive mechanism)
【微服务】Nacos集群搭建以及加载文件配置
Es6操作教程
Install MySQL using Yum for Linux
Leetcode63. 不同路径 II