RSA public key file to decrypt ciphertext Principle Analysis

RSA public key file to decrypt ciphertext Principle Analysis

Foreword

RSA encryption algorithm is an asymmetric encryption algorithm. In public-key encryption and electronic commerce RSA is widely used. RSA 1977 by the Ronald Rivest (Ron Rivest), Adi Shamir (Adi Shamir) and Leonard Adleman (Leonard Adleman) presented together. At that time the three of them are working at MIT. RSA is the three of them last names beginning with the letter composed pieces together.

analysis

For public and private keys generated rsa algorithm, we can understand the principle of generation:Here Insert Picture Description

Message encryption

First message needs to be both a good agreement to m into a format smaller than N, and an integer coprime to N n. If the message is too long, the message can be divided into several sections, which is what we call the block encryption, encrypted for each portion using the formula:

Here Insert Picture Description

Decrypts the message

D with the key to decrypt:

Here Insert Picture Description
We can know, RSA public key there are two messages: modulus (modulus) and index (exponent), it is what we call the N and e. With these two as long as the information, we will be able to generate a public key, then use the rsa library to encrypt the data -

Script to achieve the following:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import rsa

key = rsa.PublicKey(modulus, exponent)
print key

This time we have the following publickey.pem file:

-----BEGIN PUBLIC KEY-----
MDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAMJjauXD2OQ/+5erCQKPGqxsC/bNPXDr
yigb/+l/vjDdAgMBAAE=
-----END PUBLIC KEY-----

We need to do now is put forward modulus and index from this string.

First we have to know what pem files are?

Simply put, pem file format is used for ASCII (Base64) encoding various X.509 v3 certificates.

The file begins with a line ----- BEGIN PUBLIC KEY ----- start, by the end ----- ----- END PUBLIC KEY

pem types of data other than contents are removed and End begin, according to the base64 encoding and decoding, the obtained data is to be increased or special characters cut -, \ n, \ r, begin information, end information and the like.

Here are pictures clearly explain the problem ~ ~
[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-WSxETJo8-1584673233751) (src =% 22https: //pic4.zhimg.com/80/v2-5465a9360bf315676873ef7f5141dba3_720w. jpg% 22 # pic_center)]

Since we now know this pem file format, and I know the contents of the data, how do we decrypt the contents of this document it?

We can do the following to try Base64 decoding attempt:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import base64

pubkey = "MDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAMJjauXD2OQ/+5erCQKPGqxsC/bNPXDr
yigb/+l/vjDdAgMBAAE="
b64_str = base64.b64decode(pubkey)
print b64_str
print len(b64_str)```


Released four original articles · won praise 0 · Views 134

Guess you like

Origin blog.csdn.net/qq_43453035/article/details/104985662