最近遇到一个问题,肉眼可见的使用了RSA加密,但是没找到传递公钥或者私钥,而是两个奇怪的值。

经过探索知道了他们分别叫  模数  指数

浅浅记录下该如何处理这类加密

#!/usr/bin/python
from binascii import a2b_hex, b2a_hex
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
from Crypto.Util.number import bytes_to_long


def encrypt_with_modulus(content, public_exponent, modulus=None):
    e = int(public_exponent, 16)  # 指数
    n = bytes_to_long(a2b_hex(modulus))
    rsa_key = RSA.construct((n, e))  # generate/export
    # public key
    public_key = rsa_key.publickey()
    cipher = PKCS1_v1_5.new(public_key)
    content = cipher.encrypt(content)
    content = b2a_hex(content)
    return str(content)


text = b'123'  # 待加密文本
public_exponent = '10001'
modulus = 'A23322F080BD5876C0735D585D25C7BC409F637237B07744D27FBF39FB'
result = encrypt_with_modulus(text, public_exponent, modulus)
print(result)

 

参考:

https://wenku.baidu.com/view/63e9d71864ec102de2bd960590c69ec3d5bbdbb2.html?fr=sogou&_wkts_=1687266653781

https://cloud.tencent.com/developer/article/1677831

https://blog.csdn.net/herishwater/article/details/91629372

https://www.coder.work/article/3208084

https://www.hreshhao.com/rsa-encryption/

https://www.ngui.cc/el/2223262.html?action=onClick

 posted on 2023-06-20 22:00  宁青楼  阅读(670)  评论(0)    收藏  举报