In order to make it work you need to convert key from str to tuple before decryption(ast.literal_eval function). Here is fixed code:
import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
import ast
random_generator = Random.new().read
key = RSA.generate(1024, random_generator) #generate pub and priv key
publickey = key.publickey() # pub key export for exchange
encrypted = publickey.encrypt('encrypt this message', 32)
#message to encrypt is in the above line 'encrypt this message'
print('encrypted message:', encrypted) #ciphertext
f = open ('encryption.txt', 'w')
f.write(str(encrypted)) #write ciphertext to file
f.close()
#decrypted code below
f = open('encryption.txt', 'r')
message = f.read()
decrypted = key.decrypt(ast.literal_eval(str(encrypted)))
print('decrypted', decrypted)
f = open ('encryption.txt', 'w')
f.write(str(message))
f.write(str(decrypted))
f.close()
Answer from Esdes on Stack OverflowStuvel
stuvel.eu › python-rsa-doc
Welcome to Python-RSA’s documentation! — Python-RSA 4.8 documentation
Python-RSA is a pure-Python RSA implementation.
PyCryptodome
pycryptodome.readthedocs.io › en › latest › src › public_key › rsa.html
RSA — PyCryptodome 3.23.0 documentation
Both RSA ciphertexts and RSA signatures are as large as the RSA modulus n (384 bytes if n is 3072 bit long).
Videos
43:30
RSA Encryption From Scratch - Math & Python Code - YouTube
RSA Encryption in Python | Coding Tutorial - YouTube
12:42
RSA Private & Public Key Encryption in Python - YouTube
15:53
RSA Encryption In Python - YouTube
01:13:44
Programming Challenges - 5 - RSA Encryption (Python) - YouTube
00:32
python rsa cryptography example - YouTube
PyPI
pypi.org › project › rsa
rsa · PyPI
It supports encryption and decryption, ... the commandline. The code was mostly written by Sybren A. Stüvel. Documentation can be found at the Python-RSA homepage....
» pip install rsa
Cryptography
cryptography.io › en › latest › hazmat › primitives › asymmetric › rsa
RSA — Cryptography 47.0.0.dev1 documentation
Generates a new RSA private key. key_size describes how many bits long the key should be. Larger keys provide more security; currently 1024 and below are considered breakable while 2048 or 4096 are reasonable default key sizes for new keys. The public_exponent indicates what one mathematical ...
Stuvel
stuvel.eu › python-rsa-doc › usage.html
5. Usage — Python-RSA 4.8 documentation
The most common way to use RSA with larger files uses a block cypher like AES or DES3 to encrypt the file with a random key, then encrypt the random key with RSA. You would send the encrypted file along with the encrypted key to the recipient. The complete flow is: ... Use that key to encrypt the file with AES. ... The recipient now reverses this process to obtain the encrypted file. ... The Python-RSA module does not contain functionality to do the AES encryption for you.
Chilkat
chilkatsoft.com › refdoc › pythonRsaRef.html
Rsa Python Reference Documentation
Decrypts encBytes with the RSA algorithm. encBytes holds the encrypted binary data. The Charset property specifies the byte representation for interpreting the decrypted text. Set usePrivateKey to True to use the private key for decryption; otherwise, set usePrivateKey to False to use the public ...
Top answer 1 of 7
56
In order to make it work you need to convert key from str to tuple before decryption(ast.literal_eval function). Here is fixed code:
import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
import ast
random_generator = Random.new().read
key = RSA.generate(1024, random_generator) #generate pub and priv key
publickey = key.publickey() # pub key export for exchange
encrypted = publickey.encrypt('encrypt this message', 32)
#message to encrypt is in the above line 'encrypt this message'
print('encrypted message:', encrypted) #ciphertext
f = open ('encryption.txt', 'w')
f.write(str(encrypted)) #write ciphertext to file
f.close()
#decrypted code below
f = open('encryption.txt', 'r')
message = f.read()
decrypted = key.decrypt(ast.literal_eval(str(encrypted)))
print('decrypted', decrypted)
f = open ('encryption.txt', 'w')
f.write(str(message))
f.write(str(decrypted))
f.close()
2 of 7
14
PKCS#1 OAEP is an asymmetric cipher based on RSA and the OAEP padding
from Crypto.PublicKey import RSA
from Crypto import Random
from Crypto.Cipher import PKCS1_OAEP
def rsa_encrypt_decrypt():
key = RSA.generate(2048)
private_key = key.export_key('PEM')
public_key = key.publickey().exportKey('PEM')
message = input('plain text for RSA encryption and decryption:')
message = str.encode(message)
rsa_public_key = RSA.importKey(public_key)
rsa_public_key = PKCS1_OAEP.new(rsa_public_key)
encrypted_text = rsa_public_key.encrypt(message)
#encrypted_text = b64encode(encrypted_text)
print('your encrypted_text is : {}'.format(encrypted_text))
rsa_private_key = RSA.importKey(private_key)
rsa_private_key = PKCS1_OAEP.new(rsa_private_key)
decrypted_text = rsa_private_key.decrypt(encrypted_text)
print('your decrypted_text is : {}'.format(decrypted_text))
GitHub
github.com › sybrenstuvel › python-rsa
GitHub - sybrenstuvel/python-rsa: Python-RSA is a pure-Python RSA implementation. · GitHub
April 20, 2025 - It supports encryption and decryption, ... the commandline. The code was mostly written by Sybren A. Stüvel. Documentation can be found at the Python-RSA homepage....
Starred by 491 users
Forked by 125 users
Languages Python 99.6% | Shell 0.4%
CircuitPython
docs.circuitpython.org › projects › rsa › en › latest
Introduction — Adafruit RSA Library 1.0 documentation
RSA implementation based on Sybren A. Stüvel’s python-rsa pure-python RSA implementation.
GitHub
github.com › awnonbhowmik › RSA-Python
GitHub - awnonbhowmik/RSA-Python: The RSA algorithm coded in Python · GitHub
RSA-Python/ ├── RSA_Python.py # Core implementation (key generation, encrypt, decrypt) ├── requirements.txt └── README.md
Starred by 36 users
Forked by 10 users
Languages Python
Stuvel
stuvel.eu › python-rsa-doc › reference.html
8. Reference — Python-RSA 4.8 documentation
The private key is also known as the ‘decryption key’ and is a rsa.PrivateKey object. ... accurate – when True, n will have exactly the number of bits you asked for. However, this makes key generation much slower. When False, n` may have slightly less bits. poolsize – the number of processes to use to generate the prime numbers. If set to a number > 1, a parallel algorithm will be used. This requires Python 2.6 or newer.
Google
chromium.googlesource.com › external › github.com › sybrenstuvel › python-rsa › + › refs › tags › version-4.5 › README.md
Pure Python RSA implementation
It supports encryption and decryption, signing and verifying signatures, and key generation according to PKCS#1 version 1.5. It can be used as a Python library as well as on the commandline. The code was mostly written by Sybren A. Stüvel. Documentation can be found at the Python-RSA homepage.
CircuitPython
docs.circuitpython.org › projects › rsa › en › latest › api.html
API Reference — Adafruit RSA Library 1.0 documentation
Module for calculating large primes, and RSA encryption, decryption, signing and verification. Includes generating public and private keys. WARNING: This implementation does not use compression of the cleartext input to prevent repetitions, or other common security improvements.
dr. Sybren
stuvel.eu › software › rsa
Python-RSA | dr. Sybren
Python-RSA has extensive documentation with plenty of code examples, interoperation with OpenSSL and more goodies.
Stuvel
stuvel.eu › files › python-rsa-doc › usage.html
5. Usage — Python-RSA 3.3 documentation
The most common way to use RSA ... key with RSA. You would send the encrypted file along with the encrypted key to the recipient. The complete flow is: ... Use that key to encrypt the file with AES. ... The recipient now reverses this process to obtain the encrypted file. ... The Python-RSA module ...
GitHub
github.com › deepin-community › python-rsa
GitHub - deepin-community/python-rsa
It supports encryption and decryption, ... the commandline. The code was mostly written by Sybren A. Stüvel. Documentation can be found at the Python-RSA homepage....
Author deepin-community
University of Toronto
teach.cs.toronto.edu › ~csc110y › fall › notes › 08-cryptography › 05-rsa-cryptosystem-implementation.html
8.5 Implementing RSA in Python
In the previous section we defined the RSA cryptosystem that used both a public key and private key to send encrypted messages between two parties. In this section, we will see how to implement the RSA cryptosystem in Python. First, we will see how to generate a private key when given two prime ...