当前位置:网站首页>RSA encrypts data with private key and decrypts data with public key (not a signature verification process)
RSA encrypts data with private key and decrypts data with public key (not a signature verification process)
2022-07-28 14:09:00 【uiop_ uiop_ uiop】
RSA Encrypt data with private key and decrypt data with public key , Strictly speaking , Is not safe . But recently I'm writing a keygen When , This is what software authors use , So we can only have one such .
Some of the conventional ones rsa password utils There is no such function . Only sealed sign and verify function . But we don't need the inside hash function . So pick it out separately for use .
https://github.com/sybrenstuvel/python-rsa
Clone this warehouse . Time of my cloning by :2022 year 7 month 21 Japan ,22:55:29.

The last item commit hash:c4dc7beb04bea05ed86adb2e4b7f780f173774b8
It needs to be pip install rsa, And then in python Of site-packages Modify the corresponding pkcs1.py Document and __init__ file . And then directly import rsa that will do
def encrypt_by_privkey(msg: bytes, priv_key: key.PrivateKey) -> bytes:
# Encrypt the msg with the private key
cleartext = msg
keylength = common.byte_size(priv_key.n)
padded = _pad_for_signing(cleartext, keylength)
payload = transform.bytes2int(padded)
encrypted = priv_key.blinded_encrypt(payload)
block = transform.int2bytes(encrypted, keylength)
return block
def decrypt_by_pubkey(encrypted: bytes, pub_key: key.PublicKey) -> bytes:
keylength = common.byte_size(pub_key.n)
payload = transform.bytes2int(encrypted)
decrypted = core.decrypt_int(payload, pub_key.e, pub_key.n)
clearsig = transform.int2bytes(decrypted, keylength)
sep_idx = clearsig.find(b"\x00", 2)
return clearsig[sep_idx + 1 :]Add the revision code above rsa/pkcs1.py in .

After the modification rsa/__init__.py
from rsa.pkcs1 import (
encrypt,
decrypt,
encrypt_by_privkey,
decrypt_by_pubkey,
sign,
verify,
DecryptionError,
VerificationError,
find_signature_hash,
sign_hash,
compute_hash,
) 
__all__ = [
"newkeys",
"encrypt",
"decrypt",
"sign",
"verify",
"PublicKey",
"PrivateKey",
"DecryptionError",
"VerificationError",
"find_signature_hash",
"compute_hash",
"sign_hash",
"encrypt_by_privkey",
"decrypt_by_pubkey"
]
"encrypt_by_privkey",
"decrypt_by_pubkey"
Then you can encrypt ordinary content with the private key , Public key decrypts ordinary content .
Cloned python-rsa-main Change the name of this folder to py_rsa. After that py_rsa New in the same directory py file :
import py_rsa.rsa as rsa
import base64
pub = rsa.PublicKey.load_pkcs1_openssl_pem(open('pubkey.pem').read())
priv = rsa.PrivateKey.load_pkcs1(open('privkey.pem').read())
message = 'hello world now'.encode()
e = rsa.encrypt_by_privkey(message, priv)
d = rsa.decrypt_by_pubkey(e, pub)
print(d)
print(base64.b64encode(e).decode())
Output :
b'hello world now'
l2zTjZeDrrz2LHTQGn0QFzSYN81W4pYOnuuQrPGPEafh12rwX1rTGYhnuEpNVaghWmXDen45QWoWD3YZvWbOfbA5KMqmJph3UlVu0zvWXNQrSlg9ILJOjfCOdqQLyqzkEtoijIsDWKbr1lr5prh+GOHHPOmlF/jG95Dw2E5YZzU=
Use website tools to detect :
http://tool.chacuo.net/cryptrsapubkey

success .
边栏推荐
- 【LVGL事件(Events)】事件代码
- 安全保障基于软件全生命周期-PSP应用
- Jmeter安装教程及登录增加token
- 30 day question brushing training (I)
- Read how to deploy highly available k3s with external database
- Qt5开发从入门到精通——第一篇概述
- 文献阅读(245)Roller
- [lvgl events] Application of events on different components (I)
- 安全保障基于软件全生命周期-Istio的授权机制
- Tutorial on the principle and application of database system (062) -- MySQL exercise questions: operation questions 32-38 (6)
猜你喜欢

MVC模型:日历系统

Istio IV fault injection and link tracking

Multithreading and high concurrency (III) -- source code analysis AQS principle

What is the reason why the words behind word disappear when typing? How to solve it?

修订版 | 目标检测:速度和准确性比较(Faster R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)...

30 day question brushing training (I)

Operator3 - design an operator

多线程与高并发(三)—— 源码解析 AQS 原理

如何有效进行回顾会议(上)?

Implementation of StrCmp, strstr, memcpy, memmove
随机推荐
30 day question brushing plan (II)
【飞控开发基础教程7】疯壳·开源编队无人机-SPI(气压计数据获取)
Security assurance is based on software life cycle -istio authentication mechanism
DXF读写:标注样式组码中文说明
redis哨兵机制
牛客多校-Link with Level Edito I-(线性dp)
SLAM论文合集
Leetcode depth first and breadth first traversal
Docker deploys Mysql to realize remote connection [easy to understand]
Understanding of stack and practical application scenarios
Clickhouse架构与设计
DXF reading and writing: align the calculation of the position of the dimension text in the middle and above
了解BFC特性,轻松实现自适应布局
《机器学习》(周志华) 第6章 支持向量 学习心得 笔记
多线程与高并发(三)—— 源码解析 AQS 原理
qml 图片预览
Graph traversal (BFS & DFS basis)
Master closures and consolidate basic skills
文献阅读(245)Roller
目标检测:速度和准确性比较(Fater R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)