Home > Backend Development > Python Tutorial > j Detailed explanation of Python's method of using rsa library for public key decryption

j Detailed explanation of Python's method of using rsa library for public key decryption

小云云
Release: 2017-12-12 09:16:06
Original
3484 people have browsed it

RSA is a public key cryptographic algorithm. The ciphertext of RSA is the result of mod N of the E-th power of the number in the plaintext of the code. The following article mainly introduces to you a tutorial on how to use rsa library for public key decryption in Python. The article introduces it in detail through sample code. Friends who need it can refer to it. I hope it can help you.

Preface

For decryption of RSA, that is, the Dth power of the ciphertext number can be found mod N, that is, the ciphertext and itself Do D times of multiplication, and then divide the result by N to find the remainder to get the plaintext. The combination of D and N is the private key.

The encryption and decryption of the algorithm is still very simple, but the algorithm for generating the public key and private key is not arbitrary. Use the RSA public key to decrypt. The openssl command is openssl rsautl -verify -in cipher_text -inkey public.pem -pubin -out clear_text. However, I haven’t found any blog posts on the Python Internet, only hash rsa decryption signature.

The rsa library is used here. If not, you can download it from the official website https://pypi.python.org/pypi/rsa/3.1.4.

You can refer to the specific installation method here: http://www.jb51.net/article/70331.htm

Think about the principle, and then look for it in the python code of the rsa library I searched for it, extracted it from the verify code, and tested it again. Everything was OK.

The code is as follows:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
#rsa
from rsa import PublicKey, common, transform, core
def f(cipher, PUBLIC_KEY):
 public_key = PublicKey.load_pkcs1(PUBLIC_KEY)
 encrypted = transform.bytes2int(cipher)
 decrypted = core.decrypt_int(encrypted, public_key.e, public_key.n)
 text = transform.int2bytes(decrypted) 
 if len(text) > 0 and text[0] == '\x01':
  pos = text.find('\x00')
  if pos > 0:
  return text[pos+1:]
  else:
  return None 
fn = sys.stdin.readline()[:-1]
public_key = sys.stdin.readline()[:-1]
x = f(open(fn).read(), open(public_key).read())
print x
Copy after login

Use shell to verify as follows:

$ openssl genrsa -out pri2048.pem 2048
Generating RSA private key, 2048 bit long modulus
..+++
..............................................+++
e is 65537 (0x10001)
 $ openssl rsa -in pri2048.pem -out pub2048.pem -RSAPublicKey_out
writing RSA key
 $ echo -n 'Just a test' >1.txt
 $ openssl rsautl -sign -in 1.txt -inkey pri2048.pem -out 1.bin
 $ { echo 1.bin; echo pub2048.pem; } | ./test_rsa.py
Just a test
Copy after login

Everything is OK. Note that -RSAPublicKey_out must be used to extract the public key pem from the private key, so that the first and last lines of the pem file are as follows , so that rsa.PublicKey.load_pkcs1 will know it.

Related recommendations:

Examples of implementation of permutation and combination calculation operations in Python

Examples of python implementation of binary search and quick sort Detailed explanation

Python example code using regular expression connector

The above is the detailed content of j Detailed explanation of Python's method of using rsa library for public key decryption. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template